]>
Commit | Line | Data |
---|---|---|
726f6388 JA |
1 | /* subst.h -- Names of externally visible functions in subst.c. */ |
2 | ||
d233b485 | 3 | /* Copyright (C) 1993-2017 Free Software Foundation, Inc. |
726f6388 JA |
4 | |
5 | This file is part of GNU Bash, the Bourne Again SHell. | |
6 | ||
3185942a JA |
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. | |
726f6388 | 11 | |
3185942a JA |
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. | |
726f6388 | 16 | |
3185942a JA |
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/>. | |
19 | */ | |
726f6388 JA |
20 | |
21 | #if !defined (_SUBST_H_) | |
22 | #define _SUBST_H_ | |
23 | ||
24 | #include "stdc.h" | |
25 | ||
28ef6c31 JA |
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 | |
7117c2d2 JA |
31 | to unconditionally retain the backslash. Q_PATQUOTE means that we're |
32 | expanding a pattern ${var%#[#%]pattern} in an expansion surrounded | |
495aee44 CR |
33 | by double quotes. Q_DOLBRACE means we are expanding a ${...} word, so |
34 | backslashes should also escape { and } and be removed. */ | |
a0c0a00f CR |
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 */ | |
28ef6c31 | 45 | |
95732b49 | 46 | /* Flag values controlling how assignment statements are treated. */ |
ac50fbac CR |
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 */ | |
a0c0a00f | 52 | #define ASS_FORCE 0x0020 /* force assignment even to readonly variable */ |
d233b485 CR |
53 | #define ASS_CHKLOCAL 0x0040 /* check local variable before assignment */ |
54 | #define ASS_NOEXPAND 0x0080 /* don't expand associative array subscripts */ | |
55 | #define ASS_NOEVAL 0x0100 /* don't evaluate value as expression */ | |
56 | #define ASS_NOLONGJMP 0x0200 /* don't longjmp on fatal assignment error */ | |
712f80b0 | 57 | #define ASS_NOINVIS 0x0400 /* don't resolve local invisible variables */ |
3185942a JA |
58 | |
59 | /* Flags for the string extraction functions. */ | |
495aee44 CR |
60 | #define SX_NOALLOC 0x0001 /* just skip; don't return substring */ |
61 | #define SX_VARNAME 0x0002 /* variable name; for string_extract () */ | |
62 | #define SX_REQMATCH 0x0004 /* closing/matching delimiter required */ | |
63 | #define SX_COMMAND 0x0008 /* extracting a shell script/command */ | |
64 | #define SX_NOCTLESC 0x0010 /* don't honor CTLESC quoting */ | |
65 | #define SX_NOESCCTLNUL 0x0020 /* don't let CTLESC quote CTLNUL */ | |
66 | #define SX_NOLONGJMP 0x0040 /* don't longjmp on fatal error */ | |
67 | #define SX_ARITHSUB 0x0080 /* extracting $(( ... )) (currently unused) */ | |
68 | #define SX_POSIXEXP 0x0100 /* extracting new Posix pattern removal expansions in extract_dollar_brace_string */ | |
49ed961b | 69 | #define SX_WORD 0x0200 /* extracting word in ${param op word} */ |
a0c0a00f CR |
70 | #define SX_COMPLETE 0x0400 /* extracting word for completion */ |
71 | #define SX_STRIPDQ 0x0800 /* strip double quotes when extracting double-quoted string */ | |
95732b49 | 72 | |
726f6388 JA |
73 | /* Remove backslashes which are quoting backquotes from STRING. Modifies |
74 | STRING, and returns a pointer to it. */ | |
712f80b0 | 75 | extern char * de_backslash PARAMS((char *)); |
726f6388 JA |
76 | |
77 | /* Replace instances of \! in a string with !. */ | |
712f80b0 | 78 | extern void unquote_bang PARAMS((char *)); |
726f6388 JA |
79 | |
80 | /* Extract the $( construct in STRING, and return a new string. | |
81 | Start extracting at (SINDEX) as if we had just seen "$(". | |
3185942a JA |
82 | Make (SINDEX) get the position just after the matching ")". |
83 | XFLAGS is additional flags to pass to other extraction functions, */ | |
712f80b0 | 84 | extern char *extract_command_subst PARAMS((char *, int *, int)); |
726f6388 JA |
85 | |
86 | /* Extract the $[ construct in STRING, and return a new string. | |
87 | Start extracting at (SINDEX) as if we had just seen "$[". | |
88 | Make (SINDEX) get the position just after the matching "]". */ | |
712f80b0 | 89 | extern char *extract_arithmetic_subst PARAMS((char *, int *)); |
726f6388 JA |
90 | |
91 | #if defined (PROCESS_SUBSTITUTION) | |
92 | /* Extract the <( or >( construct in STRING, and return a new string. | |
93 | Start extracting at (SINDEX) as if we had just seen "<(". | |
94 | Make (SINDEX) get the position just after the matching ")". */ | |
712f80b0 | 95 | extern char *extract_process_subst PARAMS((char *, char *, int *, int)); |
726f6388 JA |
96 | #endif /* PROCESS_SUBSTITUTION */ |
97 | ||
98 | /* Extract the name of the variable to bind to from the assignment string. */ | |
712f80b0 | 99 | extern char *assignment_name PARAMS((char *)); |
726f6388 | 100 | |
7117c2d2 JA |
101 | /* Return a single string of all the words present in LIST, separating |
102 | each word with SEP. */ | |
712f80b0 | 103 | extern char *string_list_internal PARAMS((WORD_LIST *, char *)); |
7117c2d2 | 104 | |
726f6388 JA |
105 | /* Return a single string of all the words present in LIST, separating |
106 | each word with a space. */ | |
712f80b0 | 107 | extern char *string_list PARAMS((WORD_LIST *)); |
726f6388 | 108 | |
f73dda09 | 109 | /* Turn $* into a single string, obeying POSIX rules. */ |
712f80b0 | 110 | extern char *string_list_dollar_star PARAMS((WORD_LIST *, int, int)); |
f73dda09 JA |
111 | |
112 | /* Expand $@ into a single string, obeying POSIX rules. */ | |
712f80b0 | 113 | extern char *string_list_dollar_at PARAMS((WORD_LIST *, int, int)); |
f73dda09 | 114 | |
712f80b0 | 115 | /* Turn the positional parameters into a string, understanding quoting and |
3185942a JA |
116 | the various subtleties of using the first character of $IFS as the |
117 | separator. Calls string_list_dollar_at, string_list_dollar_star, and | |
118 | string_list as appropriate. */ | |
712f80b0 | 119 | extern char *string_list_pos_params PARAMS((int, WORD_LIST *, int, int)); |
3185942a | 120 | |
726f6388 JA |
121 | /* Perform quoted null character removal on each element of LIST. |
122 | This modifies LIST. */ | |
712f80b0 | 123 | extern void word_list_remove_quoted_nulls PARAMS((WORD_LIST *)); |
726f6388 JA |
124 | |
125 | /* This performs word splitting and quoted null character removal on | |
126 | STRING. */ | |
712f80b0 | 127 | extern WORD_LIST *list_string PARAMS((char *, char *, int)); |
726f6388 | 128 | |
712f80b0 CR |
129 | extern char *ifs_firstchar PARAMS((int *)); |
130 | extern char *get_word_from_string PARAMS((char **, char *, char **)); | |
131 | extern char *strip_trailing_ifs_whitespace PARAMS((char *, char *, int)); | |
726f6388 JA |
132 | |
133 | /* Given STRING, an assignment string, get the value of the right side | |
134 | of the `=', and bind it to the left side. If EXPAND is true, then | |
95732b49 JA |
135 | perform tilde expansion, parameter expansion, command substitution, |
136 | and arithmetic expansion on the right-hand side. Do not perform word | |
137 | splitting on the result of expansion. */ | |
712f80b0 CR |
138 | extern int do_assignment PARAMS((char *)); |
139 | extern int do_assignment_no_expand PARAMS((char *)); | |
140 | extern int do_word_assignment PARAMS((WORD_DESC *, int)); | |
ccc6cda3 | 141 | |
726f6388 JA |
142 | /* Append SOURCE to TARGET at INDEX. SIZE is the current amount |
143 | of space allocated to TARGET. SOURCE can be NULL, in which | |
144 | case nothing happens. Gets rid of SOURCE by free ()ing it. | |
145 | Returns TARGET in case the location has changed. */ | |
712f80b0 | 146 | extern char *sub_append_string PARAMS((char *, char *, int *, size_t *)); |
726f6388 JA |
147 | |
148 | /* Append the textual representation of NUMBER to TARGET. | |
149 | INDEX and SIZE are as in SUB_APPEND_STRING. */ | |
712f80b0 | 150 | extern char *sub_append_number PARAMS((intmax_t, char *, int *, int *)); |
726f6388 JA |
151 | |
152 | /* Return the word list that corresponds to `$*'. */ | |
712f80b0 | 153 | extern WORD_LIST *list_rest_of_args PARAMS((void)); |
726f6388 JA |
154 | |
155 | /* Make a single large string out of the dollar digit variables, | |
156 | and the rest_of_args. If DOLLAR_STAR is 1, then obey the special | |
157 | case of "$*" with respect to IFS. */ | |
712f80b0 | 158 | extern char *string_rest_of_args PARAMS((int)); |
ccc6cda3 | 159 | |
726f6388 JA |
160 | /* Expand STRING by performing parameter expansion, command substitution, |
161 | and arithmetic expansion. Dequote the resulting WORD_LIST before | |
162 | returning it, but do not perform word splitting. The call to | |
f73dda09 | 163 | remove_quoted_nulls () is made here because word splitting normally |
726f6388 | 164 | takes care of quote removal. */ |
712f80b0 | 165 | extern WORD_LIST *expand_string_unsplit PARAMS((char *, int)); |
726f6388 | 166 | |
95732b49 | 167 | /* Expand the rhs of an assignment statement. */ |
712f80b0 | 168 | extern WORD_LIST *expand_string_assignment PARAMS((char *, int)); |
95732b49 | 169 | |
bb70624e | 170 | /* Expand a prompt string. */ |
712f80b0 | 171 | extern WORD_LIST *expand_prompt_string PARAMS((char *, int, int)); |
bb70624e | 172 | |
726f6388 JA |
173 | /* Expand STRING just as if you were expanding a word. This also returns |
174 | a list of words. Note that filename globbing is *NOT* done for word | |
175 | or string expansion, just when the shell is expanding a command. This | |
176 | does parameter expansion, command substitution, arithmetic expansion, | |
177 | and word splitting. Dequote the resultant WORD_LIST before returning. */ | |
712f80b0 | 178 | extern WORD_LIST *expand_string PARAMS((char *, int)); |
726f6388 | 179 | |
f73dda09 JA |
180 | /* Convenience functions that expand strings to strings, taking care of |
181 | converting the WORD_LIST * returned by the expand_string* functions | |
182 | to a string and deallocating the WORD_LIST *. */ | |
712f80b0 CR |
183 | extern char *expand_string_to_string PARAMS((char *, int)); |
184 | extern char *expand_string_unsplit_to_string PARAMS((char *, int)); | |
185 | extern char *expand_assignment_string_to_string PARAMS((char *, int)); | |
f73dda09 | 186 | |
0628567a | 187 | /* Expand an arithmetic expression string */ |
712f80b0 | 188 | extern char *expand_arith_string PARAMS((char *, int)); |
0628567a JA |
189 | |
190 | /* De-quote quoted characters in STRING. */ | |
712f80b0 | 191 | extern char *dequote_string PARAMS((char *)); |
726f6388 | 192 | |
3185942a | 193 | /* De-quote CTLESC-escaped CTLESC or CTLNUL characters in STRING. */ |
712f80b0 | 194 | extern char *dequote_escapes PARAMS((const char *)); |
d233b485 | 195 | |
712f80b0 | 196 | extern WORD_DESC *dequote_word PARAMS((WORD_DESC *)); |
3185942a | 197 | |
0628567a | 198 | /* De-quote quoted characters in each word in LIST. */ |
712f80b0 | 199 | extern WORD_LIST *dequote_list PARAMS((WORD_LIST *)); |
0628567a | 200 | |
726f6388 JA |
201 | /* Expand WORD, performing word splitting on the result. This does |
202 | parameter expansion, command substitution, arithmetic expansion, | |
203 | word splitting, and quote removal. */ | |
712f80b0 | 204 | extern WORD_LIST *expand_word PARAMS((WORD_DESC *, int)); |
726f6388 JA |
205 | |
206 | /* Expand WORD, but do not perform word splitting on the result. This | |
207 | does parameter expansion, command substitution, arithmetic expansion, | |
208 | and quote removal. */ | |
712f80b0 CR |
209 | extern WORD_LIST *expand_word_unsplit PARAMS((WORD_DESC *, int)); |
210 | extern WORD_LIST *expand_word_leave_quoted PARAMS((WORD_DESC *, int)); | |
726f6388 JA |
211 | |
212 | /* Return the value of a positional parameter. This handles values > 10. */ | |
712f80b0 | 213 | extern char *get_dollar_var_value PARAMS((intmax_t)); |
726f6388 | 214 | |
ccc6cda3 | 215 | /* Quote a string to protect it from word splitting. */ |
712f80b0 | 216 | extern char *quote_string PARAMS((char *)); |
ccc6cda3 | 217 | |
712f80b0 | 218 | /* Quote escape characters (characters special to internals of expansion) |
f73dda09 | 219 | in a string. */ |
712f80b0 | 220 | extern char *quote_escapes PARAMS((const char *)); |
f73dda09 | 221 | |
3185942a | 222 | /* And remove such quoted special characters. */ |
712f80b0 | 223 | extern char *remove_quoted_escapes PARAMS((char *)); |
3185942a JA |
224 | |
225 | /* Remove CTLNUL characters from STRING unless they are quoted with CTLESC. */ | |
712f80b0 | 226 | extern char *remove_quoted_nulls PARAMS((char *)); |
3185942a | 227 | |
726f6388 JA |
228 | /* Perform quote removal on STRING. If QUOTED > 0, assume we are obeying the |
229 | backslash quoting rules for within double quotes. */ | |
712f80b0 | 230 | extern char *string_quote_removal PARAMS((char *, int)); |
726f6388 JA |
231 | |
232 | /* Perform quote removal on word WORD. This allocates and returns a new | |
233 | WORD_DESC *. */ | |
712f80b0 | 234 | extern WORD_DESC *word_quote_removal PARAMS((WORD_DESC *, int)); |
726f6388 JA |
235 | |
236 | /* Perform quote removal on all words in LIST. If QUOTED is non-zero, | |
237 | the members of the list are treated as if they are surrounded by | |
238 | double quotes. Return a new list, or NULL if LIST is NULL. */ | |
712f80b0 | 239 | extern WORD_LIST *word_list_quote_removal PARAMS((WORD_LIST *, int)); |
726f6388 | 240 | |
7117c2d2 | 241 | /* Called when IFS is changed to maintain some private variables. */ |
712f80b0 | 242 | extern void setifs PARAMS((SHELL_VAR *)); |
7117c2d2 JA |
243 | |
244 | /* Return the value of $IFS, or " \t\n" if IFS is unset. */ | |
712f80b0 | 245 | extern char *getifs PARAMS((void)); |
7117c2d2 | 246 | |
726f6388 JA |
247 | /* This splits a single word into a WORD LIST on $IFS, but only if the word |
248 | is not quoted. list_string () performs quote removal for us, even if we | |
249 | don't do any splitting. */ | |
712f80b0 | 250 | extern WORD_LIST *word_split PARAMS((WORD_DESC *, char *)); |
726f6388 JA |
251 | |
252 | /* Take the list of words in LIST and do the various substitutions. Return | |
253 | a new list of words which is the expanded list, and without things like | |
254 | variable assignments. */ | |
712f80b0 | 255 | extern WORD_LIST *expand_words PARAMS((WORD_LIST *)); |
726f6388 JA |
256 | |
257 | /* Same as expand_words (), but doesn't hack variable or environment | |
258 | variables. */ | |
712f80b0 | 259 | extern WORD_LIST *expand_words_no_vars PARAMS((WORD_LIST *)); |
726f6388 | 260 | |
cce855bc JA |
261 | /* Perform the `normal shell expansions' on a WORD_LIST. These are |
262 | brace expansion, tilde expansion, parameter and variable substitution, | |
263 | command substitution, arithmetic expansion, and word splitting. */ | |
712f80b0 | 264 | extern WORD_LIST *expand_words_shellexp PARAMS((WORD_LIST *)); |
726f6388 | 265 | |
712f80b0 CR |
266 | extern WORD_DESC *command_substitute PARAMS((char *, int, int)); |
267 | extern char *pat_subst PARAMS((char *, char *, char *, int)); | |
ccc6cda3 | 268 | |
d233b485 | 269 | #if defined (PROCESS_SUBSTITUTION) |
712f80b0 CR |
270 | extern int fifos_pending PARAMS((void)); |
271 | extern int num_fifos PARAMS((void)); | |
272 | extern void unlink_fifo_list PARAMS((void)); | |
3eb0018e | 273 | extern void unlink_all_fifos PARAMS((void)); |
712f80b0 | 274 | extern void unlink_fifo PARAMS((int)); |
495aee44 | 275 | |
712f80b0 CR |
276 | extern void *copy_fifo_list PARAMS((int *)); |
277 | extern void close_new_fifos PARAMS((void *, int)); | |
ccc6cda3 | 278 | |
712f80b0 | 279 | extern void clear_fifo_list PARAMS((void)); |
a0c0a00f | 280 | |
712f80b0 CR |
281 | extern int find_procsub_child PARAMS((pid_t)); |
282 | extern void set_procsub_status PARAMS((int, pid_t, int)); | |
d233b485 | 283 | |
712f80b0 CR |
284 | extern void wait_procsubs PARAMS((void)); |
285 | extern void reap_procsubs PARAMS((void)); | |
d233b485 CR |
286 | #endif |
287 | ||
712f80b0 | 288 | extern WORD_LIST *list_string_with_quotes PARAMS((char *)); |
bb70624e | 289 | |
ccc6cda3 | 290 | #if defined (ARRAY_VARS) |
712f80b0 | 291 | extern char *extract_array_assignment_list PARAMS((char *, int *)); |
ccc6cda3 JA |
292 | #endif |
293 | ||
cce855bc | 294 | #if defined (COND_COMMAND) |
712f80b0 CR |
295 | extern char *remove_backslashes PARAMS((char *)); |
296 | extern char *cond_expand_word PARAMS((WORD_DESC *, int)); | |
ccc6cda3 JA |
297 | #endif |
298 | ||
3185942a | 299 | /* Flags for skip_to_delim */ |
a0c0a00f CR |
300 | #define SD_NOJMP 0x001 /* don't longjmp on fatal error. */ |
301 | #define SD_INVERT 0x002 /* look for chars NOT in passed set */ | |
302 | #define SD_NOQUOTEDELIM 0x004 /* don't let single or double quotes act as delimiters */ | |
303 | #define SD_NOSKIPCMD 0x008 /* don't skip over $(, <(, or >( command/process substitution; parse them as commands */ | |
304 | #define SD_EXTGLOB 0x010 /* skip over extended globbing patterns if appropriate */ | |
305 | #define SD_IGNOREQUOTE 0x020 /* single and double quotes are not special */ | |
306 | #define SD_GLOB 0x040 /* skip over glob patterns like bracket expressions */ | |
307 | #define SD_NOPROCSUB 0x080 /* don't parse process substitutions as commands */ | |
308 | #define SD_COMPLETE 0x100 /* skip_to_delim during completion */ | |
309 | #define SD_HISTEXP 0x200 /* skip_to_delim during history expansion */ | |
310 | #define SD_ARITHEXP 0x400 /* skip_to_delim during arithmetic expansion */ | |
3185942a | 311 | |
712f80b0 | 312 | extern int skip_to_delim PARAMS((char *, int, char *, int)); |
3185942a | 313 | |
a0c0a00f | 314 | #if defined (BANG_HISTORY) |
712f80b0 | 315 | extern int skip_to_histexp PARAMS((char *, int, char *, int)); |
a0c0a00f CR |
316 | #endif |
317 | ||
bb70624e | 318 | #if defined (READLINE) |
712f80b0 CR |
319 | extern int char_is_quoted PARAMS((char *, int)); |
320 | extern int unclosed_pair PARAMS((char *, int, char *)); | |
321 | extern WORD_LIST *split_at_delims PARAMS((char *, int, char *, int, int, int *, int *)); | |
bb70624e JA |
322 | #endif |
323 | ||
7117c2d2 JA |
324 | /* Variables used to keep track of the characters in IFS. */ |
325 | extern SHELL_VAR *ifs_var; | |
326 | extern char *ifs_value; | |
327 | extern unsigned char ifs_cmap[]; | |
d233b485 | 328 | extern int ifs_is_set, ifs_is_null; |
95732b49 JA |
329 | |
330 | #if defined (HANDLE_MULTIBYTE) | |
331 | extern unsigned char ifs_firstc[]; | |
332 | extern size_t ifs_firstc_len; | |
333 | #else | |
7117c2d2 | 334 | extern unsigned char ifs_firstc; |
95732b49 | 335 | #endif |
7117c2d2 | 336 | |
d233b485 CR |
337 | extern int assigning_in_environment; |
338 | extern int expanding_redir; | |
339 | extern int inherit_errexit; | |
340 | ||
341 | extern pid_t last_command_subst_pid; | |
342 | ||
7117c2d2 JA |
343 | /* Evaluates to 1 if C is a character in $IFS. */ |
344 | #define isifs(c) (ifs_cmap[(unsigned char)(c)] != 0) | |
345 | ||
ccc6cda3 JA |
346 | /* How to determine the quoted state of the character C. */ |
347 | #define QUOTED_CHAR(c) ((c) == CTLESC) | |
348 | ||
349 | /* Is the first character of STRING a quoted NULL character? */ | |
350 | #define QUOTED_NULL(string) ((string)[0] == CTLNUL && (string)[1] == '\0') | |
351 | ||
712f80b0 | 352 | extern void invalidate_cached_quoted_dollar_at PARAMS((void)); |
a0c0a00f | 353 | |
726f6388 | 354 | #endif /* !_SUBST_H_ */ |