1 /* subst.c -- The part of the shell that does parameter, command, and
2 globbing substitutions. */
4 /* ``Have a little faith, there's magic in the night. You ain't a
5 beauty, but, hey, you're alright.'' */
7 /* Copyright (C) 1987,1989 Free Software Foundation, Inc.
9 This file is part of GNU Bash, the Bourne Again SHell.
11 Bash is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 2, or (at your option) any later
16 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License along
22 with Bash; see the file COPYING. If not, write to the Free Software
23 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
27 #include "bashtypes.h"
29 #include "chartypes.h"
34 #if defined (HAVE_UNISTD_H)
39 #include "posixstat.h"
44 #include "execute_cmd.h"
48 #include "mailcheck.h"
50 #include "builtins/getopt.h"
51 #include "builtins/common.h"
53 #include <tilde/tilde.h>
54 #include <glob/strmatch.h>
60 /* The size that strings change by. */
61 #define DEFAULT_INITIAL_ARRAY_SIZE 112
62 #define DEFAULT_ARRAY_SIZE 128
68 #define VT_ARRAYMEMBER 3
70 /* Flags for quoted_strchr */
71 #define ST_BACKSL 0x01
72 #define ST_CTLESC 0x02
74 /* These defs make it easier to use the editor. */
80 /* Evaluates to 1 if C is one of the shell's special parameters whose length
81 can be taken, but is also one of the special expansion characters. */
82 #define VALID_SPECIAL_LENGTH_PARAM(c) \
83 ((c) == '-' || (c) == '?' || (c) == '#')
85 /* Evaluates to 1 if C is one of the shell's special parameters for which an
86 indirect variable reference may be made. */
87 #define VALID_INDIR_PARAM(c) \
88 ((c) == '#' || (c) == '?' || (c) == '@' || (c) == '*')
90 /* Evaluates to 1 if C is one of the OP characters that follows the parameter
91 in ${parameter[:]OPword}. */
92 #define VALID_PARAM_EXPAND_CHAR(c) \
93 ((c) == '-' || (c) == '=' || (c) == '?' || (c) == '+')
95 /* Evaluates to 1 if this is one of the shell's special variables. */
96 #define SPECIAL_VAR(name, wi) \
97 ((DIGIT (*name) && all_digits (name)) || \
98 (name[1] == '\0' && (sh_syntaxtab[(unsigned char)*name] & CSPECVAR)) || \
99 (wi && name[2] == '\0' && VALID_INDIR_PARAM (name[1])))
101 /* An expansion function that takes a string and a quoted flag and returns
102 a WORD_LIST *. Used as the type of the third argument to
103 expand_string_if_necessary(). */
104 typedef WORD_LIST
*EXPFUNC
__P((char *, int));
106 /* Process ID of the last command executed within command substitution. */
107 pid_t last_command_subst_pid
= NO_PID
;
108 pid_t current_command_subst_pid
= NO_PID
;
110 /* Extern functions and variables from different files. */
111 extern int last_command_exit_value
;
112 extern int subshell_environment
, startup_state
;
113 extern int return_catch_flag
, return_catch_value
;
114 extern pid_t dollar_dollar_pid
;
115 extern int posixly_correct
;
116 extern char *this_command_name
;
117 extern struct fd_bitmap
*current_fds_to_close
;
118 extern int wordexp_only
;
120 /* Non-zero means to allow unmatched globbed filenames to expand to
122 int allow_null_glob_expansion
;
125 /* Variables to keep track of which words in an expanded word list (the
126 output of expand_word_list_internal) are the result of globbing
127 expansions. GLOB_ARGV_FLAGS is used by execute_cmd.c.
128 (CURRENTLY UNUSED). */
129 char *glob_argv_flags
;
130 static int glob_argv_flags_size
;
133 static WORD_LIST expand_word_error
, expand_word_fatal
;
134 static char expand_param_error
, expand_param_fatal
;
136 /* Tell the expansion functions to not longjmp back to top_level on fatal
137 errors. Enabled when doing completion and prompt string expansion. */
138 static int no_longjmp_on_fatal_error
= 0;
140 /* Set by expand_word_unsplit; used to inhibit splitting and re-joining
141 $* on $IFS, primarily when doing assignment statements. */
142 static int expand_no_split_dollar_star
= 0;
144 /* Used to hold a list of variable assignments preceding a command. Global
145 so the SIGCHLD handler in jobs.c can unwind-protect it when it runs a
147 WORD_LIST
*subst_assign_varlist
= (WORD_LIST
*)NULL
;
149 /* A WORD_LIST of words to be expanded by expand_word_list_internal,
150 without any leading variable assignments. */
151 static WORD_LIST
*garglist
= (WORD_LIST
*)NULL
;
153 static char *quoted_substring
__P((char *, int, int));
154 static inline char *quoted_strchr
__P((char *, int, int));
156 static char *expand_string_if_necessary
__P((char *, int, EXPFUNC
*));
157 static inline char *expand_string_to_string_internal
__P((char *, int, EXPFUNC
*));
158 static WORD_LIST
*call_expand_word_internal
__P((WORD_DESC
*, int, int, int *, int *));
159 static WORD_LIST
*expand_string_internal
__P((char *, int));
160 static WORD_LIST
*expand_string_leave_quoted
__P((char *, int));
161 static WORD_LIST
*expand_string_for_rhs
__P((char *, int, int *, int *));
163 static char *remove_quoted_escapes
__P((char *));
164 static WORD_LIST
*list_quote_escapes
__P((WORD_LIST
*));
165 static char *make_quoted_char
__P((int));
166 static WORD_LIST
*quote_list
__P((WORD_LIST
*));
167 static WORD_LIST
*dequote_list
__P((WORD_LIST
*));
168 static void remove_quoted_nulls
__P((char *));
170 static int unquoted_substring
__P((char *, char *));
171 static int unquoted_member
__P((int, char *));
173 static int do_assignment_internal
__P((const char *, int));
175 static char *string_extract_verbatim
__P((char *, int *, char *));
176 static char *string_extract
__P((char *, int *, char *, int));
177 static char *string_extract_double_quoted
__P((char *, int *, int));
178 static char *string_extract_single_quoted
__P((char *, int *));
179 static inline int skip_single_quoted
__P((char *, int));
180 static int skip_double_quoted
__P((char *, int));
181 static char *extract_delimited_string
__P((char *, int *, char *, char *, char *));
182 static char *extract_dollar_brace_string
__P((char *, int *, int));
184 static char *string_list_internal
__P((WORD_LIST
*, char *));
185 static char *pos_params
__P((char *, int, int, int));
187 static char *remove_pattern
__P((char *, char *, int));
188 static int match_pattern_char
__P((char *, char *));
189 static int match_pattern
__P((char *, char *, int, char **, char **));
190 static int getpatspec
__P((int, char *));
191 static char *getpattern
__P((char *, int, int));
192 static char *parameter_brace_remove_pattern
__P((char *, char *, int, int));
193 static char *list_remove_pattern
__P((WORD_LIST
*, char *, int, int, int));
194 static char *parameter_list_remove_pattern
__P((char *, int, int, int));
196 static char *array_remove_pattern
__P((char *, char *, char *, int, int));
199 static char *process_substitute
__P((char *, int));
201 static char *read_comsub
__P((int, int));
204 static arrayind_t array_length_reference
__P((char *));
207 static int valid_brace_expansion_word
__P((char *, int));
208 static char *parameter_brace_expand_word
__P((char *, int, int));
209 static char *parameter_brace_expand_indir
__P((char *, int, int));
210 static char *parameter_brace_expand_rhs
__P((char *, char *, int, int, int *, int *));
211 static void parameter_brace_expand_error
__P((char *, char *));
213 static int valid_length_expression
__P((char *));
214 static long parameter_brace_expand_length
__P((char *));
216 static char *skiparith
__P((char *, int));
217 static int verify_substring_values
__P((char *, char *, int, long *, long *));
218 static int get_var_and_type
__P((char *, char *, SHELL_VAR
**, char **));
219 static char *parameter_brace_substring
__P((char *, char *, char *, int));
221 static char *pos_params_pat_subst
__P((char *, char *, char *, int));
223 static char *parameter_brace_patsub
__P((char *, char *, char *, int));
225 static char *parameter_brace_expand
__P((char *, int *, int, int *, int *));
226 static char *param_expand
__P((char *, int *, int, int *, int *, int *, int *, int));
228 static WORD_LIST
*expand_word_internal
__P((WORD_DESC
*, int, int, int *, int *));
230 static char *getifs
__P((void));
231 static WORD_LIST
*word_list_split
__P((WORD_LIST
*));
233 static WORD_LIST
*separate_out_assignments
__P((WORD_LIST
*));
234 static WORD_LIST
*glob_expand_word_list
__P((WORD_LIST
*, int));
235 #ifdef BRACE_EXPANSION
236 static WORD_LIST
*brace_expand_word_list
__P((WORD_LIST
*, int));
238 static WORD_LIST
*shell_expand_word_list
__P((WORD_LIST
*, int));
239 static WORD_LIST
*expand_word_list_internal
__P((WORD_LIST
*, int));
241 /* **************************************************************** */
243 /* Utility Functions */
245 /* **************************************************************** */
247 /* Cons a new string from STRING starting at START and ending at END,
248 not including END. */
250 substring (string
, start
, end
)
255 register char *result
;
258 result
= (char *)xmalloc (len
+ 1);
259 strncpy (result
, string
+ start
, len
);
265 quoted_substring (string
, start
, end
)
270 register char *result
, *s
, *r
;
274 /* Move to string[start], skipping quoted characters. */
275 for (s
= string
, l
= 0; *s
&& l
< start
; )
287 r
= result
= (char *)xmalloc (2*len
+ 1); /* save room for quotes */
289 /* Copy LEN characters, including quote characters. */
291 for (l
= 0; l
< len
; s
++)
304 /* Find the first occurrence of character C in string S, obeying shell
305 quoting rules. If (FLAGS & ST_BACKSL) is non-zero, backslash-escaped
306 characters are skipped. If (FLAGS & ST_CTLESC) is non-zero, characters
307 escaped with CTLESC are skipped. */
309 quoted_strchr (s
, c
, flags
)
317 if (((flags
& ST_BACKSL
) && *p
== '\\')
318 || ((flags
& ST_CTLESC
) && *p
== CTLESC
))
322 return ((char *)NULL
);
328 return ((char *)NULL
);
331 /* Return 1 if CHARACTER appears in an unquoted portion of
332 STRING. Return 0 otherwise. */
334 unquoted_member (character
, string
)
340 for (sindex
= 0; c
= string
[sindex
]; )
358 sindex
= skip_single_quoted (string
, ++sindex
);
362 sindex
= skip_double_quoted (string
, ++sindex
);
369 /* Return 1 if SUBSTR appears in an unquoted portion of STRING. */
371 unquoted_substring (substr
, string
)
372 char *substr
, *string
;
374 int sindex
, c
, sublen
;
376 if (substr
== 0 || *substr
== '\0')
379 sublen
= strlen (substr
);
380 for (sindex
= 0; c
= string
[sindex
]; )
382 if (STREQN (string
+ sindex
, substr
, sublen
))
395 sindex
= skip_single_quoted (string
, ++sindex
);
399 sindex
= skip_double_quoted (string
, ++sindex
);
410 /* Most of the substitutions must be done in parallel. In order
411 to avoid using tons of unclear goto's, I have some functions
412 for manipulating malloc'ed strings. They all take INDX, a
413 pointer to an integer which is the offset into the string
414 where manipulation is taking place. They also take SIZE, a
415 pointer to an integer which is the current length of the
416 character array for this string. */
418 /* Append SOURCE to TARGET at INDEX. SIZE is the current amount
419 of space allocated to TARGET. SOURCE can be NULL, in which
420 case nothing happens. Gets rid of SOURCE by freeing it.
421 Returns TARGET in case the location has changed. */
423 sub_append_string (source
, target
, indx
, size
)
424 char *source
, *target
;
431 srclen
= STRLEN (source
);
432 if (srclen
>= (int)(*size
- *indx
))
435 n
= (n
+ DEFAULT_ARRAY_SIZE
) - (n
% DEFAULT_ARRAY_SIZE
);
436 target
= (char *)xrealloc (target
, (*size
= n
));
439 FASTCOPY (source
, target
+ *indx
, srclen
);
441 target
[*indx
] = '\0';
450 /* Append the textual representation of NUMBER to TARGET.
451 INDX and SIZE are as in SUB_APPEND_STRING. */
453 sub_append_number (number
, target
, indx
, size
)
460 temp
= itos (number
);
461 return (sub_append_string (temp
, target
, indx
, size
));
465 /* Extract a substring from STRING, starting at SINDEX and ending with
466 one of the characters in CHARLIST. Don't make the ending character
467 part of the string. Leave SINDEX pointing at the ending character.
468 Understand about backslashes in the string. If VARNAME is non-zero,
469 and array variables have been compiled into the shell, everything
470 between a `[' and a corresponding `]' is skipped over. */
472 string_extract (string
, sindex
, charlist
, varname
)
481 for (i
= *sindex
; c
= string
[i
]; i
++)
488 #if defined (ARRAY_VARS)
489 else if (varname
&& c
== '[')
492 /* If this is an array subscript, skip over it and continue. */
493 ni
= skipsubscript (string
, i
);
494 if (string
[ni
] == ']')
498 else if (MEMBER (c
, charlist
))
502 temp
= substring (string
, *sindex
, i
);
507 /* Extract the contents of STRING as if it is enclosed in double quotes.
508 SINDEX, when passed in, is the offset of the character immediately
509 following the opening double quote; on exit, SINDEX is left pointing after
510 the closing double quote. If STRIPDQ is non-zero, unquoted double
511 quotes are stripped and the string is terminated by a null byte.
512 Backslashes between the embedded double quotes are processed. If STRIPDQ
513 is zero, an unquoted `"' terminates the string. */
515 string_extract_double_quoted (string
, sindex
, stripdq
)
517 int *sindex
, stripdq
;
521 char *temp
, *ret
; /* The new string we return. */
522 int pass_next
, backquote
, si
; /* State variables for the machine. */
525 pass_next
= backquote
= dquote
= 0;
526 temp
= (char *)xmalloc (1 + strlen (string
) - *sindex
);
528 for (j
= 0, i
= *sindex
; c
= string
[i
]; i
++)
530 /* Process a character that was quoted by a backslash. */
535 ``The backslash shall retain its special meaning as an escape
536 character only when followed by one of the characters:
539 If STRIPDQ is zero, we handle the double quotes here and let
540 expand_word_internal handle the rest. If STRIPDQ is non-zero,
541 we have already been through one round of backslash stripping,
542 and want to strip these backslashes only if DQUOTE is non-zero,
543 indicating that we are inside an embedded double-quoted string. */
545 /* If we are in an embedded quoted string, then don't strip
546 backslashes before characters for which the backslash
547 retains its special meaning, but remove backslashes in
548 front of other characters. If we are not in an
549 embedded quoted string, don't strip backslashes at all.
550 This mess is necessary because the string was already
551 surrounded by double quotes (and sh has some really weird
553 The returned string will be run through expansion as if
554 it were double-quoted. */
555 if ((stripdq
== 0 && c
!= '"') ||
556 (stripdq
&& ((dquote
&& (sh_syntaxtab
[c
] & CBSDQUOTE
)) || dquote
== 0)))
563 /* A backslash protects the next character. The code just above
564 handles preserving the backslash in front of any character but
572 /* Inside backquotes, ``the portion of the quoted string from the
573 initial backquote and the characters up to the next backquote
574 that is not preceded by a backslash, having escape characters
575 removed, defines that command''. */
591 /* Pass everything between `$(' and the matching `)' or a quoted
592 ${ ... } pair through according to the Posix.2 specification. */
593 if (c
== '$' && ((string
[i
+ 1] == LPAREN
) || (string
[i
+ 1] == LBRACE
)))
596 if (string
[i
+ 1] == LPAREN
)
597 ret
= extract_delimited_string (string
, &si
, "$(", "(", ")"); /*)*/
599 ret
= extract_dollar_brace_string (string
, &si
, 1);
602 temp
[j
++] = string
[i
+ 1];
604 for (t
= 0; ret
[t
]; t
++, j
++)
606 temp
[j
++] = string
[si
];
613 /* Add any character but a double quote to the quoted string we're
632 /* Point to after the closing quote. */
640 /* This should really be another option to string_extract_double_quoted. */
642 skip_double_quoted (string
, sind
)
648 int pass_next
, backquote
, si
;
650 pass_next
= backquote
= 0;
652 for (i
= sind
; c
= string
[i
]; i
++)
675 else if (c
== '$' && ((string
[i
+ 1] == LPAREN
) || (string
[i
+ 1] == LBRACE
)))
678 if (string
[i
+ 1] == LPAREN
)
679 ret
= extract_delimited_string (string
, &si
, "$(", "(", ")");
681 ret
= extract_dollar_brace_string (string
, &si
, 0);
699 /* Extract the contents of STRING as if it is enclosed in single quotes.
700 SINDEX, when passed in, is the offset of the character immediately
701 following the opening single quote; on exit, SINDEX is left pointing after
702 the closing single quote. */
704 string_extract_single_quoted (string
, sindex
)
711 for (i
= *sindex
; string
[i
] && string
[i
] != '\''; i
++)
714 t
= substring (string
, *sindex
, i
);
724 skip_single_quoted (string
, sind
)
730 for (c
= sind
; string
[c
] && string
[c
] != '\''; c
++)
737 /* Just like string_extract, but doesn't hack backslashes or any of
738 that other stuff. Obeys CTLESC quoting. Used to do splitting on $IFS. */
740 string_extract_verbatim (string
, sindex
, charlist
)
745 register int i
= *sindex
;
749 if (charlist
[0] == '\'' && charlist
[1] == '\0')
751 temp
= string_extract_single_quoted (string
, sindex
);
752 --*sindex
; /* leave *sindex at separator character */
756 for (i
= *sindex
; c
= string
[i
]; i
++)
764 if (MEMBER (c
, charlist
))
768 temp
= substring (string
, *sindex
, i
);
774 /* Extract the $( construct in STRING, and return a new string.
775 Start extracting at (SINDEX) as if we had just seen "$(".
776 Make (SINDEX) get the position of the matching ")". */
778 extract_command_subst (string
, sindex
)
782 return (extract_delimited_string (string
, sindex
, "$(", "(", ")"));
785 /* Extract the $[ construct in STRING, and return a new string. (])
786 Start extracting at (SINDEX) as if we had just seen "$[".
787 Make (SINDEX) get the position of the matching "]". */
789 extract_arithmetic_subst (string
, sindex
)
793 return (extract_delimited_string (string
, sindex
, "$[", "[", "]")); /*]*/
796 #if defined (PROCESS_SUBSTITUTION)
797 /* Extract the <( or >( construct in STRING, and return a new string.
798 Start extracting at (SINDEX) as if we had just seen "<(".
799 Make (SINDEX) get the position of the matching ")". */ /*))*/
801 extract_process_subst (string
, starter
, sindex
)
806 return (extract_delimited_string (string
, sindex
, starter
, "(", ")"));
808 #endif /* PROCESS_SUBSTITUTION */
810 #if defined (ARRAY_VARS)
812 extract_array_assignment_list (string
, sindex
)
816 return (extract_delimited_string (string
, sindex
, "(", (char *)NULL
, ")"));
820 /* Extract and create a new string from the contents of STRING, a
821 character string delimited with OPENER and CLOSER. SINDEX is
822 the address of an int describing the current offset in STRING;
823 it should point to just after the first OPENER found. On exit,
824 SINDEX gets the position of the last character of the matching CLOSER.
825 If OPENER is more than a single character, ALT_OPENER, if non-null,
826 contains a character string that can also match CLOSER and thus
827 needs to be skipped. */
829 extract_delimited_string (string
, sindex
, opener
, alt_opener
, closer
)
832 char *opener
, *alt_opener
, *closer
;
836 int pass_character
, nesting_level
;
837 int len_closer
, len_opener
, len_alt_opener
;
839 len_opener
= STRLEN (opener
);
840 len_alt_opener
= STRLEN (alt_opener
);
841 len_closer
= STRLEN (closer
);
848 while (nesting_level
)
855 if (pass_character
) /* previous char was backslash */
876 /* Process a nested OPENER. */
877 if (STREQN (string
+ i
, opener
, len_opener
))
880 t
= extract_delimited_string (string
, &si
, opener
, alt_opener
, closer
);
886 /* Process a nested ALT_OPENER */
887 if (len_alt_opener
&& STREQN (string
+ i
, alt_opener
, len_alt_opener
))
889 si
= i
+ len_alt_opener
;
890 t
= extract_delimited_string (string
, &si
, alt_opener
, alt_opener
, closer
);
896 /* If the current substring terminates the delimited string, decrement
897 the nesting level. */
898 if (STREQN (string
+ i
, closer
, len_closer
))
900 i
+= len_closer
- 1; /* move to last char of the closer */
902 if (nesting_level
== 0)
906 /* Pass old-style command substitution through verbatim. */
910 t
= string_extract (string
, &si
, "`", 0);
916 /* Pass single-quoted strings through verbatim. */
920 i
= skip_single_quoted (string
, si
);
924 /* Pass embedded double-quoted strings through verbatim as well. */
928 i
= skip_double_quoted (string
, si
);
932 i
++; /* move past this character, which was not special. */
936 if (c
== 0 && nesting_level
)
938 if (c
== 0 && nesting_level
&& no_longjmp_on_fatal_error
== 0)
941 report_error ("bad substitution: no `%s' in %s", closer
, string
);
942 last_command_exit_value
= EXECUTION_FAILURE
;
943 jump_to_top_level (DISCARD
);
946 si
= i
- *sindex
- len_closer
+ 1;
947 result
= (char *)xmalloc (1 + si
);
948 strncpy (result
, string
+ *sindex
, si
);
955 /* Extract a parameter expansion expression within ${ and } from STRING.
956 Obey the Posix.2 rules for finding the ending `}': count braces while
957 skipping over enclosed quoted strings and command substitutions.
958 SINDEX is the address of an int describing the current offset in STRING;
959 it should point to just after the first `{' found. On exit, SINDEX
960 gets the position of the matching `}'. QUOTED is non-zero if this
961 occurs inside double quotes. */
962 /* XXX -- this is very similar to extract_delimited_string -- XXX */
964 extract_dollar_brace_string (string
, sindex
, quoted
)
969 int pass_character
, nesting_level
, si
;
976 for (i
= *sindex
; (c
= string
[i
]); i
++)
984 /* CTLESCs and backslashes quote the next character. */
985 if (c
== CTLESC
|| c
== '\\')
991 if (string
[i
] == '$' && string
[i
+1] == LBRACE
)
1001 if (nesting_level
== 0)
1006 /* Pass the contents of old-style command substitutions through
1011 t
= string_extract (string
, &si
, "`", 0);
1017 /* Pass the contents of new-style command substitutions and
1018 arithmetic substitutions through verbatim. */
1019 if (string
[i
] == '$' && string
[i
+1] == LPAREN
)
1022 t
= extract_delimited_string (string
, &si
, "$(", "(", ")"); /*)*/
1028 /* Pass the contents of single-quoted and double-quoted strings
1029 through verbatim. */
1030 if (c
== '\'' || c
== '"')
1033 i
= (c
== '\'') ? skip_single_quoted (string
, si
)
1034 : skip_double_quoted (string
, si
);
1035 /* skip_XXX_quoted leaves index one past close quote */
1041 if (c
== 0 && nesting_level
&& no_longjmp_on_fatal_error
== 0)
1043 report_error ("bad substitution: no ending `}' in %s", string
);
1044 last_command_exit_value
= EXECUTION_FAILURE
;
1045 jump_to_top_level (DISCARD
);
1048 result
= substring (string
, *sindex
, i
);
1054 /* Remove backslashes which are quoting backquotes from STRING. Modifies
1055 STRING, and returns a pointer to it. */
1057 de_backslash (string
)
1062 for (i
= 0, l
= strlen (string
); i
< l
; i
++)
1063 if (string
[i
] == '\\' && (string
[i
+ 1] == '`' || string
[i
+ 1] == '\\' ||
1064 string
[i
+ 1] == '$'))
1065 strcpy (string
+ i
, string
+ i
+ 1); /* XXX - should be memmove */
1071 /* Replace instances of \! in a string with !. */
1073 unquote_bang (string
)
1077 register char *temp
;
1079 temp
= (char *)xmalloc (1 + strlen (string
));
1081 for (i
= 0, j
= 0; (temp
[j
] = string
[i
]); i
++, j
++)
1083 if (string
[i
] == '\\' && string
[i
+ 1] == '!')
1089 strcpy (string
, temp
);
1094 #if defined (READLINE)
1095 /* Return 1 if the portion of STRING ending at EINDEX is quoted (there is
1096 an unclosed quoted string), or if the character at EINDEX is quoted
1097 by a backslash. NO_LONGJMP_ON_FATAL_ERROR is used to flag that the various
1098 single and double-quoted string parsing functions should not return an
1099 error if there are unclosed quotes or braces. */
1101 #define CQ_RETURN(x) do { no_longjmp_on_fatal_error = 0; return (x); } while (0)
1104 char_is_quoted (string
, eindex
)
1110 no_longjmp_on_fatal_error
= 1;
1111 for (i
= pass_next
= 0; i
<= eindex
; i
++)
1116 if (i
>= eindex
) /* XXX was if (i >= eindex - 1) */
1120 else if (string
[i
] == '\'' || string
[i
] == '"')
1122 i
= (string
[i
] == '\'') ? skip_single_quoted (string
, ++i
)
1123 : skip_double_quoted (string
, ++i
);
1126 i
--; /* the skip functions increment past the closing quote. */
1128 else if (string
[i
] == '\\')
1138 unclosed_pair (string
, eindex
, openstr
)
1143 int i
, pass_next
, openc
, olen
;
1145 olen
= strlen (openstr
);
1146 for (i
= pass_next
= openc
= 0; i
<= eindex
; i
++)
1151 if (i
>= eindex
) /* XXX was if (i >= eindex - 1) */
1155 else if (STREQN (string
+ i
, openstr
, olen
))
1160 else if (string
[i
] == '\'' || string
[i
] == '"')
1162 i
= (string
[i
] == '\'') ? skip_single_quoted (string
, i
)
1163 : skip_double_quoted (string
, i
);
1167 else if (string
[i
] == '\\')
1176 /* Skip characters in STRING until we find a character in DELIMS, and return
1177 the index of that character. START is the index into string at which we
1178 begin. This is similar in spirit to strpbrk, but it returns an index into
1179 STRING and takes a starting index. This little piece of code knows quite
1180 a lot of shell syntax. It's very similar to skip_double_quoted and other
1181 functions of that ilk. */
1183 skip_to_delim (string
, start
, delims
)
1188 int i
, pass_next
, backq
, si
;
1191 no_longjmp_on_fatal_error
= 1;
1192 for (i
= start
, pass_next
= backq
= 0; string
[i
]; i
++)
1201 else if (string
[i
] == '\\')
1208 if (string
[i
] == '`')
1212 else if (string
[i
] == '`')
1217 else if (string
[i
] == '\'' || string
[i
] == '"')
1219 i
= (string
[i
] == '\'') ? skip_single_quoted (string
, ++i
)
1220 : skip_double_quoted (string
, ++i
);
1221 i
--; /* the skip functions increment past the closing quote. */
1223 else if (string
[i
] == '$' && (string
[i
+1] == LPAREN
|| string
[i
+1] == LBRACE
))
1226 if (string
[si
] == '\0')
1229 if (string
[i
+1] == LPAREN
)
1230 temp
= extract_delimited_string (string
, &si
, "$(", "(", ")"); /* ) */
1232 temp
= extract_dollar_brace_string (string
, &si
, 0);
1235 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
1239 else if (member (string
[i
], delims
))
1245 /* Split STRING (length SLEN) at DELIMS, and return a WORD_LIST with the
1246 individual words. If DELIMS is NULL, the current value of $IFS is used
1247 to split the string. SENTINEL is an index to look for. NWP, if non-NULL
1248 gets the number of words in the returned list. CWP, if non-NULL, gets
1249 the index of the word containing SENTINEL. Non-whitespace chars in
1250 DELIMS delimit separate fields. */
1252 split_at_delims (string
, slen
, delims
, sentinel
, nwp
, cwp
)
1259 int ts
, te
, i
, nw
, cw
;
1260 char *token
, *d
, *d2
;
1261 WORD_LIST
*ret
, *tl
;
1263 if (string
== 0 || *string
== '\0')
1269 return ((WORD_LIST
*)NULL
);
1272 d
= (delims
== 0) ? getifs () : delims
;
1274 /* Make d2 the non-whitespace characters in delims */
1278 d2
= (char *)xmalloc (strlen (delims
) + 1);
1279 for (i
= ts
= 0; delims
[i
]; i
++)
1281 if (whitespace(delims
[i
]) == 0)
1282 d2
[ts
++] = delims
[i
];
1287 ret
= (WORD_LIST
*)NULL
;
1289 for (i
= 0; member (string
[i
], d
) && (whitespace(string
[i
]) || string
[i
] == '\n'); i
++)
1291 if (string
[i
] == '\0')
1299 te
= skip_to_delim (string
, ts
, d
);
1301 /* If we have a non-whitespace delimiter character, use it to make a
1302 separate field. This is just about what $IFS splitting does and
1303 is closer to the behavior of the shell parser. */
1304 if (ts
== te
&& d2
&& member (string
[ts
], d2
))
1307 while (member (string
[te
], d2
))
1311 token
= substring (string
, ts
, te
);
1313 ret
= add_string_to_list (token
, ret
);
1317 if (sentinel
>= ts
&& sentinel
<= te
)
1320 /* If the cursor is at whitespace just before word start, set the
1321 sentinel word to the current word. */
1322 if (cwp
&& cw
== -1 && sentinel
== ts
-1)
1325 /* If the cursor is at whitespace between two words, make a new, empty
1326 word, add it before (well, after, since the list is in reverse order)
1327 the word we just added, and set the current word to that one. */
1328 if (cwp
&& cw
== -1 && sentinel
< ts
)
1330 tl
= (WORD_LIST
*)xmalloc (sizeof (WORD_LIST
));
1331 tl
->word
= make_word ("");
1332 tl
->next
= ret
->next
;
1338 if (string
[te
] == 0)
1341 i
= te
/* + member (string[te], d) */;
1342 while (member (string
[i
], d
) && whitespace(string
[i
]))
1351 /* Special case for SENTINEL at the end of STRING. If we haven't found
1352 the word containing SENTINEL yet, and the index we're looking for is at
1353 the end of STRING, add an additional null argument and set the current
1354 word pointer to that. */
1355 if (cwp
&& cw
== -1 && sentinel
>= slen
)
1357 if (whitespace (string
[sentinel
- 1]))
1360 ret
= add_string_to_list (token
, ret
);
1371 return (REVERSE_LIST (ret
, WORD_LIST
*));
1373 #endif /* READLINE */
1377 /* Extract the name of the variable to bind to from the assignment string. */
1379 assignment_name (string
)
1385 offset
= assignment (string
);
1387 return (char *)NULL
;
1388 temp
= substring (string
, 0, offset
);
1393 /* **************************************************************** */
1395 /* Functions to convert strings to WORD_LISTs and vice versa */
1397 /* **************************************************************** */
1399 /* Return a single string of all the words in LIST. SEP is the separator
1400 to put between individual elements of LIST in the output string. */
1402 string_list_internal (list
, sep
)
1406 register WORD_LIST
*t
;
1408 int word_len
, sep_len
, result_size
;
1411 return ((char *)NULL
);
1413 /* This is nearly always called with either sep[0] == 0 or sep[1] == 0. */
1414 sep_len
= STRLEN (sep
);
1417 for (t
= list
; t
; t
= t
->next
)
1420 result_size
+= sep_len
;
1421 result_size
+= strlen (t
->word
->word
);
1424 r
= result
= (char *)xmalloc (result_size
+ 1);
1426 for (t
= list
; t
; t
= t
->next
)
1428 if (t
!= list
&& sep_len
)
1432 FASTCOPY (sep
, r
, sep_len
);
1439 word_len
= strlen (t
->word
->word
);
1440 FASTCOPY (t
->word
->word
, r
, word_len
);
1448 /* Return a single string of all the words present in LIST, separating
1449 each word with a space. */
1454 return (string_list_internal (list
, " "));
1457 /* Return a single string of all the words present in LIST, obeying the
1458 quoting rules for "$*", to wit: (P1003.2, draft 11, 3.5.2) "If the
1459 expansion [of $*] appears within a double quoted string, it expands
1460 to a single field with the value of each parameter separated by the
1461 first character of the IFS variable, or by a <space> if IFS is unset." */
1463 string_list_dollar_star (list
)
1468 ifs
= get_string_value ("IFS");
1470 sep
[0] = (ifs
== 0) ? ' ' : *ifs
;
1473 return (string_list_internal (list
, sep
));
1476 /* Turn $@ into a string. If (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
1477 is non-zero, the $@ appears within double quotes, and we should quote
1478 the list before converting it into a string. If IFS is unset, and the
1479 word is not quoted, we just need to quote CTLESC and CTLNUL characters
1480 in the words in the list, because the default value of $IFS is
1481 <space><tab><newline>, IFS characters in the words in the list should
1482 also be split. If IFS is null, and the word is not quoted, we need
1483 to quote the words in the list to preserve the positional parameters
1486 string_list_dollar_at (list
, quoted
)
1493 ifs
= get_string_value ("IFS");
1495 sep
[0] = (ifs
== 0 || *ifs
== 0) ? ' ' : *ifs
;
1498 tlist
= ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || (ifs
&& *ifs
== 0))
1500 : list_quote_escapes (list
);
1501 return (string_list_internal (tlist
, sep
));
1504 /* Return the list of words present in STRING. Separate the string into
1505 words at any of the characters found in SEPARATORS. If QUOTED is
1506 non-zero then word in the list will have its quoted flag set, otherwise
1507 the quoted flag is left as make_word () deemed fit.
1509 This obeys the P1003.2 word splitting semantics. If `separators' is
1510 exactly <space><tab><newline>, then the splitting algorithm is that of
1511 the Bourne shell, which treats any sequence of characters from `separators'
1512 as a delimiter. If IFS is unset, which results in `separators' being set
1513 to "", no splitting occurs. If separators has some other value, the
1514 following rules are applied (`IFS white space' means zero or more
1515 occurrences of <space>, <tab>, or <newline>, as long as those characters
1516 are in `separators'):
1518 1) IFS white space is ignored at the start and the end of the
1520 2) Each occurrence of a character in `separators' that is not
1521 IFS white space, along with any adjacent occurrences of
1522 IFS white space delimits a field.
1523 3) Any nonzero-length sequence of IFS white space delimits a field.
1526 /* BEWARE! list_string strips null arguments. Don't call it twice and
1527 expect to have "" preserved! */
1529 /* This performs word splitting and quoted null character removal on
1531 #define issep(c) (member ((c), separators))
1534 list_string (string
, separators
, quoted
)
1535 register char *string
, *separators
;
1540 char *current_word
, *s
;
1541 int sindex
, sh_style_split
, whitesep
;
1543 if (!string
|| !*string
)
1544 return ((WORD_LIST
*)NULL
);
1547 separators
&& *separators
&& (STREQ (separators
, " \t\n"));
1549 /* Remove sequences of whitespace at the beginning of STRING, as
1550 long as those characters appear in IFS. Do not do this if
1551 STRING is quoted or if there are no separator characters. */
1552 if (!quoted
|| !separators
|| !*separators
)
1554 for (s
= string
; *s
&& spctabnl (*s
) && issep (*s
); s
++);
1557 return ((WORD_LIST
*)NULL
);
1562 /* OK, now STRING points to a word that does not begin with white space.
1563 The splitting algorithm is:
1564 extract a word, stopping at a separator
1565 skip sequences of spc, tab, or nl as long as they are separators
1566 This obeys the field splitting rules in Posix.2. */
1567 for (result
= (WORD_LIST
*)NULL
, sindex
= 0; string
[sindex
]; )
1569 current_word
= string_extract_verbatim (string
, &sindex
, separators
);
1570 if (current_word
== 0)
1573 /* If we have a quoted empty string, add a quoted null argument. We
1574 want to preserve the quoted null character iff this is a quoted
1575 empty string; otherwise the quoted null characters are removed
1577 if (QUOTED_NULL (current_word
))
1579 t
= make_bare_word ("");
1580 t
->flags
|= W_QUOTED
;
1582 t
->word
= make_quoted_char ('\0');
1583 result
= make_word_list (t
, result
);
1585 else if (current_word
[0] != '\0')
1587 /* If we have something, then add it regardless. However,
1588 perform quoted null character removal on the current word. */
1589 remove_quoted_nulls (current_word
);
1590 result
= add_string_to_list (current_word
, result
);
1591 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
1592 result
->word
->flags
|= W_QUOTED
;
1595 /* If we're not doing sequences of separators in the traditional
1596 Bourne shell style, then add a quoted null argument. */
1597 else if (!sh_style_split
&& !spctabnl (string
[sindex
]))
1599 t
= make_bare_word ("");
1600 t
->flags
|= W_QUOTED
;
1602 t
->word
= make_quoted_char ('\0');
1603 result
= make_word_list (t
, result
);
1606 free (current_word
);
1608 /* Note whether or not the separator is IFS whitespace, used later. */
1609 whitesep
= string
[sindex
] && spctabnl (string
[sindex
]);
1611 /* Move past the current separator character. */
1615 /* Now skip sequences of space, tab, or newline characters if they are
1616 in the list of separators. */
1617 while (string
[sindex
] && spctabnl (string
[sindex
]) && issep (string
[sindex
]))
1620 /* If the first separator was IFS whitespace and the current character is
1621 a non-whitespace IFS character, it should be part of the current field
1622 delimiter, not a separate delimiter that would result in an empty field.
1623 Look at POSIX.2, 3.6.5, (3)(b). */
1624 if (string
[sindex
] && whitesep
&& issep (string
[sindex
]) && !spctabnl (string
[sindex
]))
1627 return (REVERSE_LIST (result
, WORD_LIST
*));
1630 /* Parse a single word from STRING, using SEPARATORS to separate fields.
1631 ENDPTR is set to the first character after the word. This is used by
1633 XXX - this function is very similar to list_string; they should be
1636 get_word_from_string (stringp
, separators
, endptr
)
1637 char **stringp
, *separators
, **endptr
;
1641 int sindex
, sh_style_split
, whitesep
;
1643 if (!stringp
|| !*stringp
|| !**stringp
)
1644 return ((char *)NULL
);
1649 separators
&& *separators
&& (STREQ (separators
, " \t\n"));
1651 /* Remove sequences of whitespace at the beginning of STRING, as
1652 long as those characters appear in IFS. */
1653 if (sh_style_split
|| !separators
|| !*separators
)
1655 for (; *s
&& spctabnl (*s
) && issep (*s
); s
++);
1657 /* If the string is nothing but whitespace, update it and return. */
1663 return ((char *)NULL
);
1667 /* OK, S points to a word that does not begin with white space.
1668 Now extract a word, stopping at a separator, save a pointer to
1669 the first character after the word, then skip sequences of spc,
1670 tab, or nl as long as they are separators.
1672 This obeys the field splitting rules in Posix.2. */
1674 current_word
= string_extract_verbatim (s
, &sindex
, separators
);
1676 /* Set ENDPTR to the first character after the end of the word. */
1678 *endptr
= s
+ sindex
;
1680 /* Note whether or not the separator is IFS whitespace, used later. */
1681 whitesep
= s
[sindex
] && spctabnl (s
[sindex
]);
1683 /* Move past the current separator character. */
1687 /* Now skip sequences of space, tab, or newline characters if they are
1688 in the list of separators. */
1689 while (s
[sindex
] && spctabnl (s
[sindex
]) && issep (s
[sindex
]))
1692 /* If the first separator was IFS whitespace and the current character is
1693 a non-whitespace IFS character, it should be part of the current field
1694 delimiter, not a separate delimiter that would result in an empty field.
1695 Look at POSIX.2, 3.6.5, (3)(b). */
1696 if (s
[sindex
] && whitesep
&& issep (s
[sindex
]) && !spctabnl (s
[sindex
]))
1699 /* Update STRING to point to the next field. */
1700 *stringp
= s
+ sindex
;
1701 return (current_word
);
1704 /* Remove IFS white space at the end of STRING. Start at the end
1705 of the string and walk backwards until the beginning of the string
1706 or we find a character that's not IFS white space and not CTLESC.
1707 Only let CTLESC escape a white space character if SAW_ESCAPE is
1710 strip_trailing_ifs_whitespace (string
, separators
, saw_escape
)
1711 char *string
, *separators
;
1716 s
= string
+ STRLEN (string
) - 1;
1717 while (s
> string
&& ((spctabnl (*s
) && issep (*s
)) ||
1718 (saw_escape
&& *s
== CTLESC
&& spctabnl (s
[1]))))
1726 /* Split STRING into words at whitespace. Obeys shell-style quoting with
1727 backslashes, single and double quotes. */
1729 list_string_with_quotes (string
)
1734 int c
, i
, tokstart
, len
;
1736 for (s
= string
; s
&& *s
&& spctabnl (*s
); s
++)
1738 if (s
== 0 || *s
== 0)
1739 return ((WORD_LIST
*)NULL
);
1742 list
= (WORD_LIST
*)NULL
;
1753 i
= skip_single_quoted (s
, ++i
);
1755 i
= skip_double_quoted (s
, ++i
);
1756 else if (c
== 0 || spctabnl (c
))
1758 /* We have found the end of a token. Make a word out of it and
1759 add it to the word list. */
1760 token
= substring (s
, tokstart
, i
);
1761 list
= add_string_to_list (token
, list
);
1763 while (spctabnl (s
[i
]))
1771 i
++; /* normal character */
1773 return (REVERSE_LIST (list
, WORD_LIST
*));
1777 /********************************************************/
1779 /* Functions to perform assignment statements */
1781 /********************************************************/
1783 /* Given STRING, an assignment string, get the value of the right side
1784 of the `=', and bind it to the left side. If EXPAND is true, then
1785 perform parameter expansion, command substitution, and arithmetic
1786 expansion on the right-hand side. Perform tilde expansion in any
1787 case. Do not perform word splitting on the result of expansion. */
1789 do_assignment_internal (string
, expand
)
1796 #if defined (ARRAY_VARS)
1798 int ni
, assign_list
= 0;
1801 offset
= assignment (string
);
1802 name
= savestring (string
);
1803 value
= (char *)NULL
;
1805 if (name
[offset
] == '=')
1810 temp
= name
+ offset
+ 1;
1812 #if defined (ARRAY_VARS)
1813 if (expand
&& temp
[0] == LPAREN
&& strchr (temp
, RPAREN
))
1815 assign_list
= ni
= 1;
1816 value
= extract_delimited_string (temp
, &ni
, "(", (char *)NULL
, ")");
1821 /* Perform tilde expansion. */
1822 if (expand
&& temp
[0])
1824 temp
= (strchr (temp
, '~') && unquoted_member ('~', temp
))
1825 ? bash_tilde_expand (temp
)
1826 : savestring (temp
);
1828 value
= expand_string_if_necessary (temp
, 0, expand_string_unsplit
);
1832 value
= savestring (temp
);
1837 value
= (char *)xmalloc (1);
1841 if (echo_command_at_execute
)
1843 #if defined (ARRAY_VARS)
1845 fprintf (stderr
, "%s%s=(%s)\n", indirection_level_string (), name
, value
);
1848 fprintf (stderr
, "%s%s=%s\n", indirection_level_string (), name
, value
);
1851 #define ASSIGN_RETURN(r) do { FREE (value); free (name); return (r); } while (0)
1853 #if defined (ARRAY_VARS)
1854 if (t
= strchr (name
, '[')) /*]*/
1858 report_error ("%s: cannot assign list to array member", name
);
1861 entry
= assign_array_element (name
, value
);
1865 else if (assign_list
)
1866 entry
= assign_array_from_string (name
, value
);
1868 #endif /* ARRAY_VARS */
1869 entry
= bind_variable (name
, value
);
1871 stupidly_hack_special_variables (name
);
1874 VUNSETATTR (entry
, att_invisible
);
1876 /* Return 1 if the assignment seems to have been performed correctly. */
1877 ASSIGN_RETURN (entry
? ((readonly_p (entry
) == 0) && noassign_p (entry
) == 0) : 0);
1880 /* Perform the assignment statement in STRING, and expand the
1881 right side by doing command and parameter expansion. */
1883 do_assignment (string
)
1886 return do_assignment_internal (string
, 1);
1889 /* Given STRING, an assignment string, get the value of the right side
1890 of the `=', and bind it to the left side. Do not do command and
1891 parameter substitution on the right hand side. */
1893 do_assignment_no_expand (string
)
1896 return do_assignment_internal (string
, 0);
1899 /***************************************************
1901 * Functions to manage the positional parameters *
1903 ***************************************************/
1905 /* Return the word list that corresponds to `$*'. */
1907 list_rest_of_args ()
1909 register WORD_LIST
*list
, *args
;
1912 /* Break out of the loop as soon as one of the dollar variables is null. */
1913 for (i
= 1, list
= (WORD_LIST
*)NULL
; i
< 10 && dollar_vars
[i
]; i
++)
1914 list
= make_word_list (make_bare_word (dollar_vars
[i
]), list
);
1916 for (args
= rest_of_args
; args
; args
= args
->next
)
1917 list
= make_word_list (make_bare_word (args
->word
->word
), list
);
1919 return (REVERSE_LIST (list
, WORD_LIST
*));
1925 register WORD_LIST
*list
;
1928 for (n
= 0; n
< 9 && dollar_vars
[n
+1]; n
++)
1930 for (list
= rest_of_args
; list
; list
= list
->next
)
1935 /* Return the value of a positional parameter. This handles values > 10. */
1937 get_dollar_var_value (ind
)
1944 temp
= dollar_vars
[ind
] ? savestring (dollar_vars
[ind
]) : (char *)NULL
;
1945 else /* We want something like ${11} */
1948 for (p
= rest_of_args
; p
&& ind
--; p
= p
->next
)
1950 temp
= p
? savestring (p
->word
->word
) : (char *)NULL
;
1955 /* Make a single large string out of the dollar digit variables,
1956 and the rest_of_args. If DOLLAR_STAR is 1, then obey the special
1957 case of "$*" with respect to IFS. */
1959 string_rest_of_args (dollar_star
)
1962 register WORD_LIST
*list
;
1965 list
= list_rest_of_args ();
1966 string
= dollar_star
? string_list_dollar_star (list
) : string_list (list
);
1967 dispose_words (list
);
1971 /* Return a string containing the positional parameters from START to
1972 END, inclusive. If STRING[0] == '*', we obey the rules for $*,
1973 which only makes a difference if QUOTED is non-zero. */
1975 pos_params (string
, start
, end
, quoted
)
1977 int start
, end
, quoted
;
1979 WORD_LIST
*save
, *params
, *h
, *t
;
1983 /* see if we can short-circuit. if start == end, we want 0 parameters. */
1985 return ((char *)NULL
);
1987 save
= params
= list_rest_of_args ();
1989 return ((char *)NULL
);
1991 for (i
= 1; params
&& i
< start
; i
++)
1992 params
= params
->next
;
1994 return ((char *)NULL
);
1995 for (h
= t
= params
; params
&& i
< end
; i
++)
1998 params
= params
->next
;
2001 t
->next
= (WORD_LIST
*)NULL
;
2002 if (string
[0] == '*')
2003 ret
= (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) ? string_list_dollar_star (h
) : string_list (h
);
2005 ret
= string_list ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) ? quote_list (h
) : h
);
2009 dispose_words (save
);
2013 /******************************************************************/
2015 /* Functions to expand strings to strings or WORD_LISTs */
2017 /******************************************************************/
2019 #if defined (PROCESS_SUBSTITUTION)
2020 #define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC)
2022 #define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC)
2025 /* If there are any characters in STRING that require full expansion,
2026 then call FUNC to expand STRING; otherwise just perform quote
2027 removal if necessary. This returns a new string. */
2029 expand_string_if_necessary (string
, quoted
, func
)
2038 for (i
= saw_quote
= 0; string
[i
]; i
++)
2040 if (EXP_CHAR (string
[i
]))
2042 else if (string
[i
] == '\'' || string
[i
] == '\\' || string
[i
] == '"')
2048 list
= (*func
) (string
, quoted
);
2051 ret
= string_list (list
);
2052 dispose_words (list
);
2057 else if (saw_quote
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
2058 ret
= string_quote_removal (string
, quoted
);
2060 ret
= savestring (string
);
2064 static inline char *
2065 expand_string_to_string_internal (string
, quoted
, func
)
2073 if (string
== 0 || *string
== '\0')
2074 return ((char *)NULL
);
2076 list
= (*func
) (string
, quoted
);
2079 ret
= string_list (list
);
2080 dispose_words (list
);
2089 expand_string_to_string (string
, quoted
)
2093 return (expand_string_to_string_internal (string
, quoted
, expand_string
));
2097 expand_string_unsplit_to_string (string
, quoted
)
2101 return (expand_string_to_string_internal (string
, quoted
, expand_string_unsplit
));
2104 #if defined (COND_COMMAND)
2105 /* Just remove backslashes in STRING. Returns a new string. */
2107 remove_backslashes (string
)
2112 r
= ret
= (char *)xmalloc (strlen (string
) + 1);
2113 for (s
= string
; s
&& *s
; )
2125 /* This needs better error handling. */
2126 /* Expand W for use as an argument to a unary or binary operator in a
2127 [[...]] expression. If SPECIAL is nonzero, this is the rhs argument
2128 to the != or == operator, and should be treated as a pattern. In
2129 this case, we quote the string specially for the globbing code. The
2130 caller is responsible for removing the backslashes if the unquoted
2131 words is needed later. */
2133 cond_expand_word (w
, special
)
2140 if (w
->word
== 0 || w
->word
[0] == '\0')
2141 return ((char *)NULL
);
2143 if (strchr (w
->word
, '~') && unquoted_member ('~', w
->word
))
2145 p
= bash_tilde_expand (w
->word
);
2150 l
= call_expand_word_internal (w
, 0, 0, (int *)0, (int *)0);
2156 r
= string_list (l
);
2160 p
= string_list (l
);
2161 r
= quote_string_for_globbing (p
, QGLOB_CVTNULL
);
2173 /* Call expand_word_internal to expand W and handle error returns.
2174 A convenience function for functions that don't want to handle
2175 any errors or free any memory before aborting. */
2177 call_expand_word_internal (w
, q
, i
, c
, e
)
2183 result
= expand_word_internal (w
, q
, i
, c
, e
);
2184 if (result
== &expand_word_error
|| result
== &expand_word_fatal
)
2186 expand_no_split_dollar_star
= 0; /* XXX */
2187 /* By convention, each time this error is returned, w->word has
2188 already been freed (it sometimes may not be in the fatal case,
2189 but that doesn't result in a memory leak because we're going
2190 to exit in most cases). */
2191 w
->word
= (char *)NULL
;
2192 last_command_exit_value
= EXECUTION_FAILURE
;
2193 jump_to_top_level ((result
== &expand_word_error
) ? DISCARD
: FORCE_EOF
);
2200 /* Perform parameter expansion, command substitution, and arithmetic
2201 expansion on STRING, as if it were a word. Leave the result quoted. */
2203 expand_string_internal (string
, quoted
)
2210 if (string
== 0 || *string
== 0)
2211 return ((WORD_LIST
*)NULL
);
2214 td
.word
= savestring (string
);
2216 tresult
= call_expand_word_internal (&td
, quoted
, 0, (int *)NULL
, (int *)NULL
);
2222 /* Expand STRING by performing parameter expansion, command substitution,
2223 and arithmetic expansion. Dequote the resulting WORD_LIST before
2224 returning it, but do not perform word splitting. The call to
2225 remove_quoted_nulls () is in here because word splitting normally
2226 takes care of quote removal. */
2228 expand_string_unsplit (string
, quoted
)
2234 if (string
== 0 || *string
== '\0')
2235 return ((WORD_LIST
*)NULL
);
2237 expand_no_split_dollar_star
= 1;
2238 value
= expand_string_internal (string
, quoted
);
2239 expand_no_split_dollar_star
= 0;
2244 remove_quoted_nulls (value
->word
->word
);
2245 dequote_list (value
);
2251 /* Expand one of the PS? prompt strings. This is a sort of combination of
2252 expand_string_unsplit and expand_string_internal, but returns the
2253 passed string when an error occurs. Might want to trap other calls
2254 to jump_to_top_level here so we don't endlessly loop. */
2256 expand_prompt_string (string
, quoted
)
2263 if (string
== 0 || *string
== 0)
2264 return ((WORD_LIST
*)NULL
);
2267 td
.word
= savestring (string
);
2269 no_longjmp_on_fatal_error
= 1;
2270 value
= expand_word_internal (&td
, quoted
, 0, (int *)NULL
, (int *)NULL
);
2271 no_longjmp_on_fatal_error
= 0;
2273 if (value
== &expand_word_error
|| value
== &expand_word_fatal
)
2275 value
= make_word_list (make_bare_word (string
), (WORD_LIST
*)NULL
);
2282 remove_quoted_nulls (value
->word
->word
);
2283 dequote_list (value
);
2288 /* Expand STRING just as if you were expanding a word, but do not dequote
2289 the resultant WORD_LIST. This is called only from within this file,
2290 and is used to correctly preserve quoted characters when expanding
2291 things like ${1+"$@"}. This does parameter expansion, command
2292 substitution, arithmetic expansion, and word splitting. */
2294 expand_string_leave_quoted (string
, quoted
)
2301 if (string
== 0 || *string
== '\0')
2302 return ((WORD_LIST
*)NULL
);
2304 tlist
= expand_string_internal (string
, quoted
);
2308 tresult
= word_list_split (tlist
);
2309 dispose_words (tlist
);
2312 return ((WORD_LIST
*)NULL
);
2315 /* This does not perform word splitting or dequote the WORD_LIST
2318 expand_string_for_rhs (string
, quoted
, dollar_at_p
, has_dollar_at
)
2320 int quoted
, *dollar_at_p
, *has_dollar_at
;
2325 if (string
== 0 || *string
== '\0')
2326 return (WORD_LIST
*)NULL
;
2330 tresult
= call_expand_word_internal (&td
, quoted
, 1, dollar_at_p
, has_dollar_at
);
2334 /* Expand STRING just as if you were expanding a word. This also returns
2335 a list of words. Note that filename globbing is *NOT* done for word
2336 or string expansion, just when the shell is expanding a command. This
2337 does parameter expansion, command substitution, arithmetic expansion,
2338 and word splitting. Dequote the resultant WORD_LIST before returning. */
2340 expand_string (string
, quoted
)
2346 if (string
== 0 || *string
== '\0')
2347 return ((WORD_LIST
*)NULL
);
2349 result
= expand_string_leave_quoted (string
, quoted
);
2350 return (result
? dequote_list (result
) : result
);
2353 /***************************************************
2355 * Functions to handle quoting chars *
2357 ***************************************************/
2361 A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string.
2362 The parser passes CTLNUL as CTLESC CTLNUL. */
2364 /* The parser passes us CTLESC as CTLESC CTLESC and CTLNUL as CTLESC CTLNUL.
2365 This is necessary to make unquoted CTLESC and CTLNUL characters in the
2366 data stream pass through properly.
2367 Here we remove doubled CTLESC characters inside quoted strings before
2368 quoting the entire string, so we do not double the number of CTLESC
2371 remove_quoted_escapes (string
)
2381 t1
= t
= (char *)xmalloc (strlen (string
) + 1);
2382 for (docopy
= 0, s
= string
; *s
; s
++, t1
++)
2384 if (*s
== CTLESC
&& (s
[1] == CTLESC
|| s
[1] == CTLNUL
))
2398 /* Quote escape characters in string s, but no other characters. This is
2399 used to protect CTLESC and CTLNUL in variable values from the rest of
2400 the word expansion process after the variable is expanded. */
2402 quote_escapes (string
)
2405 register char *s
, *t
;
2408 result
= (char *)xmalloc ((strlen (string
) * 2) + 1);
2409 for (s
= string
, t
= result
; *s
; )
2411 if (*s
== CTLESC
|| *s
== CTLNUL
)
2420 list_quote_escapes (list
)
2423 register WORD_LIST
*w
;
2426 for (w
= list
; w
; w
= w
->next
)
2429 w
->word
->word
= quote_escapes (t
);
2438 dequote_escapes (string
)
2441 register char *s
, *t
;
2444 result
= (char *)xmalloc (strlen (string
) + 1);
2445 for (s
= string
, t
= result
; *s
; )
2447 if (*s
== CTLESC
&& (s
[1] == CTLESC
|| s
[1] == CTLNUL
))
2465 register WORD_LIST
*tlist
;
2467 for (tlist
= list
; tlist
; tlist
= tlist
->next
)
2469 s
= dequote_string (tlist
->word
->word
);
2470 free (tlist
->word
->word
);
2471 tlist
->word
->word
= s
;
2476 /* Return a new string with the quoted representation of character C. */
2478 make_quoted_char (c
)
2483 temp
= (char *)xmalloc (3);
2498 /* Quote STRING. Return a new string. */
2500 quote_string (string
)
2508 result
= (char *)xmalloc (2);
2514 result
= (char *)xmalloc ((strlen (string
) * 2) + 1);
2516 for (t
= result
; *string
; )
2526 /* De-quoted quoted characters in STRING. */
2528 dequote_string (string
)
2534 result
= (char *)xmalloc (strlen (string
) + 1);
2536 if (QUOTED_NULL (string
))
2542 /* If no character in the string can be quoted, don't bother examining
2543 each character. Just return a copy of the string passed to us. */
2544 if (strchr (string
, CTLESC
) == NULL
) /* XXX */
2546 strcpy (result
, string
); /* XXX */
2547 return (result
); /* XXX */
2550 for (t
= result
; *string
; string
++, t
++)
2552 if (*string
== CTLESC
)
2567 /* Quote the entire WORD_LIST list. */
2572 register WORD_LIST
*w
;
2575 for (w
= list
; w
; w
= w
->next
)
2578 w
->word
->word
= quote_string (t
);
2580 w
->word
->flags
|= W_QUOTED
;
2585 /* Perform quoted null character removal on STRING. We don't allow any
2586 quoted null characters in the middle or at the ends of strings because
2587 of how expand_word_internal works. remove_quoted_nulls () turns
2588 STRING into an empty string iff it only consists of a quoted null,
2589 and removes all unquoted CTLNUL characters. */
2591 #define remove_quoted_nulls(string) \
2592 do { if (QUOTED_NULL (string)) string[0] ='\0'; } while (0)
2595 remove_quoted_nulls (string
)
2600 nstr
= savestring (string
);
2602 for (p
= nstr
, s
= string
; *s
; s
++)
2606 *p
++ = *s
++; /* CTLESC */
2609 *p
++ = *s
; /* quoted char */
2617 strcpy (string
, nstr
);
2621 /* Perform quoted null character removal on each element of LIST.
2622 This modifies LIST. */
2624 word_list_remove_quoted_nulls (list
)
2627 register WORD_LIST
*t
;
2629 for (t
= list
; t
; t
= t
->next
)
2630 remove_quoted_nulls (t
->word
->word
);
2633 /* **************************************************************** */
2635 /* Functions for Matching and Removing Patterns */
2637 /* **************************************************************** */
2639 /* Remove the portion of PARAM matched by PATTERN according to OP, where OP
2640 can have one of 4 values:
2641 RP_LONG_LEFT remove longest matching portion at start of PARAM
2642 RP_SHORT_LEFT remove shortest matching portion at start of PARAM
2643 RP_LONG_RIGHT remove longest matching portion at end of PARAM
2644 RP_SHORT_RIGHT remove shortest matching portion at end of PARAM
2647 #define RP_LONG_LEFT 1
2648 #define RP_SHORT_LEFT 2
2649 #define RP_LONG_RIGHT 3
2650 #define RP_SHORT_RIGHT 4
2653 remove_pattern (param
, pattern
, op
)
2654 char *param
, *pattern
;
2659 register char *p
, *ret
, c
;
2661 if (param
== NULL
|| *param
== '\0')
2663 if (pattern
== NULL
|| *pattern
== '\0') /* minor optimization */
2664 return (savestring (param
));
2666 len
= STRLEN (param
);
2671 case RP_LONG_LEFT
: /* remove longest match at start */
2672 for (p
= end
; p
>= param
; p
--)
2675 if (strmatch (pattern
, param
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
2678 return (savestring (p
));
2684 case RP_SHORT_LEFT
: /* remove shortest match at start */
2685 for (p
= param
; p
<= end
; p
++)
2688 if (strmatch (pattern
, param
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
2691 return (savestring (p
));
2697 case RP_LONG_RIGHT
: /* remove longest match at end */
2698 for (p
= param
; p
<= end
; p
++)
2700 if (strmatch (pattern
, p
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
2703 ret
= savestring (param
);
2710 case RP_SHORT_RIGHT
: /* remove shortest match at end */
2711 for (p
= end
; p
>= param
; p
--)
2713 if (strmatch (pattern
, p
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
2716 ret
= savestring (param
);
2723 return (savestring (param
)); /* no match, return original string */
2726 /* Return 1 of the first character of STRING could match the first
2727 character of pattern PAT. Used to avoid n2 calls to strmatch(). */
2729 match_pattern_char (pat
, string
)
2740 return (*string
== c
);
2742 return (*string
== *pat
);
2744 return (*pat
== LPAREN
? 1 : (*string
!= '\0'));
2750 return (*pat
== LPAREN
? 1 : (*string
== c
));
2752 return (*string
!= '\0');
2756 /* Match PAT anywhere in STRING and return the match boundaries.
2757 This returns 1 in case of a successful match, 0 otherwise. SP
2758 and EP are pointers into the string where the match begins and
2759 ends, respectively. MTYPE controls what kind of match is attempted.
2760 MATCH_BEG and MATCH_END anchor the match at the beginning and end
2761 of the string, respectively. The longest match is returned. */
2763 match_pattern (string
, pat
, mtype
, sp
, ep
)
2769 register char *p
, *p1
;
2772 if (string
== 0 || *string
== 0 || pat
== 0 || *pat
== 0)
2775 end
= string
+ STRLEN (string
);
2780 for (p
= string
; p
<= end
; p
++)
2782 if (match_pattern_char (pat
, p
))
2784 for (p1
= end
; p1
>= p
; p1
--)
2786 c
= *p1
; *p1
= '\0';
2787 if (strmatch (pat
, p
, FNMATCH_EXTFLAG
) == 0)
2801 if (match_pattern_char (pat
, string
) == 0)
2803 for (p
= end
; p
>= string
; p
--)
2806 if (strmatch (pat
, string
, FNMATCH_EXTFLAG
) == 0)
2818 for (p
= string
; p
<= end
; p
++)
2819 if (strmatch (pat
, p
, FNMATCH_EXTFLAG
) == 0)
2832 getpatspec (c
, value
)
2837 return ((*value
== '#') ? RP_LONG_LEFT
: RP_SHORT_LEFT
);
2839 return ((*value
== '%') ? RP_LONG_RIGHT
: RP_SHORT_RIGHT
);
2842 /* Posix.2 says that the WORD should be run through tilde expansion,
2843 parameter expansion, command substitution and arithmetic expansion.
2844 This leaves the result quoted, so quote_string_for_globbing () has
2845 to be called to fix it up for strmatch (). If QUOTED is non-zero,
2846 it means that the entire expression was enclosed in double quotes.
2847 This means that quoting characters in the pattern do not make any
2848 special pattern characters quoted. For example, the `*' in the
2849 following retains its special meaning: "${foo#'*'}". */
2851 getpattern (value
, quoted
, expandpat
)
2853 int quoted
, expandpat
;
2859 tword
= strchr (value
, '~') ? bash_tilde_expand (value
) : savestring (value
);
2861 /* expand_string_internal () leaves WORD quoted and does not perform
2863 if (expandpat
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && *tword
)
2866 pat
= string_extract_double_quoted (tword
, &i
, 1);
2871 /* There is a problem here: how to handle single or double quotes in the
2872 pattern string when the whole expression is between double quotes? */
2874 l
= *tword
? expand_string_for_rhs (tword
, quoted
, (int *)NULL
, (int *)NULL
)
2876 l
= *tword
? expand_string_for_rhs (tword
,
2877 (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) ? Q_NOQUOTE
: quoted
,
2878 (int *)NULL
, (int *)NULL
)
2882 pat
= string_list (l
);
2886 tword
= quote_string_for_globbing (pat
, QGLOB_CVTNULL
);
2893 /* Handle removing a pattern from a string as a result of ${name%[%]value}
2894 or ${name#[#]value}. */
2896 parameter_brace_remove_pattern (value
, temp
, c
, quoted
)
2901 char *pattern
, *tword
;
2903 patspec
= getpatspec (c
, value
);
2904 if (patspec
== RP_LONG_LEFT
|| patspec
== RP_LONG_RIGHT
)
2907 pattern
= getpattern (value
, quoted
, 1);
2909 tword
= remove_pattern (temp
, pattern
, patspec
);
2916 list_remove_pattern (list
, pattern
, patspec
, type
, quoted
)
2919 int patspec
, type
, quoted
;
2925 for (new = (WORD_LIST
*)NULL
, l
= list
; l
; l
= l
->next
)
2927 tword
= remove_pattern (l
->word
->word
, pattern
, patspec
);
2928 w
= make_bare_word (tword
);
2930 new = make_word_list (w
, new);
2933 l
= REVERSE_LIST (new, WORD_LIST
*);
2935 tword
= (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) ? string_list_dollar_star (l
) : string_list (l
);
2937 tword
= string_list ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) ? quote_list (l
) : l
);
2944 parameter_list_remove_pattern (value
, type
, c
, quoted
)
2946 int type
, c
, quoted
;
2949 char *pattern
, *ret
;
2952 patspec
= getpatspec (c
, value
);
2953 if (patspec
== RP_LONG_LEFT
|| patspec
== RP_LONG_RIGHT
)
2956 pattern
= getpattern (value
, quoted
, 1);
2958 list
= list_rest_of_args ();
2959 ret
= list_remove_pattern (list
, pattern
, patspec
, type
, quoted
);
2960 dispose_words (list
);
2965 #if defined (ARRAY_VARS)
2967 array_remove_pattern (value
, aspec
, aval
, c
, quoted
)
2968 char *value
, *aspec
, *aval
; /* AVAL == evaluated ASPEC */
2973 char *ret
, *t
, *pattern
;
2976 var
= array_variable_part (aspec
, &t
, &len
);
2978 return ((char *)NULL
);
2980 patspec
= getpatspec (c
, value
);
2981 if (patspec
== RP_LONG_LEFT
|| patspec
== RP_LONG_RIGHT
)
2984 pattern
= getpattern (value
, quoted
, 1);
2986 if (ALL_ELEMENT_SUB (t
[0]) && t
[1] == ']')
2988 if (array_p (var
) == 0)
2990 report_error ("%s: bad array subscript", aspec
);
2992 return ((char *)NULL
);
2994 l
= array_to_word_list (array_cell (var
));
2996 return ((char *)NULL
);
2997 ret
= list_remove_pattern (l
, pattern
, patspec
, t
[0], quoted
);
3002 ret
= remove_pattern (aval
, pattern
, patspec
);
3005 t
= quote_escapes (ret
);
3014 #endif /* ARRAY_VARS */
3016 /*******************************************
3018 * Functions to expand WORD_DESCs *
3020 *******************************************/
3022 /* Expand WORD, performing word splitting on the result. This does
3023 parameter expansion, command substitution, arithmetic expansion,
3024 word splitting, and quote removal. */
3027 expand_word (word
, quoted
)
3031 WORD_LIST
*result
, *tresult
;
3033 tresult
= call_expand_word_internal (word
, quoted
, 0, (int *)NULL
, (int *)NULL
);
3034 result
= word_list_split (tresult
);
3035 dispose_words (tresult
);
3036 return (result
? dequote_list (result
) : result
);
3039 /* Expand WORD, but do not perform word splitting on the result. This
3040 does parameter expansion, command substitution, arithmetic expansion,
3041 and quote removal. */
3043 expand_word_unsplit (word
, quoted
)
3049 expand_no_split_dollar_star
= 1;
3050 result
= call_expand_word_internal (word
, quoted
, 0, (int *)NULL
, (int *)NULL
);
3051 expand_no_split_dollar_star
= 0;
3053 return (result
? dequote_list (result
) : result
);
3056 /* Perform shell expansions on WORD, but do not perform word splitting or
3057 quote removal on the result. */
3059 expand_word_leave_quoted (word
, quoted
)
3063 return (call_expand_word_internal (word
, quoted
, 0, (int *)NULL
, (int *)NULL
));
3066 #if defined (PROCESS_SUBSTITUTION)
3068 /*****************************************************************/
3070 /* Hacking Process Substitution */
3072 /*****************************************************************/
3074 #if !defined (HAVE_DEV_FD)
3075 /* Named pipes must be removed explicitly with `unlink'. This keeps a list
3076 of FIFOs the shell has open. unlink_fifo_list will walk the list and
3077 unlink all of them. add_fifo_list adds the name of an open FIFO to the
3078 list. NFIFO is a count of the number of FIFOs in the list. */
3079 #define FIFO_INCR 20
3086 static struct temp_fifo
*fifo_list
= (struct temp_fifo
*)NULL
;
3088 static int fifo_list_size
;
3091 add_fifo_list (pathname
)
3094 if (nfifo
>= fifo_list_size
- 1)
3096 fifo_list_size
+= FIFO_INCR
;
3097 fifo_list
= (struct temp_fifo
*)xrealloc (fifo_list
,
3098 fifo_list_size
* sizeof (struct temp_fifo
));
3101 fifo_list
[nfifo
].file
= savestring (pathname
);
3113 for (i
= saved
= 0; i
< nfifo
; i
++)
3115 if ((fifo_list
[i
].proc
== -1) || (kill(fifo_list
[i
].proc
, 0) == -1))
3117 unlink (fifo_list
[i
].file
);
3118 free (fifo_list
[i
].file
);
3119 fifo_list
[i
].file
= (char *)NULL
;
3120 fifo_list
[i
].proc
= -1;
3126 /* If we didn't remove some of the FIFOs, compact the list. */
3129 for (i
= j
= 0; i
< nfifo
; i
++)
3130 if (fifo_list
[i
].file
)
3132 fifo_list
[j
].file
= fifo_list
[i
].file
;
3133 fifo_list
[j
].proc
= fifo_list
[i
].proc
;
3147 tname
= sh_mktmpname ("sh-np", MT_USERANDOM
);
3148 if (mkfifo (tname
, 0600) < 0)
3151 return ((char *)NULL
);
3154 add_fifo_list (tname
);
3158 #else /* HAVE_DEV_FD */
3160 /* DEV_FD_LIST is a bitmap of file descriptors attached to pipes the shell
3161 has open to children. NFDS is a count of the number of bits currently
3162 set in DEV_FD_LIST. TOTFDS is a count of the highest possible number
3164 static char *dev_fd_list
= (char *)NULL
;
3166 static int totfds
; /* The highest possible number of open files. */
3172 if (!dev_fd_list
|| fd
>= totfds
)
3177 totfds
= getdtablesize ();
3178 if (totfds
< 0 || totfds
> 256)
3183 dev_fd_list
= (char *)xrealloc (dev_fd_list
, totfds
);
3184 bzero (dev_fd_list
+ ofds
, totfds
- ofds
);
3187 dev_fd_list
[fd
] = 1;
3199 for (i
= 0; nfds
&& i
< totfds
; i
++)
3210 #if defined (NOTDEF)
3211 print_dev_fd_list ()
3215 fprintf (stderr
, "pid %ld: dev_fd_list:", (long)getpid ());
3218 for (i
= 0; i
< totfds
; i
++)
3221 fprintf (stderr
, " %d", i
);
3223 fprintf (stderr
, "\n");
3228 make_dev_fd_filename (fd
)
3231 char *ret
, intbuf
[INT_STRLEN_BOUND (int) + 1], *p
;
3233 ret
= (char *)xmalloc (sizeof (DEV_FD_PREFIX
) + 4);
3235 strcpy (ret
, DEV_FD_PREFIX
);
3236 p
= inttostr (fd
, intbuf
, sizeof (intbuf
));
3237 strcpy (ret
+ sizeof (DEV_FD_PREFIX
) - 1, p
);
3243 #endif /* HAVE_DEV_FD */
3245 /* Return a filename that will open a connection to the process defined by
3246 executing STRING. HAVE_DEV_FD, if defined, means open a pipe and return
3247 a filename in /dev/fd corresponding to a descriptor that is one of the
3248 ends of the pipe. If not defined, we use named pipes on systems that have
3249 them. Systems without /dev/fd and named pipes are out of luck.
3251 OPEN_FOR_READ_IN_CHILD, if 1, means open the named pipe for reading or
3252 use the read end of the pipe and dup that file descriptor to fd 0 in
3253 the child. If OPEN_FOR_READ_IN_CHILD is 0, we open the named pipe for
3254 writing or use the write end of the pipe in the child, and dup that
3255 file descriptor to fd 1 in the child. The parent does the opposite. */
3258 process_substitute (string
, open_for_read_in_child
)
3260 int open_for_read_in_child
;
3265 #if defined (HAVE_DEV_FD)
3266 int parent_pipe_fd
, child_pipe_fd
;
3268 #endif /* HAVE_DEV_FD */
3269 #if defined (JOB_CONTROL)
3270 pid_t old_pipeline_pgrp
;
3273 if (!string
|| !*string
|| wordexp_only
)
3274 return ((char *)NULL
);
3276 #if !defined (HAVE_DEV_FD)
3277 pathname
= make_named_pipe ();
3278 #else /* HAVE_DEV_FD */
3279 if (pipe (fildes
) < 0)
3281 sys_error ("cannot make pipe for process substitution");
3282 return ((char *)NULL
);
3284 /* If OPEN_FOR_READ_IN_CHILD == 1, we want to use the write end of
3285 the pipe in the parent, otherwise the read end. */
3286 parent_pipe_fd
= fildes
[open_for_read_in_child
];
3287 child_pipe_fd
= fildes
[1 - open_for_read_in_child
];
3288 /* Move the parent end of the pipe to some high file descriptor, to
3289 avoid clashes with FDs used by the script. */
3290 parent_pipe_fd
= move_to_high_fd (parent_pipe_fd
, 1, 64);
3292 pathname
= make_dev_fd_filename (parent_pipe_fd
);
3293 #endif /* HAVE_DEV_FD */
3297 sys_error ("cannot make pipe for process substitution");
3298 return ((char *)NULL
);
3301 old_pid
= last_made_pid
;
3303 #if defined (JOB_CONTROL)
3304 old_pipeline_pgrp
= pipeline_pgrp
;
3305 pipeline_pgrp
= shell_pgrp
;
3307 #endif /* JOB_CONTROL */
3309 pid
= make_child ((char *)NULL
, 1);
3312 reset_terminating_signals (); /* XXX */
3313 /* Cancel traps, in trap.c. */
3314 restore_original_signals ();
3315 setup_async_signals ();
3316 subshell_environment
|= SUBSHELL_COMSUB
;
3319 #if defined (JOB_CONTROL)
3320 set_sigchld_handler ();
3321 stop_making_children ();
3322 pipeline_pgrp
= old_pipeline_pgrp
;
3323 #endif /* JOB_CONTROL */
3327 sys_error ("cannot make child for process substitution");
3329 #if defined (HAVE_DEV_FD)
3330 close (parent_pipe_fd
);
3331 close (child_pipe_fd
);
3332 #endif /* HAVE_DEV_FD */
3333 return ((char *)NULL
);
3338 #if defined (JOB_CONTROL)
3339 restore_pipeline (1);
3342 #if !defined (HAVE_DEV_FD)
3343 fifo_list
[nfifo
-1].proc
= pid
;
3346 last_made_pid
= old_pid
;
3348 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
3350 #endif /* JOB_CONTROL && PGRP_PIPE */
3352 #if defined (HAVE_DEV_FD)
3353 close (child_pipe_fd
);
3354 #endif /* HAVE_DEV_FD */
3359 set_sigint_handler ();
3361 #if defined (JOB_CONTROL)
3362 set_job_control (0);
3363 #endif /* JOB_CONTROL */
3365 #if !defined (HAVE_DEV_FD)
3366 /* Open the named pipe in the child. */
3367 fd
= open (pathname
, open_for_read_in_child
? O_RDONLY
|O_NONBLOCK
: O_WRONLY
);
3370 sys_error ("cannot open named pipe %s for %s", pathname
,
3371 open_for_read_in_child
? "reading" : "writing");
3374 if (open_for_read_in_child
)
3376 if (sh_unset_nodelay_mode (fd
) < 0)
3378 sys_error ("cannout reset nodelay mode for fd %d", fd
);
3382 #else /* HAVE_DEV_FD */
3384 #endif /* HAVE_DEV_FD */
3386 if (dup2 (fd
, open_for_read_in_child
? 0 : 1) < 0)
3388 sys_error ("cannot duplicate named pipe %s as fd %d", pathname
,
3389 open_for_read_in_child
? 0 : 1);
3393 if (fd
!= (open_for_read_in_child
? 0 : 1))
3396 /* Need to close any files that this process has open to pipes inherited
3398 if (current_fds_to_close
)
3400 close_fd_bitmap (current_fds_to_close
);
3401 current_fds_to_close
= (struct fd_bitmap
*)NULL
;
3404 #if defined (HAVE_DEV_FD)
3405 /* Make sure we close the parent's end of the pipe and clear the slot
3406 in the fd list so it is not closed later, if reallocated by, for
3407 instance, pipe(2). */
3408 close (parent_pipe_fd
);
3409 dev_fd_list
[parent_pipe_fd
] = 0;
3410 #endif /* HAVE_DEV_FD */
3412 result
= parse_and_execute (string
, "process substitution", (SEVAL_NONINT
|SEVAL_NOHIST
));
3414 #if !defined (HAVE_DEV_FD)
3415 /* Make sure we close the named pipe in the child before we exit. */
3416 close (open_for_read_in_child
? 0 : 1);
3417 #endif /* !HAVE_DEV_FD */
3422 #endif /* PROCESS_SUBSTITUTION */
3424 /***********************************/
3426 /* Command Substitution */
3428 /***********************************/
3431 read_comsub (fd
, quoted
)
3434 char *istring
, buf
[128], *bufp
;
3435 int istring_index
, istring_size
, c
;
3438 istring
= (char *)NULL
;
3439 istring_index
= istring_size
= bufn
= 0;
3442 setmode (fd
, O_TEXT
); /* we don't want CR/LF, we want Unix-style */
3445 /* Read the output of the command through the pipe. */
3452 bufn
= zread (fd
, buf
, sizeof (buf
));
3462 internal_warning ("read_comsub: ignored null byte in input");
3467 /* Add the character to ISTRING, possibly after resizing it. */
3468 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 2, istring_size
, DEFAULT_ARRAY_SIZE
);
3470 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || c
== CTLESC
|| c
== CTLNUL
)
3471 istring
[istring_index
++] = CTLESC
;
3473 istring
[istring_index
++] = c
;
3476 #if defined (__CYGWIN__)
3477 if (c
== '\n' && istring_index
> 1 && istring
[istring_index
- 2] == '\r')
3480 istring
[istring_index
- 1] = '\n';
3487 istring
[istring_index
] = '\0';
3489 /* If we read no output, just return now and save ourselves some
3491 if (istring_index
== 0)
3494 return (char *)NULL
;
3497 /* Strip trailing newlines from the output of the command. */
3498 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
3500 while (istring_index
> 0)
3502 if (istring
[istring_index
- 1] == '\n')
3506 /* If the newline was quoted, remove the quoting char. */
3507 if (istring
[istring_index
- 1] == CTLESC
)
3513 istring
[istring_index
] = '\0';
3516 strip_trailing (istring
, istring_index
- 1, 1);
3521 /* Perform command substitution on STRING. This returns a string,
3524 command_substitute (string
, quoted
)
3528 pid_t pid
, old_pid
, old_pipeline_pgrp
;
3530 int result
, fildes
[2], function_value
;
3532 istring
= (char *)NULL
;
3534 /* Don't fork () if there is no need to. In the case of no command to
3535 run, just return NULL. */
3536 if (!string
|| !*string
|| (string
[0] == '\n' && !string
[1]))
3537 return ((char *)NULL
);
3539 if (wordexp_only
&& read_but_dont_execute
)
3541 last_command_exit_value
= 125;
3542 jump_to_top_level (EXITPROG
);
3545 /* We're making the assumption here that the command substitution will
3546 eventually run a command from the file system. Since we'll run
3547 maybe_make_export_env in this subshell before executing that command,
3548 the parent shell and any other shells it starts will have to remake
3549 the environment. If we make it before we fork, other shells won't
3550 have to. Don't bother if we have any temporary variable assignments,
3551 though, because the export environment will be remade after this
3552 command completes anyway, but do it if all the words to be expanded
3553 are variable assignments. */
3554 if (subst_assign_varlist
== 0 || garglist
== 0)
3555 maybe_make_export_env (); /* XXX */
3557 /* Pipe the output of executing STRING into the current shell. */
3558 if (pipe (fildes
) < 0)
3560 sys_error ("cannot make pipes for command substitution");
3564 old_pid
= last_made_pid
;
3565 #if defined (JOB_CONTROL)
3566 old_pipeline_pgrp
= pipeline_pgrp
;
3567 /* Don't reset the pipeline pgrp if we're already a subshell in a pipeline. */
3568 if ((subshell_environment
& SUBSHELL_PIPE
) == 0)
3569 pipeline_pgrp
= shell_pgrp
;
3570 cleanup_the_pipeline ();
3573 pid
= make_child ((char *)NULL
, 0);
3575 /* Reset the signal handlers in the child, but don't free the
3577 reset_signal_handlers ();
3579 #if defined (JOB_CONTROL)
3580 set_sigchld_handler ();
3581 stop_making_children ();
3582 pipeline_pgrp
= old_pipeline_pgrp
;
3584 stop_making_children ();
3585 #endif /* JOB_CONTROL */
3589 sys_error ("cannot make child for command substitution");
3595 return ((char *)NULL
);
3600 set_sigint_handler (); /* XXX */
3602 if (dup2 (fildes
[1], 1) < 0)
3604 sys_error ("command_substitute: cannot duplicate pipe as fd 1");
3605 exit (EXECUTION_FAILURE
);
3608 /* If standard output is closed in the parent shell
3609 (such as after `exec >&-'), file descriptor 1 will be
3610 the lowest available file descriptor, and end up in
3611 fildes[0]. This can happen for stdin and stderr as well,
3612 but stdout is more important -- it will cause no output
3613 to be generated from this command. */
3614 if ((fildes
[1] != fileno (stdin
)) &&
3615 (fildes
[1] != fileno (stdout
)) &&
3616 (fildes
[1] != fileno (stderr
)))
3619 if ((fildes
[0] != fileno (stdin
)) &&
3620 (fildes
[0] != fileno (stdout
)) &&
3621 (fildes
[0] != fileno (stderr
)))
3624 /* The currently executing shell is not interactive. */
3627 /* This is a subshell environment. */
3628 subshell_environment
|= SUBSHELL_COMSUB
;
3630 /* When not in POSIX mode, command substitution does not inherit
3632 if (posixly_correct
== 0)
3633 exit_immediately_on_error
= 0;
3635 remove_quoted_escapes (string
);
3637 startup_state
= 2; /* see if we can avoid a fork */
3638 /* Give command substitution a place to jump back to on failure,
3639 so we don't go back up to main (). */
3640 result
= setjmp (top_level
);
3642 /* If we're running a command substitution inside a shell function,
3643 trap `return' so we don't return from the function in the subshell
3644 and go off to never-never land. */
3645 if (result
== 0 && return_catch_flag
)
3646 function_value
= setjmp (return_catch
);
3650 if (result
== EXITPROG
)
3651 exit (last_command_exit_value
);
3653 exit (EXECUTION_FAILURE
);
3654 else if (function_value
)
3655 exit (return_catch_value
);
3657 exit (parse_and_execute (string
, "command substitution", SEVAL_NOHIST
));
3661 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
3663 #endif /* JOB_CONTROL && PGRP_PIPE */
3667 istring
= read_comsub (fildes
[0], quoted
);
3671 current_command_subst_pid
= pid
;
3672 last_command_exit_value
= wait_for (pid
);
3673 last_command_subst_pid
= pid
;
3674 last_made_pid
= old_pid
;
3676 #if defined (JOB_CONTROL)
3677 /* If last_command_exit_value > 128, then the substituted command
3678 was terminated by a signal. If that signal was SIGINT, then send
3679 SIGINT to ourselves. This will break out of loops, for instance. */
3680 if (last_command_exit_value
== (128 + SIGINT
))
3681 kill (getpid (), SIGINT
);
3683 /* wait_for gives the terminal back to shell_pgrp. If some other
3684 process group should have it, give it away to that group here.
3685 pipeline_pgrp is non-zero only while we are constructing a
3686 pipline, so what we are concerned about is whether or not that
3687 pipeline was started in the background. A pipeline started in
3688 the background should never get the tty back here. */
3690 if (interactive
&& pipeline_pgrp
!= (pid_t
)0 && pipeline_pgrp
!= last_asynchronous_pid
)
3692 if (interactive
&& pipeline_pgrp
!= (pid_t
)0 && (subshell_environment
& SUBSHELL_ASYNC
) == 0)
3694 give_terminal_to (pipeline_pgrp
, 0);
3695 #endif /* JOB_CONTROL */
3701 /********************************************************
3703 * Utility functions for parameter expansion *
3705 ********************************************************/
3707 #if defined (ARRAY_VARS)
3710 array_length_reference (s
)
3719 var
= array_variable_part (s
, &t
, &len
);
3721 /* If unbound variables should generate an error, report one and return
3723 if ((var
== 0 || array_p (var
) == 0) && unbound_vars_is_error
)
3727 report_error ("%s: unbound variable", s
);
3734 /* We support a couple of expansions for variables that are not arrays.
3735 We'll return the length of the value for v[0], and 1 for v[@] or
3736 v[*]. Return 0 for everything else. */
3738 array
= array_p (var
) ? array_cell (var
) : (ARRAY
*)NULL
;
3740 if (ALL_ELEMENT_SUB (t
[0]) && t
[1] == ']')
3741 return (array_p (var
) ? array_num_elements (array
) : 1);
3743 ind
= array_expand_index (t
, len
);
3746 report_error ("%s: bad array subscript", t
);
3751 t
= array_reference (array
, ind
);
3753 t
= (ind
== 0) ? value_cell (var
) : (char *)NULL
;
3758 #endif /* ARRAY_VARS */
3761 valid_brace_expansion_word (name
, var_is_special
)
3765 if (DIGIT (*name
) && all_digits (name
))
3767 else if (var_is_special
)
3769 #if defined (ARRAY_VARS)
3770 else if (valid_array_reference (name
))
3772 #endif /* ARRAY_VARS */
3773 else if (legal_identifier (name
))
3779 /* Parameter expand NAME, and return a new string which is the expansion,
3780 or NULL if there was no expansion.
3781 VAR_IS_SPECIAL is non-zero if NAME is one of the special variables in
3782 the shell, e.g., "@", "$", "*", etc. QUOTED, if non-zero, means that
3783 NAME was found inside of a double-quoted expression. */
3785 parameter_brace_expand_word (name
, var_is_special
, quoted
)
3787 int var_is_special
, quoted
;
3796 /* Handle multiple digit arguments, as in ${11}. */
3798 if (legal_number (name
, &arg_index
))
3799 temp
= get_dollar_var_value (arg_index
);
3800 else if (var_is_special
) /* ${@} */
3803 tt
= (char *)xmalloc (2 + strlen (name
));
3804 tt
[sindex
= 0] = '$';
3805 strcpy (tt
+ 1, name
);
3807 l
= expand_string_leave_quoted (tt
, quoted
);
3809 temp
= string_list (l
);
3812 temp
= param_expand (tt
, &sindex
, quoted
, (int *)NULL
, (int *)NULL
,
3813 (int *)NULL
, (int *)NULL
, 0);
3817 #if defined (ARRAY_VARS)
3818 else if (valid_array_reference (name
))
3820 temp
= array_value (name
, quoted
);
3823 else if (var
= find_variable (name
))
3825 if (var
&& invisible_p (var
) == 0)
3827 #if defined (ARRAY_VARS)
3828 temp
= array_p (var
) ? array_reference (array_cell (var
), 0) : value_cell (var
);
3830 temp
= value_cell (var
);
3834 temp
= quote_escapes (temp
);
3836 if (tempvar_p (var
))
3837 dispose_variable (var
);
3840 temp
= (char *)NULL
;
3843 temp
= (char *)NULL
;
3848 /* Expand an indirect reference to a variable: ${!NAME} expands to the
3849 value of the variable whose name is the value of NAME. */
3851 parameter_brace_expand_indir (name
, var_is_special
, quoted
)
3853 int var_is_special
, quoted
;
3857 t
= parameter_brace_expand_word (name
, var_is_special
, quoted
);
3861 temp
= parameter_brace_expand_word (t
, t
[0] == '@' && t
[1] == '\0', quoted
);
3863 temp
= parameter_brace_expand_word (t
, SPECIAL_VAR(t
, 0), quoted
);
3869 /* Expand the right side of a parameter expansion of the form ${NAMEcVALUE},
3870 depending on the value of C, the separating character. C can be one of
3871 "-", "+", or "=". QUOTED is true if the entire brace expression occurs
3872 between double quotes. */
3874 parameter_brace_expand_rhs (name
, value
, c
, quoted
, qdollaratp
, hasdollarat
)
3876 int c
, quoted
, *qdollaratp
, *hasdollarat
;
3879 char *t
, *t1
, *temp
;
3882 temp
= (*value
== '~' || (strchr (value
, '~') && unquoted_substring ("=~", value
)))
3883 ? bash_tilde_expand (value
)
3884 : savestring (value
);
3886 /* If the entire expression is between double quotes, we want to treat
3887 the value as a double-quoted string, with the exception that we strip
3888 embedded unescaped double quotes. */
3889 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && *temp
)
3892 t
= string_extract_double_quoted (temp
, &hasdol
, 1);
3898 /* XXX was 0 not quoted */
3899 l
= *temp
? expand_string_for_rhs (temp
, quoted
, &hasdol
, (int *)NULL
)
3902 *hasdollarat
= hasdol
|| (l
&& l
->next
);
3906 /* The expansion of TEMP returned something. We need to treat things
3907 slightly differently if HASDOL is non-zero. */
3908 temp
= string_list (l
);
3909 /* If l->next is not null, we know that TEMP contained "$@", since that
3910 is the only expansion that creates more than one word. */
3911 if ((hasdol
&& quoted
) || l
->next
)
3915 else if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && hasdol
)
3917 /* The brace expansion occurred between double quotes and there was
3918 a $@ in TEMP. It does not matter if the $@ is quoted, as long as
3919 it does not expand to anything. In this case, we want to return
3920 a quoted empty string. */
3921 temp
= (char *)xmalloc (2);
3926 temp
= (char *)NULL
;
3928 if (c
== '-' || c
== '+')
3932 t
= temp
? savestring (temp
) : savestring ("");
3933 t1
= dequote_string (t
);
3935 bind_variable (name
, t1
);
3940 /* Deal with the right hand side of a ${name:?value} expansion in the case
3941 that NAME is null or not set. If VALUE is non-null it is expanded and
3942 used as the error message to print, otherwise a standard message is
3945 parameter_brace_expand_error (name
, value
)
3951 if (value
&& *value
)
3953 temp
= (*value
== '~' || (strchr (value
, '~') && unquoted_substring ("=~", value
)))
3954 ? bash_tilde_expand (value
)
3955 : savestring (value
);
3957 l
= expand_string (temp
, 0);
3959 temp
= string_list (l
);
3960 report_error ("%s: %s", name
, temp
? temp
: ""); /* XXX was value not "" */
3965 report_error ("%s: parameter null or not set", name
);
3967 /* Free the data we have allocated during this expansion, since we
3968 are about to longjmp out. */
3973 /* Return 1 if NAME is something for which parameter_brace_expand_length is
3976 valid_length_expression (name
)
3979 return (name
[1] == '\0' || /* ${#} */
3980 ((sh_syntaxtab
[(unsigned char) name
[1]] & CSPECVAR
) && name
[2] == '\0') || /* special param */
3981 (DIGIT (name
[1]) && all_digits (name
+ 1)) || /* ${#11} */
3982 #if defined (ARRAY_VARS)
3983 valid_array_reference (name
+ 1) || /* ${#a[7]} */
3985 legal_identifier (name
+ 1)); /* ${#PS1} */
3988 /* Handle the parameter brace expansion that requires us to return the
3989 length of a parameter. */
3991 parameter_brace_expand_length (name
)
3995 long number
, arg_index
;
3997 #if defined (ARRAY_VARS)
4001 if (name
[1] == '\0') /* ${#} */
4002 number
= number_of_args ();
4003 else if ((name
[1] == '@' || name
[1] == '*') && name
[2] == '\0') /* ${#@}, ${#*} */
4004 number
= number_of_args ();
4005 else if ((sh_syntaxtab
[(unsigned char) name
[1]] & CSPECVAR
) && name
[2] == '\0')
4007 /* Take the lengths of some of the shell's special parameters. */
4011 t
= which_set_flags ();
4014 t
= itos (last_command_exit_value
);
4017 t
= itos (dollar_dollar_pid
);
4020 if (last_asynchronous_pid
== NO_PID
)
4023 t
= itos (last_asynchronous_pid
);
4026 t
= itos (number_of_args ());
4029 number
= STRLEN (t
);
4032 #if defined (ARRAY_VARS)
4033 else if (valid_array_reference (name
+ 1))
4034 number
= array_length_reference (name
+ 1);
4035 #endif /* ARRAY_VARS */
4040 if (legal_number (name
+ 1, &arg_index
)) /* ${#1} */
4042 t
= get_dollar_var_value (arg_index
);
4043 number
= STRLEN (t
);
4046 #if defined (ARRAY_VARS)
4047 else if ((var
= find_variable (name
+ 1)) && array_p (var
))
4049 t
= array_reference (array_cell (var
), 0);
4050 number
= STRLEN (t
);
4055 newname
= savestring (name
);
4057 list
= expand_string (newname
, Q_DOUBLE_QUOTES
);
4058 t
= list
? string_list (list
) : (char *)NULL
;
4061 dispose_words (list
);
4063 number
= STRLEN (t
);
4071 /* Skip characters in SUBSTR until DELIM. SUBSTR is an arithmetic expression,
4072 so we do some ad-hoc parsing of an arithmetic expression to find
4073 the first DELIM, instead of using strchr(3). Two rules:
4074 1. If the substring contains a `(', read until closing `)'.
4075 2. If the substring contains a `?', read past one `:' for each `?'.
4079 skiparith (substr
, delim
)
4083 int skipcol
, pcount
;
4086 for (skipcol
= pcount
= 0, t
= substr
; *t
; t
++)
4088 /* Balance parens */
4094 if (*t
== ')' && pcount
)
4102 /* Skip one `:' for each `?' */
4103 if (*t
== ':' && skipcol
)
4119 /* Verify and limit the start and end of the desired substring. If
4120 VTYPE == 0, a regular shell variable is being used; if it is 1,
4121 then the positional parameters are being used; if it is 2, then
4122 VALUE is really a pointer to an array variable that should be used.
4123 Return value is 1 if both values were OK, 0 if there was a problem
4124 with an invalid expression, or -1 if the values were out of range. */
4126 verify_substring_values (value
, substr
, vtype
, e1p
, e2p
)
4127 char *value
, *substr
;
4131 char *t
, *temp1
, *temp2
;
4134 #if defined (ARRAY_VARS)
4138 /* duplicate behavior of strchr(3) */
4139 t
= skiparith (substr
, ':');
4140 if (*t
&& *t
== ':')
4145 temp1
= expand_string_if_necessary (substr
, Q_DOUBLE_QUOTES
, expand_string
);
4146 *e1p
= evalexp (temp1
, &expok
);
4151 len
= -1; /* paranoia */
4155 case VT_ARRAYMEMBER
:
4156 len
= strlen (value
);
4159 len
= number_of_args () + 1;
4161 #if defined (ARRAY_VARS)
4164 len
= array_num_elements (a
) + 1;
4169 if (len
== -1) /* paranoia */
4172 if (*e1p
< 0) /* negative offsets count from end */
4175 if (*e1p
>= len
|| *e1p
< 0)
4181 temp2
= savestring (t
);
4182 temp1
= expand_string_if_necessary (temp2
, Q_DOUBLE_QUOTES
, expand_string
);
4185 *e2p
= evalexp (temp1
, &expok
);
4191 internal_error ("%s: substring expression < 0", t
);
4194 *e2p
+= *e1p
; /* want E2 chars starting at E1 */
4204 /* Return the type of variable specified by VARNAME (simple variable,
4205 positional param, or array variable). Also return the value specified
4206 by VARNAME (value of a variable or a reference to an array element). */
4208 get_var_and_type (varname
, value
, varp
, valp
)
4209 char *varname
, *value
;
4215 #if defined (ARRAY_VARS)
4219 vtype
= (varname
[0] == '@' || varname
[0] == '*') && varname
[1] == '\0'; /* VT_POSPARMS */
4220 *varp
= (SHELL_VAR
*)NULL
;
4222 #if defined (ARRAY_VARS)
4223 if (valid_array_reference (varname
))
4225 v
= array_variable_part (varname
, &temp
, (int *)0);
4226 if (v
&& array_p (v
))
4228 if (ALL_ELEMENT_SUB (temp
[0]) && temp
[1] == ']')
4230 vtype
= VT_ARRAYVAR
;
4231 *valp
= (char *)array_cell (v
);
4235 vtype
= VT_ARRAYMEMBER
;
4236 *valp
= array_value (varname
, 1);
4243 else if ((v
= find_variable (varname
)) && array_p (v
))
4245 vtype
= VT_VARIABLE
;
4247 *valp
= array_reference (array_cell (v
), 0);
4256 /******************************************************/
4258 /* Functions to extract substrings of variable values */
4260 /******************************************************/
4262 /* Process a variable substring expansion: ${name:e1[:e2]}. If VARNAME
4263 is `@', use the positional parameters; otherwise, use the value of
4264 VARNAME. If VARNAME is an array variable, use the array elements. */
4267 parameter_brace_substring (varname
, value
, substr
, quoted
)
4268 char *varname
, *value
, *substr
;
4277 return ((char *)NULL
);
4279 this_command_name
= varname
;
4281 vtype
= get_var_and_type (varname
, value
, &v
, &val
);
4283 return ((char *)NULL
);
4285 r
= verify_substring_values (val
, substr
, vtype
, &e1
, &e2
);
4288 if (val
&& vtype
== VT_ARRAYMEMBER
)
4290 return ((r
== 0) ? &expand_param_error
: (char *)NULL
);
4296 case VT_ARRAYMEMBER
:
4297 temp
= quoted
? quoted_substring (value
, e1
, e2
) : substring (value
, e1
, e2
);
4298 if (val
&& vtype
== VT_ARRAYMEMBER
)
4302 temp
= pos_params (varname
, e1
, e2
, quoted
);
4304 #if defined (ARRAY_VARS)
4306 temp
= array_subrange (array_cell (v
), e1
, e2
, quoted
);
4310 temp
= (char *)NULL
;
4316 /****************************************************************/
4318 /* Functions to perform pattern substitution on variable values */
4320 /****************************************************************/
4323 pat_subst (string
, pat
, rep
, mflags
)
4324 char *string
, *pat
, *rep
;
4327 char *ret
, *s
, *e
, *str
;
4328 int rsize
, rptr
, l
, replen
, mtype
;
4330 mtype
= mflags
& MATCH_TYPEMASK
;
4333 * 1. A null pattern with mtype == MATCH_BEG means to prefix STRING
4334 * with REP and return the result.
4335 * 2. A null pattern with mtype == MATCH_END means to append REP to
4336 * STRING and return the result.
4338 if ((pat
== 0 || *pat
== 0) && (mtype
== MATCH_BEG
|| mtype
== MATCH_END
))
4340 replen
= STRLEN (rep
);
4341 l
= strlen (string
);
4342 ret
= (char *)xmalloc (replen
+ l
+ 2);
4344 strcpy (ret
, string
);
4345 else if (mtype
== MATCH_BEG
)
4348 strcpy (ret
+ replen
, string
);
4352 strcpy (ret
, string
);
4353 strcpy (ret
+ l
, rep
);
4358 ret
= (char *)xmalloc (rsize
= 64);
4361 for (replen
= STRLEN (rep
), rptr
= 0, str
= string
;;)
4363 if (match_pattern (str
, pat
, mtype
, &s
, &e
) == 0)
4366 RESIZE_MALLOCED_BUFFER (ret
, rptr
, (l
+ replen
), rsize
, 64);
4368 /* OK, now copy the leading unmatched portion of the string (from
4369 str to s) to ret starting at rptr (the current offset). Then copy
4370 the replacement string at ret + rptr + (s - str). Increment
4371 rptr (if necessary) and str and go on. */
4374 strncpy (ret
+ rptr
, str
, l
);
4379 strncpy (ret
+ rptr
, rep
, replen
);
4383 e
++; /* avoid infinite recursion on zero-length match */
4384 str
= e
; /* e == end of match */
4385 if (((mflags
& MATCH_GLOBREP
) == 0) || mtype
!= MATCH_ANY
)
4389 /* Now copy the unmatched portion of the input string */
4392 RESIZE_MALLOCED_BUFFER (ret
, rptr
, STRLEN(str
) + 1, rsize
, 64);
4393 strcpy (ret
+ rptr
, str
);
4401 /* Do pattern match and replacement on the positional parameters. */
4403 pos_params_pat_subst (string
, pat
, rep
, mflags
)
4404 char *string
, *pat
, *rep
;
4407 WORD_LIST
*save
, *params
;
4411 save
= params
= list_rest_of_args ();
4413 return ((char *)NULL
);
4415 for ( ; params
; params
= params
->next
)
4417 ret
= pat_subst (params
->word
->word
, pat
, rep
, mflags
);
4418 w
= make_bare_word (ret
);
4419 dispose_word (params
->word
);
4424 ret
= string_list ((mflags
& MATCH_QUOTED
) ? quote_list (save
) : save
);
4425 dispose_words (save
);
4430 /* Perform pattern substitution on VALUE, which is the expansion of
4431 VARNAME. PATSUB is an expression supplying the pattern to match
4432 and the string to substitute. QUOTED is a flags word containing
4433 the type of quoting currently in effect. */
4435 parameter_brace_patsub (varname
, value
, patsub
, quoted
)
4436 char *varname
, *value
, *patsub
;
4440 char *val
, *temp
, *pat
, *rep
, *p
, *lpatsub
;
4444 return ((char *)NULL
);
4446 this_command_name
= varname
;
4448 vtype
= get_var_and_type (varname
, value
, &v
, &val
);
4450 return ((char *)NULL
);
4455 mflags
|= MATCH_GLOBREP
;
4458 /* Malloc this because expand_string_if_necessary or one of the expansion functions
4459 in its call chain may free it on a substitution error. */
4460 lpatsub
= savestring (patsub
);
4462 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
4463 mflags
|= MATCH_QUOTED
;
4465 if (rep
= quoted_strchr (lpatsub
, '/', ST_BACKSL
))
4470 if (rep
&& *rep
== '\0')
4473 /* Expand PAT and REP for command, variable and parameter, arithmetic,
4474 and process substitution. Also perform quote removal. Do not
4475 perform word splitting or filename generation. */
4477 pat
= expand_string_if_necessary (lpatsub
, quoted
, expand_string_unsplit
);
4479 pat
= expand_string_if_necessary (lpatsub
, (quoted
& ~Q_DOUBLE_QUOTES
), expand_string_unsplit
);
4484 if ((mflags
& MATCH_QUOTED
) == 0)
4485 rep
= expand_string_if_necessary (rep
, quoted
, expand_string_unsplit
);
4487 rep
= expand_string_to_string_internal (rep
, quoted
, expand_string_unsplit
);
4491 if (pat
&& pat
[0] == '#')
4493 mflags
|= MATCH_BEG
;
4496 else if (pat
&& pat
[0] == '%')
4498 mflags
|= MATCH_END
;
4502 mflags
|= MATCH_ANY
;
4504 /* OK, we now want to substitute REP for PAT in VAL. If
4505 flags & MATCH_GLOBREP is non-zero, the substitution is done
4506 everywhere, otherwise only the first occurrence of PAT is
4511 case VT_ARRAYMEMBER
:
4512 temp
= pat_subst (val
, p
, rep
, mflags
);
4515 temp
= pos_params_pat_subst (val
, p
, rep
, mflags
);
4517 #if defined (ARRAY_VARS)
4519 temp
= array_pat_subst (array_cell (v
), p
, rep
, mflags
);
4524 if (val
&& v
&& array_p (v
) && vtype
== VT_ARRAYMEMBER
)
4534 /****************************************************************/
4536 /* Functions to perform parameter expansion on a string */
4538 /****************************************************************/
4540 /* ${[#][!]name[[:]#[#]%[%]-=?+[word][:e1[:e2]]]} */
4542 parameter_brace_expand (string
, indexp
, quoted
, quoted_dollar_atp
, contains_dollar_at
)
4544 int *indexp
, quoted
, *quoted_dollar_atp
, *contains_dollar_at
;
4546 int check_nullness
, var_is_set
, var_is_null
, var_is_special
;
4547 int want_substring
, want_indir
, want_patsub
;
4548 char *name
, *value
, *temp
, *temp1
;
4549 int t_index
, sindex
, c
;
4552 value
= (char *)NULL
;
4553 var_is_set
= var_is_null
= var_is_special
= check_nullness
= 0;
4554 want_substring
= want_indir
= want_patsub
= 0;
4558 name
= string_extract (string
, &t_index
, "#%:-=?+/}", 1);
4560 /* If the name really consists of a special variable, then make sure
4561 that we have the entire name. We don't allow indirect references
4562 to special variables except `#', `?', `@' and `*'. */
4563 if ((sindex
== t_index
&&
4564 (string
[t_index
] == '-' ||
4565 string
[t_index
] == '?' ||
4566 string
[t_index
] == '#')) ||
4567 (sindex
== t_index
- 1 && string
[sindex
] == '!' &&
4568 (string
[t_index
] == '#' ||
4569 string
[t_index
] == '?' ||
4570 string
[t_index
] == '@' ||
4571 string
[t_index
] == '*')))
4575 temp1
= string_extract (string
, &t_index
, "#%:-=?+/}", 0);
4576 name
= (char *)xmalloc (3 + (strlen (temp1
)));
4577 *name
= string
[sindex
];
4578 if (string
[sindex
] == '!')
4580 /* indirect reference of $#, $?, $@, or $* */
4581 name
[1] = string
[sindex
+ 1];
4582 strcpy (name
+ 2, temp1
);
4585 strcpy (name
+ 1, temp1
);
4590 /* Find out what character ended the variable name. Then
4591 do the appropriate thing. */
4592 if (c
= string
[sindex
])
4595 /* If c is followed by one of the valid parameter expansion
4596 characters, move past it as normal. If not, assume that
4597 a substring specification is being given, and do not move
4599 if (c
== ':' && VALID_PARAM_EXPAND_CHAR (string
[sindex
]))
4602 if (c
= string
[sindex
])
4605 else if (c
== ':' && string
[sindex
] != RBRACE
)
4607 else if (c
== '/' && string
[sindex
] != RBRACE
)
4610 /* Catch the valid and invalid brace expressions that made it through the
4612 /* ${#-} is a valid expansion and means to take the length of $-.
4613 Similarly for ${#?} and ${##}... */
4614 if (name
[0] == '#' && name
[1] == '\0' && check_nullness
== 0 &&
4615 VALID_SPECIAL_LENGTH_PARAM (c
) && string
[sindex
] == RBRACE
)
4617 name
= (char *)xrealloc (name
, 3);
4620 c
= string
[sindex
++];
4623 /* ...but ${#%}, ${#:}, ${#=}, ${#+}, and ${#/} are errors. */
4624 if (name
[0] == '#' && name
[1] == '\0' && check_nullness
== 0 &&
4625 member (c
, "%:=+/") && string
[sindex
] == RBRACE
)
4627 temp
= (char *)NULL
;
4628 goto bad_substitution
;
4631 /* Indirect expansion begins with a `!'. A valid indirect expansion is
4632 either a variable name, one of the positional parameters or a special
4633 variable that expands to one of the positional parameters. */
4634 want_indir
= *name
== '!' &&
4635 (legal_variable_starter ((unsigned char)name
[1]) || DIGIT (name
[1])
4636 || VALID_INDIR_PARAM (name
[1]));
4638 /* Determine the value of this variable. */
4640 /* Check for special variables, directly referenced. */
4641 if (SPECIAL_VAR (name
, want_indir
))
4644 /* Check for special expansion things, like the length of a parameter */
4645 if (*name
== '#' && name
[1])
4647 /* If we are not pointing at the character just after the
4648 closing brace, then we haven't gotten all of the name.
4649 Since it begins with a special character, this is a bad
4650 substitution. Also check NAME for validity before trying
4652 if (string
[sindex
- 1] != RBRACE
|| (valid_length_expression (name
) == 0))
4654 temp
= (char *)NULL
;
4655 goto bad_substitution
;
4658 number
= parameter_brace_expand_length (name
);
4662 return ((number
< 0) ? &expand_param_error
: itos (number
));
4665 /* ${@} is identical to $@. */
4666 if (name
[0] == '@' && name
[1] == '\0')
4668 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
4669 *quoted_dollar_atp
= 1;
4671 if (contains_dollar_at
)
4672 *contains_dollar_at
= 1;
4675 /* Process ${PREFIX*} expansion. */
4676 if (want_indir
&& string
[sindex
- 1] == RBRACE
&&
4677 (string
[sindex
- 2] == '*' || string
[sindex
- 2] == '@') &&
4678 legal_variable_starter ((unsigned char) name
[1]))
4683 temp1
= savestring (name
+ 1);
4684 number
= strlen (temp1
);
4685 temp1
[number
- 1] = '\0';
4686 x
= all_variables_matching_prefix (temp1
);
4687 xlist
= argv_to_word_list (x
, 1, 0);
4688 if (string
[sindex
- 2] == '*')
4689 temp
= string_list_dollar_star (xlist
);
4692 temp
= string_list_dollar_at (xlist
, quoted
);
4693 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
4694 *quoted_dollar_atp
= 1;
4695 if (contains_dollar_at
)
4696 *contains_dollar_at
= 1;
4705 /* Make sure that NAME is valid before trying to go on. */
4706 if (valid_brace_expansion_word (want_indir
? name
+ 1 : name
,
4707 var_is_special
) == 0)
4709 temp
= (char *)NULL
;
4710 goto bad_substitution
;
4714 temp
= parameter_brace_expand_indir (name
+ 1, var_is_special
, quoted
);
4716 temp
= parameter_brace_expand_word (name
, var_is_special
, quoted
);
4718 #if defined (ARRAY_VARS)
4719 if (valid_array_reference (name
))
4721 temp1
= strchr (name
, '[');
4722 if (temp1
&& temp1
[1] == '@' && temp1
[2] == ']')
4724 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
4725 *quoted_dollar_atp
= 1;
4726 if (contains_dollar_at
)
4727 *contains_dollar_at
= 1;
4729 /* ${array[*]}, when unquoted, should be treated like ${array[@]},
4730 which should result in separate words even when IFS is unset. */
4731 if (temp1
&& temp1
[1] == '*' && temp1
[2] == ']' && quoted
== 0)
4733 if (contains_dollar_at
)
4734 *contains_dollar_at
= 1;
4739 var_is_set
= temp
!= (char *)0;
4740 var_is_null
= check_nullness
&& (var_is_set
== 0 || *temp
== 0);
4742 /* Get the rest of the stuff inside the braces. */
4743 if (c
&& c
!= RBRACE
)
4745 /* Extract the contents of the ${ ... } expansion
4746 according to the Posix.2 rules. */
4747 value
= extract_dollar_brace_string (string
, &sindex
, quoted
);
4748 if (string
[sindex
] == RBRACE
)
4751 goto bad_substitution
;
4754 value
= (char *)NULL
;
4758 /* If this is a substring spec, process it and add the result. */
4761 temp1
= parameter_brace_substring (name
, temp
, value
, quoted
);
4767 else if (want_patsub
)
4769 temp1
= parameter_brace_patsub (name
, temp
, value
, quoted
);
4776 /* Do the right thing based on which character ended the variable name. */
4782 report_error ("%s: bad substitution", string
? string
: "??");
4786 return &expand_param_error
;
4789 if (var_is_set
== 0 && unbound_vars_is_error
)
4791 report_error ("%s: unbound variable", name
);
4795 last_command_exit_value
= EXECUTION_FAILURE
;
4796 return (interactive_shell
? &expand_param_error
: &expand_param_fatal
);
4800 case '#': /* ${param#[#]pattern} */
4801 case '%': /* ${param%[%]pattern} */
4802 if (value
== 0 || *value
== '\0' || temp
== 0 || *temp
== '\0')
4807 if ((name
[0] == '@' || name
[0] == '*') && name
[1] == '\0')
4808 temp1
= parameter_list_remove_pattern (value
, name
[0], c
, quoted
);
4809 #if defined (ARRAY_VARS)
4810 else if (valid_array_reference (name
))
4811 temp1
= array_remove_pattern (value
, name
, temp
, c
, quoted
);
4814 temp1
= parameter_brace_remove_pattern (value
, temp
, c
, quoted
);
4824 if (var_is_set
&& var_is_null
== 0)
4826 /* If the operator is `+', we don't want the value of the named
4827 variable for anything, just the value of the right hand side. */
4831 /* XXX -- if we're double-quoted and the named variable is "$@",
4832 we want to turn off any special handling of "$@" --
4833 we're not using it, so whatever is on the rhs applies. */
4834 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
4835 *quoted_dollar_atp
= 0;
4836 if (contains_dollar_at
)
4837 *contains_dollar_at
= 0;
4842 temp
= parameter_brace_expand_rhs (name
, value
, c
,
4845 contains_dollar_at
);
4849 temp
= (char *)NULL
;
4855 /* Otherwise do nothing; just use the value in TEMP. */
4857 else /* VAR not set or VAR is NULL. */
4860 temp
= (char *)NULL
;
4861 if (c
== '=' && var_is_special
)
4863 report_error ("$%s: cannot assign in this way", name
);
4866 return &expand_param_error
;
4870 parameter_brace_expand_error (name
, value
);
4871 return (interactive_shell
? &expand_param_error
: &expand_param_fatal
);
4875 /* XXX -- if we're double-quoted and the named variable is "$@",
4876 we want to turn off any special handling of "$@" --
4877 we're not using it, so whatever is on the rhs applies. */
4878 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
4879 *quoted_dollar_atp
= 0;
4880 if (contains_dollar_at
)
4881 *contains_dollar_at
= 0;
4883 temp
= parameter_brace_expand_rhs (name
, value
, c
, quoted
,
4885 contains_dollar_at
);
4896 /* Expand a single ${xxx} expansion. The braces are optional. When
4897 the braces are used, parameter_brace_expand() does the work,
4898 possibly calling param_expand recursively. */
4900 param_expand (string
, sindex
, quoted
, expanded_something
,
4901 contains_dollar_at
, quoted_dollar_at_p
, had_quoted_null_p
,
4904 int *sindex
, quoted
, *expanded_something
, *contains_dollar_at
;
4905 int *quoted_dollar_at_p
, *had_quoted_null_p
, pflags
;
4908 int zindex
, t_index
, expok
;
4915 c
= string
[++zindex
];
4917 temp
= (char *)NULL
;
4919 /* Do simple cases first. Switch on what follows '$'. */
4933 temp1
= dollar_vars
[TODIGIT (c
)];
4934 if (unbound_vars_is_error
&& temp1
== (char *)NULL
)
4936 report_error ("$%c: unbound variable", c
);
4937 last_command_exit_value
= EXECUTION_FAILURE
;
4938 return (interactive_shell
? &expand_param_error
: &expand_param_fatal
);
4940 temp
= temp1
? savestring (temp1
) : (char *)NULL
;
4943 /* $$ -- pid of the invoking shell. */
4945 temp
= itos (dollar_dollar_pid
);
4948 /* $# -- number of positional parameters. */
4950 temp
= itos (number_of_args ());
4953 /* $? -- return value of the last synchronous command. */
4955 temp
= itos (last_command_exit_value
);
4958 /* $- -- flags supplied to the shell on invocation or by `set'. */
4960 temp
= which_set_flags ();
4963 /* $! -- Pid of the last asynchronous command. */
4965 /* If no asynchronous pids have been created, expand to nothing.
4966 If `set -u' has been executed, and no async processes have
4967 been created, this is an expansion error. */
4968 if (last_asynchronous_pid
== NO_PID
)
4970 if (expanded_something
)
4971 *expanded_something
= 0;
4972 temp
= (char *)NULL
;
4973 if (unbound_vars_is_error
)
4975 report_error ("$%c: unbound variable", c
);
4976 last_command_exit_value
= EXECUTION_FAILURE
;
4977 return (interactive_shell
? &expand_param_error
: &expand_param_fatal
);
4981 temp
= itos (last_asynchronous_pid
);
4984 /* The only difference between this and $@ is when the arg is quoted. */
4985 case '*': /* `$*' */
4986 list
= list_rest_of_args ();
4988 /* If there are no command-line arguments, this should just
4989 disappear if there are other characters in the expansion,
4990 even if it's quoted. */
4991 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && list
== 0)
4992 temp
= (char *)NULL
;
4993 else if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
4995 /* If we have "$*" we want to make a string of the positional
4996 parameters, separated by the first character of $IFS, and
4997 quote the whole string, including the separators. If IFS
4998 is unset, the parameters are separated by ' '; if $IFS is
4999 null, the parameters are concatenated. */
5000 temp
= string_list_dollar_star (list
);
5001 temp1
= quote_string (temp
);
5007 /* If the $* is not quoted it is identical to $@ */
5008 temp
= string_list_dollar_at (list
, quoted
);
5009 if (expand_no_split_dollar_star
== 0 && contains_dollar_at
)
5010 *contains_dollar_at
= 1;
5013 dispose_words (list
);
5016 /* When we have "$@" what we want is "$1" "$2" "$3" ... This
5017 means that we have to turn quoting off after we split into
5018 the individually quoted arguments so that the final split
5019 on the first character of $IFS is still done. */
5020 case '@': /* `$@' */
5021 list
= list_rest_of_args ();
5023 /* We want to flag the fact that we saw this. We can't turn
5024 off quoting entirely, because other characters in the
5025 string might need it (consider "\"$@\""), but we need some
5026 way to signal that the final split on the first character
5027 of $IFS should be done, even though QUOTED is 1. */
5028 if (quoted_dollar_at_p
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
5029 *quoted_dollar_at_p
= 1;
5030 if (contains_dollar_at
)
5031 *contains_dollar_at
= 1;
5033 /* We want to separate the positional parameters with the first
5034 character of $IFS in case $IFS is something other than a space.
5035 We also want to make sure that splitting is done no matter what --
5036 according to POSIX.2, this expands to a list of the positional
5037 parameters no matter what IFS is set to. */
5038 temp
= string_list_dollar_at (list
, quoted
);
5040 dispose_words (list
);
5044 temp
= parameter_brace_expand (string
, &zindex
, quoted
,
5046 contains_dollar_at
);
5047 if (temp
== &expand_param_error
|| temp
== &expand_param_fatal
)
5051 /* Quoted nulls should be removed if there is anything else
5053 /* Note that we saw the quoted null so we can add one back at
5054 the end of this function if there are no other characters
5055 in the string, discard TEMP, and go on. The exception to
5056 this is when we have "${@}" and $1 is '', since $@ needs
5057 special handling. */
5058 if (temp
&& QUOTED_NULL (temp
))
5060 if (had_quoted_null_p
)
5061 *had_quoted_null_p
= 1;
5062 if (*quoted_dollar_at_p
== 0)
5065 temp
= (char *)NULL
;
5072 /* Do command or arithmetic substitution. */
5074 /* We have to extract the contents of this paren substitution. */
5075 t_index
= zindex
+ 1;
5076 temp
= extract_command_subst (string
, &t_index
);
5079 /* For Posix.2-style `$(( ))' arithmetic substitution,
5080 extract the expression and pass it to the evaluator. */
5081 if (temp
&& *temp
== LPAREN
)
5085 temp2
= savestring (temp1
);
5086 t_index
= strlen (temp2
) - 1;
5088 if (temp2
[t_index
] != RPAREN
)
5094 /* Cut off ending `)' */
5095 temp2
[t_index
] = '\0';
5097 /* Expand variables found inside the expression. */
5098 temp1
= expand_string_if_necessary (temp2
, Q_DOUBLE_QUOTES
, expand_string
);
5102 /* No error messages. */
5103 this_command_name
= (char *)NULL
;
5104 number
= evalexp (temp1
, &expok
);
5109 if (interactive_shell
== 0 && posixly_correct
)
5111 last_command_exit_value
= EXECUTION_FAILURE
;
5112 return (&expand_param_fatal
);
5115 return (&expand_param_error
);
5117 temp
= itos (number
);
5122 temp1
= command_substitute (temp
, quoted
);
5127 /* Do POSIX.2d9-style arithmetic substitution. This will probably go
5128 away in a future bash release. */
5130 /* Extract the contents of this arithmetic substitution. */
5131 t_index
= zindex
+ 1;
5132 temp
= extract_arithmetic_subst (string
, &t_index
);
5135 /* Do initial variable expansion. */
5136 temp1
= expand_string_if_necessary (temp
, Q_DOUBLE_QUOTES
, expand_string
);
5141 /* Find the variable in VARIABLE_LIST. */
5142 temp
= (char *)NULL
;
5144 for (t_index
= zindex
; (c
= string
[zindex
]) && legal_variable_char (c
); zindex
++)
5146 temp1
= (zindex
> t_index
) ? substring (string
, t_index
, zindex
) : (char *)NULL
;
5148 /* If this isn't a variable name, then just output the `$'. */
5149 if (temp1
== 0 || *temp1
== '\0')
5152 temp
= (char *)xmalloc (2);
5155 if (expanded_something
)
5156 *expanded_something
= 0;
5160 /* If the variable exists, return its value cell. */
5161 var
= find_variable (temp1
);
5163 if (var
&& invisible_p (var
) == 0 && value_cell (var
))
5165 #if defined (ARRAY_VARS)
5168 temp
= array_reference (array_cell (var
), 0);
5170 temp
= quote_escapes (temp
);
5174 temp
= quote_escapes (value_cell (var
));
5176 if (tempvar_p (var
)) /* XXX */
5178 dispose_variable (var
); /* XXX */
5179 var
= (SHELL_VAR
*)NULL
;
5184 temp
= (char *)NULL
;
5186 if (unbound_vars_is_error
)
5187 report_error ("%s: unbound variable", temp1
);
5195 last_command_exit_value
= EXECUTION_FAILURE
;
5196 return ((unbound_vars_is_error
&& interactive_shell
== 0)
5197 ? &expand_param_fatal
5198 : &expand_param_error
);
5209 /* Make a word list which is the result of parameter and variable
5210 expansion, command substitution, arithmetic substitution, and
5211 quote removal of WORD. Return a pointer to a WORD_LIST which is
5212 the result of the expansion. If WORD contains a null word, the
5213 word list returned is also null.
5215 QUOTED contains flag values defined in shell.h.
5217 ISEXP is used to tell expand_word_internal that the word should be
5218 treated as the result of an expansion. This has implications for
5219 how IFS characters in the word are treated.
5221 CONTAINS_DOLLAR_AT and EXPANDED_SOMETHING are return values; when non-null
5222 they point to an integer value which receives information about expansion.
5223 CONTAINS_DOLLAR_AT gets non-zero if WORD contained "$@", else zero.
5224 EXPANDED_SOMETHING get non-zero if WORD contained any parameter expansions,
5227 This only does word splitting in the case of $@ expansion. In that
5228 case, we split on ' '. */
5230 /* Values for the local variable quoted_state. */
5232 #define PARTIALLY_QUOTED 1
5233 #define WHOLLY_QUOTED 2
5236 expand_word_internal (word
, quoted
, isexp
, contains_dollar_at
, expanded_something
)
5239 int *contains_dollar_at
;
5240 int *expanded_something
;
5245 /* The intermediate string that we build while expanding. */
5248 /* The current size of the above object. */
5251 /* Index into ISTRING. */
5254 /* Temporary string storage. */
5257 /* The text of WORD. */
5258 register char *string
;
5260 /* The index into STRING. */
5263 /* This gets 1 if we see a $@ while quoted. */
5264 int quoted_dollar_at
;
5266 /* One of UNQUOTED, PARTIALLY_QUOTED, or WHOLLY_QUOTED, depending on
5267 whether WORD contains no quoting characters, a partially quoted
5268 string (e.g., "xx"ab), or is fully quoted (e.g., "xxab"). */
5271 int had_quoted_null
;
5275 register unsigned char c
; /* Current character. */
5277 int t_index
; /* For calls to string_extract_xxx. */
5282 istring
= (char *)xmalloc (istring_size
= DEFAULT_INITIAL_ARRAY_SIZE
);
5283 istring
[istring_index
= 0] = '\0';
5284 quoted_dollar_at
= had_quoted_null
= has_dollar_at
= 0;
5285 quoted_state
= UNQUOTED
;
5287 string
= word
->word
;
5289 goto finished_with_string
;
5291 if (contains_dollar_at
)
5292 *contains_dollar_at
= 0;
5294 /* Cache a bitmap of characters in IFS for quoting IFS characters that are
5295 not part of an expansion. POSIX.2 says this is a must. */
5297 bzero (ifscmap
, sizeof (ifscmap
));
5298 for (temp1
= temp
; temp1
&& *temp1
; temp1
++)
5300 /* This check compensates for what I think is a parsing problem -- the
5301 end brace matching algorithms for ${...} expressions differ between
5302 parse.y and subst.c. For instance, the parser passes
5303 ${abc:-G { I } K } as one word when it should be three. */
5304 if (*temp1
!= ' ' && *temp1
!= '\t' && *temp1
!= '\n')
5311 /* Begin the expansion. */
5317 /* Case on toplevel character. */
5321 goto finished_with_string
;
5324 temp
= (char *)xmalloc (3);
5326 temp
[1] = c
= string
[++sindex
];
5336 istring
= sub_append_string (temp
, istring
, &istring_index
, &istring_size
);
5342 #if defined (PROCESS_SUBSTITUTION)
5343 /* Process substitution. */
5347 if (string
[++sindex
] != LPAREN
|| (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || posixly_correct
)
5349 sindex
--; /* add_character: label increments sindex */
5353 t_index
= sindex
+ 1; /* skip past both '<' and LPAREN */
5355 temp1
= extract_process_subst (string
, (c
== '<') ? "<(" : ">(", &t_index
); /*))*/
5358 /* If the process substitution specification is `<()', we want to
5359 open the pipe for writing in the child and produce output; if
5360 it is `>()', we want to open the pipe for reading in the child
5361 and consume input. */
5362 temp
= temp1
? process_substitute (temp1
, (c
== '>')) : (char *)0;
5366 goto dollar_add_string
;
5368 #endif /* PROCESS_SUBSTITUTION */
5371 if (expanded_something
)
5372 *expanded_something
= 1;
5375 temp
= param_expand (string
, &sindex
, quoted
, expanded_something
,
5376 &has_dollar_at
, "ed_dollar_at
,
5377 &had_quoted_null
, 0);
5379 if (temp
== &expand_param_error
|| temp
== &expand_param_fatal
)
5383 return ((temp
== &expand_param_error
) ? &expand_word_error
5384 : &expand_word_fatal
);
5386 if (contains_dollar_at
&& has_dollar_at
)
5387 *contains_dollar_at
= 1;
5391 case '`': /* Backquoted command substitution. */
5395 if (expanded_something
)
5396 *expanded_something
= 1;
5398 temp
= string_extract (string
, &sindex
, "`", 0);
5399 de_backslash (temp
);
5400 temp1
= command_substitute (temp
, quoted
);
5403 goto dollar_add_string
;
5407 if (string
[sindex
+ 1] == '\n')
5413 c
= string
[++sindex
];
5415 if (quoted
& Q_HERE_DOCUMENT
)
5417 else if (quoted
& Q_DOUBLE_QUOTES
)
5423 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && ((sh_syntaxtab
[c
] & tflag
) == 0))
5431 sindex
--; /* add_character: label increments sindex */
5436 twochars
[0] = CTLESC
;
5442 /* BEFORE jumping here, we need to increment sindex if appropriate */
5443 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 2, istring_size
,
5444 DEFAULT_ARRAY_SIZE
);
5445 istring
[istring_index
++] = twochars
[0];
5446 istring
[istring_index
++] = twochars
[1];
5447 istring
[istring_index
] = '\0';
5452 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
|Q_NOQUOTE
))
5456 temp
= string_extract_double_quoted (string
, &sindex
, 0);
5458 /* If the quotes surrounded the entire string, then the
5459 whole word was quoted. */
5460 quoted_state
= (t_index
== 1 && string
[sindex
] == '\0')
5466 tword
= make_word (temp
); /* XXX */
5468 temp
= (char *)NULL
;
5471 list
= expand_word_internal (tword
, Q_DOUBLE_QUOTES
, 0, &has_dollar_at
, (int *)NULL
);
5473 if (list
== &expand_word_error
|| list
== &expand_word_fatal
)
5477 /* expand_word_internal has already freed temp_word->word
5478 for us because of the way it prints error messages. */
5479 tword
->word
= (char *)NULL
;
5480 dispose_word (tword
);
5484 dispose_word (tword
);
5486 /* "$@" (a double-quoted dollar-at) expands into nothing,
5487 not even a NULL word, when there are no positional
5489 if (list
== 0 && has_dollar_at
)
5495 /* If we get "$@", we know we have expanded something, so we
5496 need to remember it for the final split on $IFS. This is
5497 a special case; it's the only case where a quoted string
5498 can expand into more than one word. It's going to come back
5499 from the above call to expand_word_internal as a list with
5500 a single word, in which all characters are quoted and
5501 separated by blanks. What we want to do is to turn it back
5502 into a list for the next piece of code. */
5504 dequote_list (list
);
5509 if (contains_dollar_at
)
5510 *contains_dollar_at
= 1;
5511 if (expanded_something
)
5512 *expanded_something
= 1;
5517 /* What we have is "". This is a minor optimization. */
5519 list
= (WORD_LIST
*)NULL
;
5522 /* The code above *might* return a list (consider the case of "$@",
5523 where it returns "$1", "$2", etc.). We can't throw away the
5524 rest of the list, and we have to make sure each word gets added
5525 as quoted. We test on tresult->next: if it is non-NULL, we
5526 quote the whole list, save it to a string with string_list, and
5527 add that string. We don't need to quote the results of this
5528 (and it would be wrong, since that would quote the separators
5529 as well), so we go directly to add_string. */
5534 /* Testing quoted_dollar_at makes sure that "$@" is
5535 split correctly when $IFS does not contain a space. */
5536 temp
= quoted_dollar_at
5537 ? string_list_dollar_at (list
, Q_DOUBLE_QUOTES
)
5538 : string_list (quote_list (list
));
5539 dispose_words (list
);
5544 temp
= savestring (list
->word
->word
);
5545 dispose_words (list
);
5547 /* If the string is not a quoted null string, we want
5548 to remove any embedded unquoted CTLNUL characters.
5549 We do not want to turn quoted null strings back into
5550 the empty string, though. We do this because we
5551 want to remove any quoted nulls from expansions that
5552 contain other characters. For example, if we have
5553 x"$*"y or "x$*y" and there are no positional parameters,
5554 the $* should expand into nothing. */
5555 if (QUOTED_NULL (temp
) == 0)
5556 remove_quoted_nulls (temp
); /* XXX */
5561 temp
= (char *)NULL
;
5563 /* We do not want to add quoted nulls to strings that are only
5564 partially quoted; we can throw them away. */
5565 if (temp
== 0 && quoted_state
== PARTIALLY_QUOTED
)
5573 temp
= quote_string (temp
);
5581 sindex
--; /* add_character: label increments sindex */
5588 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
|Q_NOQUOTE
))
5592 temp
= string_extract_single_quoted (string
, &sindex
);
5594 /* If the entire STRING was surrounded by single quotes,
5595 then the string is wholly quoted. */
5596 quoted_state
= (t_index
== 1 && string
[sindex
] == '\0')
5600 /* If all we had was '', it is a null expansion. */
5604 temp
= (char *)NULL
;
5607 remove_quoted_escapes (temp
);
5609 /* We do not want to add quoted nulls to strings that are only
5610 partially quoted; such nulls are discarded. */
5611 if (temp
== 0 && (quoted_state
== PARTIALLY_QUOTED
))
5614 /* If we have a quoted null expansion, add a quoted NULL to istring. */
5618 sindex
--; /* add_character: label increments sindex */
5622 goto add_quoted_string
;
5627 /* This is the fix for " $@ " */
5628 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || (isexp
== 0 && ifscmap
[c
]))
5630 if (string
[sindex
]) /* from old goto dollar_add_string */
5639 twochars
[0] = CTLESC
;
5646 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 1, istring_size
,
5647 DEFAULT_ARRAY_SIZE
);
5648 istring
[istring_index
++] = c
;
5649 istring
[istring_index
] = '\0';
5651 /* Next character. */
5656 finished_with_string
:
5657 /* OK, we're ready to return. If we have a quoted string, and
5658 quoted_dollar_at is not set, we do no splitting at all; otherwise
5659 we split on ' '. The routines that call this will handle what to
5660 do if nothing has been expanded. */
5662 /* Partially and wholly quoted strings which expand to the empty
5663 string are retained as an empty arguments. Unquoted strings
5664 which expand to the empty string are discarded. The single
5665 exception is the case of expanding "$@" when there are no
5666 positional parameters. In that case, we discard the expansion. */
5668 /* Because of how the code that handles "" and '' in partially
5669 quoted strings works, we need to make ISTRING into a QUOTED_NULL
5670 if we saw quoting characters, but the expansion was empty.
5671 "" and '' are tossed away before we get to this point when
5672 processing partially quoted strings. This makes "" and $xxx""
5673 equivalent when xxx is unset. We also look to see whether we
5674 saw a quoted null from a ${} expansion and add one back if we
5677 /* If we expand to nothing and there were no single or double quotes
5678 in the word, we throw it away. Otherwise, we return a NULL word.
5679 The single exception is for $@ surrounded by double quotes when
5680 there are no positional parameters. In that case, we also throw
5683 if (*istring
== '\0')
5685 if (quoted_dollar_at
== 0 && (had_quoted_null
|| quoted_state
== PARTIALLY_QUOTED
))
5687 istring
[0] = CTLNUL
;
5689 tword
= make_bare_word (istring
);
5690 list
= make_word_list (tword
, (WORD_LIST
*)NULL
);
5691 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
5692 tword
->flags
|= W_QUOTED
;
5694 /* According to sh, ksh, and Posix.2, if a word expands into nothing
5695 and a double-quoted "$@" appears anywhere in it, then the entire
5697 else if (quoted_state
== UNQUOTED
|| quoted_dollar_at
)
5698 list
= (WORD_LIST
*)NULL
;
5702 tword
= make_bare_word (istring
);
5703 list
= make_word_list (tword
, (WORD_LIST
*)NULL
);
5704 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
5705 tword
->flags
|= W_QUOTED
;
5709 list
= (WORD_LIST
*)NULL
;
5712 else if (word
->flags
& W_NOSPLIT
)
5714 tword
= make_bare_word (istring
);
5715 list
= make_word_list (tword
, (WORD_LIST
*)NULL
);
5716 if (word
->flags
& W_ASSIGNMENT
)
5717 tword
->flags
|= W_ASSIGNMENT
; /* XXX */
5718 if (word
->flags
& W_NOGLOB
)
5719 tword
->flags
|= W_NOGLOB
; /* XXX */
5720 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
5721 tword
->flags
|= W_QUOTED
;
5727 ifs_chars
= (quoted_dollar_at
|| has_dollar_at
) ? getifs () : (char *)NULL
;
5729 /* If we have $@, we need to split the results no matter what. If
5730 IFS is unset or NULL, string_list_dollar_at has separated the
5731 positional parameters with a space, so we split on space (we have
5732 set ifs_chars to " \t\n" above if ifs is unset). If IFS is set,
5733 string_list_dollar_at has separated the positional parameters
5734 with the first character of $IFS, so we split on $IFS. */
5735 if (has_dollar_at
&& ifs_chars
)
5736 list
= list_string (istring
, *ifs_chars
? ifs_chars
: " ", 1);
5739 tword
= make_bare_word (istring
);
5740 list
= make_word_list (tword
, (WORD_LIST
*)NULL
);
5741 if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) || (quoted_state
== WHOLLY_QUOTED
))
5742 tword
->flags
|= W_QUOTED
;
5743 if (word
->flags
& W_ASSIGNMENT
)
5744 tword
->flags
|= W_ASSIGNMENT
;
5745 if (word
->flags
& W_NOGLOB
)
5746 tword
->flags
|= W_NOGLOB
;
5754 /* **************************************************************** */
5756 /* Functions for Quote Removal */
5758 /* **************************************************************** */
5760 /* Perform quote removal on STRING. If QUOTED > 0, assume we are obeying the
5761 backslash quoting rules for within double quotes. */
5763 string_quote_removal (string
, quoted
)
5767 char *r
, *result_string
, *temp
;
5768 int sindex
, tindex
, dquote
;
5771 /* The result can be no longer than the original string. */
5772 r
= result_string
= (char *)xmalloc (strlen (string
) + 1);
5774 for (dquote
= sindex
= 0; c
= string
[sindex
];)
5779 c
= string
[++sindex
];
5780 if (((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || dquote
) && (sh_syntaxtab
[c
] & CBSDQUOTE
) == 0)
5790 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || dquote
)
5796 tindex
= sindex
+ 1;
5797 temp
= string_extract_single_quoted (string
, &tindex
);
5808 dquote
= 1 - dquote
;
5814 return (result_string
);
5819 /* Perform quote removal on word WORD. This allocates and returns a new
5822 word_quote_removal (word
, quoted
)
5829 t
= string_quote_removal (word
->word
, quoted
);
5830 w
= make_bare_word (t
);
5835 /* Perform quote removal on all words in LIST. If QUOTED is non-zero,
5836 the members of the list are treated as if they are surrounded by
5837 double quotes. Return a new list, or NULL if LIST is NULL. */
5839 word_list_quote_removal (list
, quoted
)
5843 WORD_LIST
*result
, *t
, *tresult
;
5845 for (t
= list
, result
= (WORD_LIST
*)NULL
; t
; t
= t
->next
)
5847 tresult
= (WORD_LIST
*)xmalloc (sizeof (WORD_LIST
));
5848 tresult
->word
= word_quote_removal (t
->word
, quoted
);
5849 tresult
->next
= (WORD_LIST
*)NULL
;
5850 result
= (WORD_LIST
*) list_append (result
, tresult
);
5856 /*******************************************
5858 * Functions to perform word splitting *
5860 *******************************************/
5867 ifs
= find_variable ("IFS");
5868 /* If IFS is unset, it defaults to " \t\n". */
5869 return (ifs
? value_cell (ifs
) : " \t\n");
5872 /* This splits a single word into a WORD LIST on $IFS, but only if the word
5873 is not quoted. list_string () performs quote removal for us, even if we
5874 don't do any splitting. */
5885 ifs
= find_variable ("IFS");
5886 /* If IFS is unset, it defaults to " \t\n". */
5887 ifs_chars
= ifs
? value_cell (ifs
) : " \t\n";
5889 if ((w
->flags
& W_QUOTED
) || !ifs_chars
)
5892 result
= list_string (w
->word
, ifs_chars
, w
->flags
& W_QUOTED
);
5894 if (ifs
&& tempvar_p (ifs
)) /* XXX */
5895 dispose_variable (ifs
); /* XXX */
5898 result
= (WORD_LIST
*)NULL
;
5903 /* Perform word splitting on LIST and return the RESULT. It is possible
5904 to return (WORD_LIST *)NULL. */
5906 word_list_split (list
)
5909 WORD_LIST
*result
, *t
, *tresult
;
5911 for (t
= list
, result
= (WORD_LIST
*)NULL
; t
; t
= t
->next
)
5913 tresult
= word_split (t
->word
);
5914 result
= (WORD_LIST
*) list_append (result
, tresult
);
5919 /**************************************************
5921 * Functions to expand an entire WORD_LIST *
5923 **************************************************/
5925 /* Put NLIST (which is a WORD_LIST * of only one element) at the front of
5926 ELIST, and set ELIST to the new list. */
5927 #define PREPEND_LIST(nlist, elist) \
5928 do { nlist->next = elist; elist = nlist; } while (0)
5930 /* Separate out any initial variable assignments from TLIST. If set -k has
5931 been executed, remove all assignment statements from TLIST. Initial
5932 variable assignments and other environment assignments are placed
5933 on SUBST_ASSIGN_VARLIST. */
5935 separate_out_assignments (tlist
)
5938 register WORD_LIST
*vp
, *lp
;
5941 return ((WORD_LIST
*)NULL
);
5943 if (subst_assign_varlist
)
5944 dispose_words (subst_assign_varlist
); /* Clean up after previous error */
5946 subst_assign_varlist
= (WORD_LIST
*)NULL
;
5949 /* Separate out variable assignments at the start of the command.
5950 Loop invariant: vp->next == lp
5952 lp = list of words left after assignment statements skipped
5953 tlist = original list of words
5955 while (lp
&& (lp
->word
->flags
& W_ASSIGNMENT
))
5961 /* If lp != tlist, we have some initial assignment statements.
5962 We make SUBST_ASSIGN_VARLIST point to the list of assignment
5963 words and TLIST point to the remaining words. */
5966 subst_assign_varlist
= tlist
;
5967 /* ASSERT(vp->next == lp); */
5968 vp
->next
= (WORD_LIST
*)NULL
; /* terminate variable list */
5969 tlist
= lp
; /* remainder of word list */
5972 /* vp == end of variable list */
5973 /* tlist == remainder of original word list without variable assignments */
5975 /* All the words in tlist were assignment statements */
5976 return ((WORD_LIST
*)NULL
);
5978 /* ASSERT(tlist != NULL); */
5979 /* ASSERT((tlist->word->flags & W_ASSIGNMENT) == 0); */
5981 /* If the -k option is in effect, we need to go through the remaining
5982 words, separate out the assignment words, and place them on
5983 SUBST_ASSIGN_VARLIST. */
5984 if (place_keywords_in_env
)
5986 WORD_LIST
*tp
; /* tp == running pointer into tlist */
5991 /* Loop Invariant: tp->next == lp */
5992 /* Loop postcondition: tlist == word list without assignment statements */
5995 if (lp
->word
->flags
& W_ASSIGNMENT
)
5997 /* Found an assignment statement, add this word to end of
5998 subst_assign_varlist (vp). */
5999 if (!subst_assign_varlist
)
6000 subst_assign_varlist
= vp
= lp
;
6007 /* Remove the word pointed to by LP from TLIST. */
6008 tp
->next
= lp
->next
;
6009 /* ASSERT(vp == lp); */
6010 lp
->next
= (WORD_LIST
*)NULL
;
6023 #define WEXP_VARASSIGN 0x001
6024 #define WEXP_BRACEEXP 0x002
6025 #define WEXP_TILDEEXP 0x004
6026 #define WEXP_PARAMEXP 0x008
6027 #define WEXP_PATHEXP 0x010
6029 /* All of the expansions, including variable assignments at the start of
6031 #define WEXP_ALL (WEXP_VARASSIGN|WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
6033 /* All of the expansions except variable assignments at the start of
6035 #define WEXP_NOVARS (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
6037 /* All of the `shell expansions': brace expansion, tilde expansion, parameter
6038 expansion, command substitution, arithmetic expansion, word splitting, and
6040 #define WEXP_SHELLEXP (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP)
6042 /* Take the list of words in LIST and do the various substitutions. Return
6043 a new list of words which is the expanded list, and without things like
6044 variable assignments. */
6050 return (expand_word_list_internal (list
, WEXP_ALL
));
6053 /* Same as expand_words (), but doesn't hack variable or environment
6056 expand_words_no_vars (list
)
6059 return (expand_word_list_internal (list
, WEXP_NOVARS
));
6063 expand_words_shellexp (list
)
6066 return (expand_word_list_internal (list
, WEXP_SHELLEXP
));
6070 glob_expand_word_list (tlist
, eflags
)
6074 char **glob_array
, *temp_string
;
6075 register int glob_index
;
6076 WORD_LIST
*glob_list
, *output_list
, *disposables
, *next
;
6079 output_list
= disposables
= (WORD_LIST
*)NULL
;
6080 glob_array
= (char **)NULL
;
6083 /* For each word, either globbing is attempted or the word is
6084 added to orig_list. If globbing succeeds, the results are
6085 added to orig_list and the word (tlist) is added to the list
6086 of disposable words. If globbing fails and failed glob
6087 expansions are left unchanged (the shell default), the
6088 original word is added to orig_list. If globbing fails and
6089 failed glob expansions are removed, the original word is
6090 added to the list of disposable words. orig_list ends up
6091 in reverse order and requires a call to reverse_list to
6092 be set right. After all words are examined, the disposable
6096 /* If the word isn't an assignment and contains an unquoted
6097 pattern matching character, then glob it. */
6098 if ((tlist
->word
->flags
& W_NOGLOB
) == 0 &&
6099 unquoted_glob_pattern_p (tlist
->word
->word
))
6101 glob_array
= shell_glob_filename (tlist
->word
->word
);
6103 /* Handle error cases.
6104 I don't think we should report errors like "No such file
6105 or directory". However, I would like to report errors
6106 like "Read failed". */
6108 if (GLOB_FAILED (glob_array
))
6110 glob_array
= (char **)xmalloc (sizeof (char *));
6111 glob_array
[0] = (char *)NULL
;
6114 /* Dequote the current word in case we have to use it. */
6115 if (glob_array
[0] == NULL
)
6117 temp_string
= dequote_string (tlist
->word
->word
);
6118 free (tlist
->word
->word
);
6119 tlist
->word
->word
= temp_string
;
6122 /* Make the array into a word list. */
6123 glob_list
= (WORD_LIST
*)NULL
;
6124 for (glob_index
= 0; glob_array
[glob_index
]; glob_index
++)
6126 tword
= make_bare_word (glob_array
[glob_index
]);
6127 tword
->flags
|= W_GLOBEXP
; /* XXX */
6128 glob_list
= make_word_list (tword
, glob_list
);
6133 output_list
= (WORD_LIST
*)list_append (glob_list
, output_list
);
6134 PREPEND_LIST (tlist
, disposables
);
6136 else if (allow_null_glob_expansion
== 0)
6138 /* Failed glob expressions are left unchanged. */
6139 PREPEND_LIST (tlist
, output_list
);
6143 /* Failed glob expressions are removed. */
6144 PREPEND_LIST (tlist
, disposables
);
6149 /* Dequote the string. */
6150 temp_string
= dequote_string (tlist
->word
->word
);
6151 free (tlist
->word
->word
);
6152 tlist
->word
->word
= temp_string
;
6153 PREPEND_LIST (tlist
, output_list
);
6156 free_array (glob_array
);
6157 glob_array
= (char **)NULL
;
6163 dispose_words (disposables
);
6166 output_list
= REVERSE_LIST (output_list
, WORD_LIST
*);
6168 return (output_list
);
6171 #if defined (BRACE_EXPANSION)
6173 brace_expand_word_list (tlist
, eflags
)
6177 register char **expansions
;
6179 WORD_LIST
*disposables
, *output_list
, *next
;
6183 for (disposables
= output_list
= (WORD_LIST
*)NULL
; tlist
; tlist
= next
)
6187 /* Only do brace expansion if the word has a brace character. If
6188 not, just add the word list element to BRACES and continue. In
6189 the common case, at least when running shell scripts, this will
6190 degenerate to a bunch of calls to `strchr', and then what is
6191 basically a reversal of TLIST into BRACES, which is corrected
6192 by a call to reverse_list () on BRACES when the end of TLIST
6194 if (strchr (tlist
->word
->word
, LBRACE
))
6196 expansions
= brace_expand (tlist
->word
->word
);
6198 for (eindex
= 0; temp_string
= expansions
[eindex
]; eindex
++)
6200 w
= make_word (temp_string
);
6201 /* If brace expansion didn't change the word, preserve
6202 the flags. We may want to preserve the flags
6203 unconditionally someday -- XXX */
6204 if (STREQ (temp_string
, tlist
->word
->word
))
6205 w
->flags
= tlist
->word
->flags
;
6206 output_list
= make_word_list (w
, output_list
);
6207 free (expansions
[eindex
]);
6211 /* Add TLIST to the list of words to be freed after brace
6212 expansion has been performed. */
6213 PREPEND_LIST (tlist
, disposables
);
6216 PREPEND_LIST (tlist
, output_list
);
6220 dispose_words (disposables
);
6223 output_list
= REVERSE_LIST (output_list
, WORD_LIST
*);
6225 return (output_list
);
6230 shell_expand_word_list (tlist
, eflags
)
6234 WORD_LIST
*expanded
, *orig_list
, *new_list
, *next
, *temp_list
;
6235 int expanded_something
, has_dollar_at
;
6238 /* We do tilde expansion all the time. This is what 1003.2 says. */
6239 new_list
= (WORD_LIST
*)NULL
;
6240 for (orig_list
= tlist
; tlist
; tlist
= next
)
6242 temp_string
= tlist
->word
->word
;
6246 /* Posix.2 section 3.6.1 says that tildes following `=' in words
6247 which are not assignment statements are not expanded. We do
6248 this only if POSIXLY_CORRECT is enabled. Essentially, we do
6249 tilde expansion on unquoted assignment statements (flags include
6250 W_ASSIGNMENT but not W_QUOTED). */
6251 if (temp_string
[0] == '~' ||
6252 (((tlist
->word
->flags
& (W_ASSIGNMENT
|W_QUOTED
)) == W_ASSIGNMENT
) &&
6253 posixly_correct
== 0 &&
6254 strchr (temp_string
, '~') &&
6255 (unquoted_substring ("=~", temp_string
) || unquoted_substring (":~", temp_string
))))
6257 tlist
->word
->word
= bash_tilde_expand (temp_string
);
6261 expanded_something
= 0;
6262 expanded
= expand_word_internal
6263 (tlist
->word
, 0, 0, &has_dollar_at
, &expanded_something
);
6265 if (expanded
== &expand_word_error
|| expanded
== &expand_word_fatal
)
6267 /* By convention, each time this error is returned,
6268 tlist->word->word has already been freed. */
6269 tlist
->word
->word
= (char *)NULL
;
6271 /* Dispose our copy of the original list. */
6272 dispose_words (orig_list
);
6273 /* Dispose the new list we're building. */
6274 dispose_words (new_list
);
6276 last_command_exit_value
= EXECUTION_FAILURE
;
6277 if (expanded
== &expand_word_error
)
6278 jump_to_top_level (DISCARD
);
6280 jump_to_top_level (FORCE_EOF
);
6283 /* Don't split words marked W_NOSPLIT. */
6284 if (expanded_something
&& (tlist
->word
->flags
& W_NOSPLIT
) == 0)
6286 temp_list
= word_list_split (expanded
);
6287 dispose_words (expanded
);
6291 /* If no parameter expansion, command substitution, process
6292 substitution, or arithmetic substitution took place, then
6293 do not do word splitting. We still have to remove quoted
6294 null characters from the result. */
6295 word_list_remove_quoted_nulls (expanded
);
6296 temp_list
= expanded
;
6299 expanded
= REVERSE_LIST (temp_list
, WORD_LIST
*);
6300 new_list
= (WORD_LIST
*)list_append (expanded
, new_list
);
6304 dispose_words (orig_list
);
6307 new_list
= REVERSE_LIST (new_list
, WORD_LIST
*);
6312 /* The workhorse for expand_words () and expand_words_no_vars ().
6313 First arg is LIST, a WORD_LIST of words.
6314 Second arg EFLAGS is a flags word controlling which expansions are
6317 This does all of the substitutions: brace expansion, tilde expansion,
6318 parameter expansion, command substitution, arithmetic expansion,
6319 process substitution, word splitting, and pathname expansion, according
6320 to the bits set in EFLAGS. Words with the W_QUOTED or W_NOSPLIT bits
6321 set, or for which no expansion is done, do not undergo word splitting.
6322 Words with the W_NOGLOB bit set do not undergo pathname expansion. */
6324 expand_word_list_internal (list
, eflags
)
6328 WORD_LIST
*new_list
, *temp_list
;
6332 return ((WORD_LIST
*)NULL
);
6334 garglist
= new_list
= copy_word_list (list
);
6335 if (eflags
& WEXP_VARASSIGN
)
6337 garglist
= new_list
= separate_out_assignments (new_list
);
6340 if (subst_assign_varlist
)
6342 /* All the words were variable assignments, so they are placed
6343 into the shell's environment. */
6344 for (temp_list
= subst_assign_varlist
; temp_list
; temp_list
= temp_list
->next
)
6346 this_command_name
= (char *)NULL
; /* no arithmetic errors */
6347 tint
= do_assignment (temp_list
->word
->word
);
6348 /* Variable assignment errors in non-interactive shells
6349 running in Posix.2 mode cause the shell to exit. */
6352 last_command_exit_value
= EXECUTION_FAILURE
;
6353 if (interactive_shell
== 0 && posixly_correct
)
6354 jump_to_top_level (FORCE_EOF
);
6356 jump_to_top_level (DISCARD
);
6359 dispose_words (subst_assign_varlist
);
6360 subst_assign_varlist
= (WORD_LIST
*)NULL
;
6362 return ((WORD_LIST
*)NULL
);
6366 /* Begin expanding the words that remain. The expansions take place on
6367 things that aren't really variable assignments. */
6369 #if defined (BRACE_EXPANSION)
6370 /* Do brace expansion on this word if there are any brace characters
6372 if ((eflags
& WEXP_BRACEEXP
) && brace_expansion
&& new_list
)
6373 new_list
= brace_expand_word_list (new_list
, eflags
);
6374 #endif /* BRACE_EXPANSION */
6376 /* Perform the `normal' shell expansions: tilde expansion, parameter and
6377 variable substitution, command substitution, arithmetic expansion,
6378 and word splitting. */
6379 new_list
= shell_expand_word_list (new_list
, eflags
);
6381 /* Okay, we're almost done. Now let's just do some filename
6385 if ((eflags
& WEXP_PATHEXP
) && disallow_filename_globbing
== 0)
6386 /* Glob expand the word list unless globbing has been disabled. */
6387 new_list
= glob_expand_word_list (new_list
, eflags
);
6389 /* Dequote the words, because we're not performing globbing. */
6390 new_list
= dequote_list (new_list
);
6393 if ((eflags
& WEXP_VARASSIGN
) && subst_assign_varlist
)
6395 sh_assign_func_t
*assign_func
;
6397 /* If the remainder of the words expand to nothing, Posix.2 requires
6398 that the variable and environment assignments affect the shell's
6400 assign_func
= new_list
? assign_in_env
: do_assignment
;
6402 for (temp_list
= subst_assign_varlist
; temp_list
; temp_list
= temp_list
->next
)
6404 this_command_name
= (char *)NULL
;
6405 tint
= (*assign_func
) (temp_list
->word
->word
);
6406 /* Variable assignment errors in non-interactive shells running
6407 in Posix.2 mode cause the shell to exit. */
6408 if (tint
== 0 && assign_func
== do_assignment
)
6410 last_command_exit_value
= EXECUTION_FAILURE
;
6411 if (interactive_shell
== 0 && posixly_correct
)
6412 jump_to_top_level (FORCE_EOF
);
6414 jump_to_top_level (DISCARD
);
6418 dispose_words (subst_assign_varlist
);
6419 subst_assign_varlist
= (WORD_LIST
*)NULL
;
6423 tint
= list_length (new_list
) + 1;
6424 RESIZE_MALLOCED_BUFFER (glob_argv_flags
, 0, tint
, glob_argv_flags_size
, 16);
6425 for (tint
= 0, temp_list
= new_list
; temp_list
; temp_list
= temp_list
->next
)
6426 glob_argv_flags
[tint
++] = (temp_list
->word
->flags
& W_GLOBEXP
) ? '1' : '0';
6427 glob_argv_flags
[tint
] = '\0';