1 /* subst.h -- Names of externally visible functions in subst.c. */
3 /* Copyright (C) 1993-2015 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
21 #if !defined (_SUBST_H_)
26 /* Constants which specify how to handle backslashes and quoting in
27 expand_word_internal (). Q_DOUBLE_QUOTES means to use the function
28 slashify_in_quotes () to decide whether the backslash should be
29 retained. Q_HERE_DOCUMENT means slashify_in_here_document () to
30 decide whether to retain the backslash. Q_KEEP_BACKSLASH means
31 to unconditionally retain the backslash. Q_PATQUOTE means that we're
32 expanding a pattern ${var%#[#%]pattern} in an expansion surrounded
33 by double quotes. Q_DOLBRACE means we are expanding a ${...} word, so
34 backslashes should also escape { and } and be removed. */
35 #define Q_DOUBLE_QUOTES 0x001
36 #define Q_HERE_DOCUMENT 0x002
37 #define Q_KEEP_BACKSLASH 0x004
38 #define Q_PATQUOTE 0x008
39 #define Q_QUOTED 0x010
40 #define Q_ADDEDQUOTES 0x020
41 #define Q_QUOTEDNULL 0x040
42 #define Q_DOLBRACE 0x080
43 #define Q_ARITH 0x100 /* expanding string for arithmetic evaluation */
44 #define Q_ARRAYSUB 0x200 /* expanding indexed array subscript */
46 /* Flag values controlling how assignment statements are treated. */
47 #define ASS_APPEND 0x0001
48 #define ASS_MKLOCAL 0x0002
49 #define ASS_MKASSOC 0x0004
50 #define ASS_MKGLOBAL 0x0008 /* force global assignment */
51 #define ASS_NAMEREF 0x0010 /* assigning to nameref variable */
52 #define ASS_FORCE 0x0020 /* force assignment even to readonly variable */
54 /* Flags for the string extraction functions. */
55 #define SX_NOALLOC 0x0001 /* just skip; don't return substring */
56 #define SX_VARNAME 0x0002 /* variable name; for string_extract () */
57 #define SX_REQMATCH 0x0004 /* closing/matching delimiter required */
58 #define SX_COMMAND 0x0008 /* extracting a shell script/command */
59 #define SX_NOCTLESC 0x0010 /* don't honor CTLESC quoting */
60 #define SX_NOESCCTLNUL 0x0020 /* don't let CTLESC quote CTLNUL */
61 #define SX_NOLONGJMP 0x0040 /* don't longjmp on fatal error */
62 #define SX_ARITHSUB 0x0080 /* extracting $(( ... )) (currently unused) */
63 #define SX_POSIXEXP 0x0100 /* extracting new Posix pattern removal expansions in extract_dollar_brace_string */
64 #define SX_WORD 0x0200 /* extracting word in ${param op word} */
65 #define SX_COMPLETE 0x0400 /* extracting word for completion */
66 #define SX_STRIPDQ 0x0800 /* strip double quotes when extracting double-quoted string */
68 /* Remove backslashes which are quoting backquotes from STRING. Modifies
69 STRING, and returns a pointer to it. */
70 extern char * de_backslash
__P((char *));
72 /* Replace instances of \! in a string with !. */
73 extern void unquote_bang
__P((char *));
75 /* Extract the $( construct in STRING, and return a new string.
76 Start extracting at (SINDEX) as if we had just seen "$(".
77 Make (SINDEX) get the position just after the matching ")".
78 XFLAGS is additional flags to pass to other extraction functions, */
79 extern char *extract_command_subst
__P((char *, int *, int));
81 /* Extract the $[ construct in STRING, and return a new string.
82 Start extracting at (SINDEX) as if we had just seen "$[".
83 Make (SINDEX) get the position just after the matching "]". */
84 extern char *extract_arithmetic_subst
__P((char *, int *));
86 #if defined (PROCESS_SUBSTITUTION)
87 /* Extract the <( or >( construct in STRING, and return a new string.
88 Start extracting at (SINDEX) as if we had just seen "<(".
89 Make (SINDEX) get the position just after the matching ")". */
90 extern char *extract_process_subst
__P((char *, char *, int *, int));
91 #endif /* PROCESS_SUBSTITUTION */
93 /* Extract the name of the variable to bind to from the assignment string. */
94 extern char *assignment_name
__P((char *));
96 /* Return a single string of all the words present in LIST, separating
97 each word with SEP. */
98 extern char *string_list_internal
__P((WORD_LIST
*, char *));
100 /* Return a single string of all the words present in LIST, separating
101 each word with a space. */
102 extern char *string_list
__P((WORD_LIST
*));
104 /* Turn $* into a single string, obeying POSIX rules. */
105 extern char *string_list_dollar_star
__P((WORD_LIST
*));
107 /* Expand $@ into a single string, obeying POSIX rules. */
108 extern char *string_list_dollar_at
__P((WORD_LIST
*, int, int));
110 /* Turn the positional paramters into a string, understanding quoting and
111 the various subtleties of using the first character of $IFS as the
112 separator. Calls string_list_dollar_at, string_list_dollar_star, and
113 string_list as appropriate. */
114 extern char *string_list_pos_params
__P((int, WORD_LIST
*, int));
116 /* Perform quoted null character removal on each element of LIST.
117 This modifies LIST. */
118 extern void word_list_remove_quoted_nulls
__P((WORD_LIST
*));
120 /* This performs word splitting and quoted null character removal on
122 extern WORD_LIST
*list_string
__P((char *, char *, int));
124 extern char *ifs_firstchar
__P((int *));
125 extern char *get_word_from_string
__P((char **, char *, char **));
126 extern char *strip_trailing_ifs_whitespace
__P((char *, char *, int));
128 /* Given STRING, an assignment string, get the value of the right side
129 of the `=', and bind it to the left side. If EXPAND is true, then
130 perform tilde expansion, parameter expansion, command substitution,
131 and arithmetic expansion on the right-hand side. Do not perform word
132 splitting on the result of expansion. */
133 extern int do_assignment
__P((char *));
134 extern int do_assignment_no_expand
__P((char *));
135 extern int do_word_assignment
__P((WORD_DESC
*, int));
137 /* Append SOURCE to TARGET at INDEX. SIZE is the current amount
138 of space allocated to TARGET. SOURCE can be NULL, in which
139 case nothing happens. Gets rid of SOURCE by free ()ing it.
140 Returns TARGET in case the location has changed. */
141 extern char *sub_append_string
__P((char *, char *, int *, size_t *));
143 /* Append the textual representation of NUMBER to TARGET.
144 INDEX and SIZE are as in SUB_APPEND_STRING. */
145 extern char *sub_append_number
__P((intmax_t, char *, int *, int *));
147 /* Return the word list that corresponds to `$*'. */
148 extern WORD_LIST
*list_rest_of_args
__P((void));
150 /* Make a single large string out of the dollar digit variables,
151 and the rest_of_args. If DOLLAR_STAR is 1, then obey the special
152 case of "$*" with respect to IFS. */
153 extern char *string_rest_of_args
__P((int));
155 extern int number_of_args
__P((void));
157 /* Expand STRING by performing parameter expansion, command substitution,
158 and arithmetic expansion. Dequote the resulting WORD_LIST before
159 returning it, but do not perform word splitting. The call to
160 remove_quoted_nulls () is made here because word splitting normally
161 takes care of quote removal. */
162 extern WORD_LIST
*expand_string_unsplit
__P((char *, int));
164 /* Expand the rhs of an assignment statement. */
165 extern WORD_LIST
*expand_string_assignment
__P((char *, int));
167 /* Expand a prompt string. */
168 extern WORD_LIST
*expand_prompt_string
__P((char *, int, int));
170 /* Expand STRING just as if you were expanding a word. This also returns
171 a list of words. Note that filename globbing is *NOT* done for word
172 or string expansion, just when the shell is expanding a command. This
173 does parameter expansion, command substitution, arithmetic expansion,
174 and word splitting. Dequote the resultant WORD_LIST before returning. */
175 extern WORD_LIST
*expand_string
__P((char *, int));
177 /* Convenience functions that expand strings to strings, taking care of
178 converting the WORD_LIST * returned by the expand_string* functions
179 to a string and deallocating the WORD_LIST *. */
180 extern char *expand_string_to_string
__P((char *, int));
181 extern char *expand_string_unsplit_to_string
__P((char *, int));
182 extern char *expand_assignment_string_to_string
__P((char *, int));
184 /* Expand an arithmetic expression string */
185 extern char *expand_arith_string
__P((char *, int));
187 /* De-quote quoted characters in STRING. */
188 extern char *dequote_string
__P((char *));
190 /* De-quote CTLESC-escaped CTLESC or CTLNUL characters in STRING. */
191 extern char *dequote_escapes
__P((char *));
193 /* De-quote quoted characters in each word in LIST. */
194 extern WORD_LIST
*dequote_list
__P((WORD_LIST
*));
196 /* Expand WORD, performing word splitting on the result. This does
197 parameter expansion, command substitution, arithmetic expansion,
198 word splitting, and quote removal. */
199 extern WORD_LIST
*expand_word
__P((WORD_DESC
*, int));
201 /* Expand WORD, but do not perform word splitting on the result. This
202 does parameter expansion, command substitution, arithmetic expansion,
203 and quote removal. */
204 extern WORD_LIST
*expand_word_unsplit
__P((WORD_DESC
*, int));
205 extern WORD_LIST
*expand_word_leave_quoted
__P((WORD_DESC
*, int));
207 /* Return the value of a positional parameter. This handles values > 10. */
208 extern char *get_dollar_var_value
__P((intmax_t));
210 /* Quote a string to protect it from word splitting. */
211 extern char *quote_string
__P((char *));
213 /* Quote escape characters (characters special to interals of expansion)
215 extern char *quote_escapes
__P((char *));
217 /* And remove such quoted special characters. */
218 extern char *remove_quoted_escapes
__P((char *));
220 /* Remove CTLNUL characters from STRING unless they are quoted with CTLESC. */
221 extern char *remove_quoted_nulls
__P((char *));
223 /* Perform quote removal on STRING. If QUOTED > 0, assume we are obeying the
224 backslash quoting rules for within double quotes. */
225 extern char *string_quote_removal
__P((char *, int));
227 /* Perform quote removal on word WORD. This allocates and returns a new
229 extern WORD_DESC
*word_quote_removal
__P((WORD_DESC
*, int));
231 /* Perform quote removal on all words in LIST. If QUOTED is non-zero,
232 the members of the list are treated as if they are surrounded by
233 double quotes. Return a new list, or NULL if LIST is NULL. */
234 extern WORD_LIST
*word_list_quote_removal
__P((WORD_LIST
*, int));
236 /* Called when IFS is changed to maintain some private variables. */
237 extern void setifs
__P((SHELL_VAR
*));
239 /* Return the value of $IFS, or " \t\n" if IFS is unset. */
240 extern char *getifs
__P((void));
242 /* This splits a single word into a WORD LIST on $IFS, but only if the word
243 is not quoted. list_string () performs quote removal for us, even if we
244 don't do any splitting. */
245 extern WORD_LIST
*word_split
__P((WORD_DESC
*, char *));
247 /* Take the list of words in LIST and do the various substitutions. Return
248 a new list of words which is the expanded list, and without things like
249 variable assignments. */
250 extern WORD_LIST
*expand_words
__P((WORD_LIST
*));
252 /* Same as expand_words (), but doesn't hack variable or environment
254 extern WORD_LIST
*expand_words_no_vars
__P((WORD_LIST
*));
256 /* Perform the `normal shell expansions' on a WORD_LIST. These are
257 brace expansion, tilde expansion, parameter and variable substitution,
258 command substitution, arithmetic expansion, and word splitting. */
259 extern WORD_LIST
*expand_words_shellexp
__P((WORD_LIST
*));
261 extern WORD_DESC
*command_substitute
__P((char *, int));
262 extern char *pat_subst
__P((char *, char *, char *, int));
264 extern int fifos_pending
__P((void));
265 extern int num_fifos
__P((void));
266 extern void unlink_fifo_list
__P((void));
267 extern void unlink_fifo
__P((int));
269 extern char *copy_fifo_list
__P((int *));
270 extern void unlink_new_fifos
__P((char *, int));
271 extern void close_new_fifos
__P((char *, int));
273 extern void clear_fifo_list
__P((void));
275 extern WORD_LIST
*list_string_with_quotes
__P((char *));
277 #if defined (ARRAY_VARS)
278 extern char *extract_array_assignment_list
__P((char *, int *));
281 #if defined (COND_COMMAND)
282 extern char *remove_backslashes
__P((char *));
283 extern char *cond_expand_word
__P((WORD_DESC
*, int));
286 /* Flags for skip_to_delim */
287 #define SD_NOJMP 0x001 /* don't longjmp on fatal error. */
288 #define SD_INVERT 0x002 /* look for chars NOT in passed set */
289 #define SD_NOQUOTEDELIM 0x004 /* don't let single or double quotes act as delimiters */
290 #define SD_NOSKIPCMD 0x008 /* don't skip over $(, <(, or >( command/process substitution; parse them as commands */
291 #define SD_EXTGLOB 0x010 /* skip over extended globbing patterns if appropriate */
292 #define SD_IGNOREQUOTE 0x020 /* single and double quotes are not special */
293 #define SD_GLOB 0x040 /* skip over glob patterns like bracket expressions */
294 #define SD_NOPROCSUB 0x080 /* don't parse process substitutions as commands */
295 #define SD_COMPLETE 0x100 /* skip_to_delim during completion */
296 #define SD_HISTEXP 0x200 /* skip_to_delim during history expansion */
297 #define SD_ARITHEXP 0x400 /* skip_to_delim during arithmetic expansion */
299 extern int skip_to_delim
__P((char *, int, char *, int));
301 #if defined (BANG_HISTORY)
302 extern int skip_to_histexp
__P((char *, int, char *, int));
305 #if defined (READLINE)
306 extern int char_is_quoted
__P((char *, int));
307 extern int unclosed_pair
__P((char *, int, char *));
308 extern WORD_LIST
*split_at_delims
__P((char *, int, char *, int, int, int *, int *));
311 /* Variables used to keep track of the characters in IFS. */
312 extern SHELL_VAR
*ifs_var
;
313 extern char *ifs_value
;
314 extern unsigned char ifs_cmap
[];
316 #if defined (HANDLE_MULTIBYTE)
317 extern unsigned char ifs_firstc
[];
318 extern size_t ifs_firstc_len
;
320 extern unsigned char ifs_firstc
;
323 /* Evaluates to 1 if C is a character in $IFS. */
324 #define isifs(c) (ifs_cmap[(unsigned char)(c)] != 0)
326 /* How to determine the quoted state of the character C. */
327 #define QUOTED_CHAR(c) ((c) == CTLESC)
329 /* Is the first character of STRING a quoted NULL character? */
330 #define QUOTED_NULL(string) ((string)[0] == CTLNUL && (string)[1] == '\0')
332 extern void invalidate_cached_quoted_dollar_at
__P((void));
334 #endif /* !_SUBST_H_ */