1 /* subst.h -- Names of externally visible functions in subst.c. */
2 
3 /* Copyright (C) 1993-2017 Free Software Foundation, Inc.
4 
5    This file is part of GNU Bash, the Bourne Again SHell.
6 
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.
11 
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.
16 
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 */
20 
21 #if !defined (_SUBST_H_)
22 #define _SUBST_H_
23 
24 #include "stdc.h"
25 
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 */
45 
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 */
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 */
57 #define ASS_NOINVIS	0x0400	/* don't resolve local invisible variables */
58 
59 /* Flags for the string extraction functions. */
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 */
69 #define SX_WORD		0x0200	/* extracting word in ${param op word} */
70 #define SX_COMPLETE	0x0400	/* extracting word for completion */
71 #define SX_STRIPDQ	0x0800	/* strip double quotes when extracting double-quoted string */
72 
73 /* Remove backslashes which are quoting backquotes from STRING.  Modifies
74    STRING, and returns a pointer to it. */
75 extern char * de_backslash PARAMS((char *));
76 
77 /* Replace instances of \! in a string with !. */
78 extern void unquote_bang PARAMS((char *));
79 
80 /* Extract the $( construct in STRING, and return a new string.
81    Start extracting at (SINDEX) as if we had just seen "$(".
82    Make (SINDEX) get the position just after the matching ")".
83    XFLAGS is additional flags to pass to other extraction functions, */
84 extern char *extract_command_subst PARAMS((char *, int *, int));
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 "]". */
89 extern char *extract_arithmetic_subst PARAMS((char *, int *));
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 ")". */
95 extern char *extract_process_subst PARAMS((char *, char *, int *, int));
96 #endif /* PROCESS_SUBSTITUTION */
97 
98 /* Extract the name of the variable to bind to from the assignment string. */
99 extern char *assignment_name PARAMS((char *));
100 
101 /* Return a single string of all the words present in LIST, separating
102    each word with SEP. */
103 extern char *string_list_internal PARAMS((WORD_LIST *, char *));
104 
105 /* Return a single string of all the words present in LIST, separating
106    each word with a space. */
107 extern char *string_list PARAMS((WORD_LIST *));
108 
109 /* Turn $* into a single string, obeying POSIX rules. */
110 extern char *string_list_dollar_star PARAMS((WORD_LIST *, int, int));
111 
112 /* Expand $@ into a single string, obeying POSIX rules. */
113 extern char *string_list_dollar_at PARAMS((WORD_LIST *, int, int));
114 
115 /* Turn the positional parameters into a string, understanding quoting and
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. */
119 extern char *string_list_pos_params PARAMS((int, WORD_LIST *, int, int));
120 
121 /* Perform quoted null character removal on each element of LIST.
122    This modifies LIST. */
123 extern void word_list_remove_quoted_nulls PARAMS((WORD_LIST *));
124 
125 /* This performs word splitting and quoted null character removal on
126    STRING. */
127 extern WORD_LIST *list_string PARAMS((char *, char *, int));
128 
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));
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
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. */
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));
141 
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. */
146 extern char *sub_append_string PARAMS((char *, char *, int *, size_t *));
147 
148 /* Append the textual representation of NUMBER to TARGET.
149    INDEX and SIZE are as in SUB_APPEND_STRING. */
150 extern char *sub_append_number PARAMS((intmax_t, char *, int *, int *));
151 
152 /* Return the word list that corresponds to `$*'. */
153 extern WORD_LIST *list_rest_of_args PARAMS((void));
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. */
158 extern char *string_rest_of_args PARAMS((int));
159 
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
163    remove_quoted_nulls () is made here because word splitting normally
164    takes care of quote removal. */
165 extern WORD_LIST *expand_string_unsplit PARAMS((char *, int));
166 
167 /* Expand the rhs of an assignment statement. */
168 extern WORD_LIST *expand_string_assignment PARAMS((char *, int));
169 
170 /* Expand a prompt string. */
171 extern WORD_LIST *expand_prompt_string PARAMS((char *, int, int));
172 
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. */
178 extern WORD_LIST *expand_string PARAMS((char *, int));
179 
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 *. */
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));
186 
187 /* Expand an arithmetic expression string */
188 extern char *expand_arith_string PARAMS((char *, int));
189 
190 /* De-quote quoted characters in STRING. */
191 extern char *dequote_string PARAMS((char *));
192 
193 /* De-quote CTLESC-escaped CTLESC or CTLNUL characters in STRING. */
194 extern char *dequote_escapes PARAMS((const char *));
195 
196 extern WORD_DESC *dequote_word PARAMS((WORD_DESC *));
197 
198 /* De-quote quoted characters in each word in LIST. */
199 extern WORD_LIST *dequote_list PARAMS((WORD_LIST *));
200 
201 /* Expand WORD, performing word splitting on the result.  This does
202    parameter expansion, command substitution, arithmetic expansion,
203    word splitting, and quote removal. */
204 extern WORD_LIST *expand_word PARAMS((WORD_DESC *, int));
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. */
209 extern WORD_LIST *expand_word_unsplit PARAMS((WORD_DESC *, int));
210 extern WORD_LIST *expand_word_leave_quoted PARAMS((WORD_DESC *, int));
211 
212 /* Return the value of a positional parameter.  This handles values > 10. */
213 extern char *get_dollar_var_value PARAMS((intmax_t));
214 
215 /* Quote a string to protect it from word splitting. */
216 extern char *quote_string PARAMS((char *));
217 
218 /* Quote escape characters (characters special to internals of expansion)
219    in a string. */
220 extern char *quote_escapes PARAMS((const char *));
221 
222 /* And remove such quoted special characters. */
223 extern char *remove_quoted_escapes PARAMS((char *));
224 
225 /* Remove CTLNUL characters from STRING unless they are quoted with CTLESC. */
226 extern char *remove_quoted_nulls PARAMS((char *));
227 
228 /* Perform quote removal on STRING.  If QUOTED > 0, assume we are obeying the
229    backslash quoting rules for within double quotes. */
230 extern char *string_quote_removal PARAMS((char *, int));
231 
232 /* Perform quote removal on word WORD.  This allocates and returns a new
233    WORD_DESC *. */
234 extern WORD_DESC *word_quote_removal PARAMS((WORD_DESC *, int));
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. */
239 extern WORD_LIST *word_list_quote_removal PARAMS((WORD_LIST *, int));
240 
241 /* Called when IFS is changed to maintain some private variables. */
242 extern void setifs PARAMS((SHELL_VAR *));
243 
244 /* Return the value of $IFS, or " \t\n" if IFS is unset. */
245 extern char *getifs PARAMS((void));
246 
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. */
250 extern WORD_LIST *word_split PARAMS((WORD_DESC *, char *));
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. */
255 extern WORD_LIST *expand_words PARAMS((WORD_LIST *));
256 
257 /* Same as expand_words (), but doesn't hack variable or environment
258    variables. */
259 extern WORD_LIST *expand_words_no_vars PARAMS((WORD_LIST *));
260 
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. */
264 extern WORD_LIST *expand_words_shellexp PARAMS((WORD_LIST *));
265 
266 extern WORD_DESC *command_substitute PARAMS((char *, int, int));
267 extern char *pat_subst PARAMS((char *, char *, char *, int));
268 
269 #if defined (PROCESS_SUBSTITUTION)
270 extern int fifos_pending PARAMS((void));
271 extern int num_fifos PARAMS((void));
272 extern void unlink_fifo_list PARAMS((void));
273 extern void unlink_all_fifos PARAMS((void));
274 extern void unlink_fifo PARAMS((int));
275 
276 extern void *copy_fifo_list PARAMS((int *));
277 extern void close_new_fifos PARAMS((void *, int));
278 
279 extern void clear_fifo_list PARAMS((void));
280 
281 extern int find_procsub_child PARAMS((pid_t));
282 extern void set_procsub_status PARAMS((int, pid_t, int));
283 
284 extern void wait_procsubs PARAMS((void));
285 extern void reap_procsubs PARAMS((void));
286 #endif
287 
288 extern WORD_LIST *list_string_with_quotes PARAMS((char *));
289 
290 #if defined (ARRAY_VARS)
291 extern char *extract_array_assignment_list PARAMS((char *, int *));
292 #endif
293 
294 #if defined (COND_COMMAND)
295 extern char *remove_backslashes PARAMS((char *));
296 extern char *cond_expand_word PARAMS((WORD_DESC *, int));
297 #endif
298 
299 /* Flags for skip_to_delim */
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 */
311 
312 extern int skip_to_delim PARAMS((char *, int, char *, int));
313 
314 #if defined (BANG_HISTORY)
315 extern int skip_to_histexp PARAMS((char *, int, char *, int));
316 #endif
317 
318 #if defined (READLINE)
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 *));
322 #endif
323 
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[];
328 extern int ifs_is_set, ifs_is_null;
329 
330 #if defined (HANDLE_MULTIBYTE)
331 extern unsigned char ifs_firstc[];
332 extern size_t ifs_firstc_len;
333 #else
334 extern unsigned char ifs_firstc;
335 #endif
336 
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 
343 /* Evaluates to 1 if C is a character in $IFS. */
344 #define isifs(c)	(ifs_cmap[(unsigned char)(c)] != 0)
345 
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 
352 extern void invalidate_cached_quoted_dollar_at PARAMS((void));
353 
354 #endif /* !_SUBST_H_ */
355