]>
Commit | Line | Data |
---|---|---|
1 | /* command.h -- The structures used internally to represent commands, and | |
2 | the extern declarations of the functions used to create them. */ | |
3 | ||
4 | /* Copyright (C) 1993-2021 Free Software Foundation, Inc. | |
5 | ||
6 | This file is part of GNU Bash, the Bourne Again SHell. | |
7 | ||
8 | Bash is free software: you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation, either version 3 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | Bash is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with Bash. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | #if !defined (_COMMAND_H_) | |
23 | #define _COMMAND_H_ | |
24 | ||
25 | #include "stdc.h" | |
26 | ||
27 | /* Instructions describing what kind of thing to do for a redirection. */ | |
28 | enum r_instruction { | |
29 | r_output_direction, r_input_direction, r_inputa_direction, | |
30 | r_appending_to, r_reading_until, r_reading_string, | |
31 | r_duplicating_input, r_duplicating_output, r_deblank_reading_until, | |
32 | r_close_this, r_err_and_out, r_input_output, r_output_force, | |
33 | r_duplicating_input_word, r_duplicating_output_word, | |
34 | r_move_input, r_move_output, r_move_input_word, r_move_output_word, | |
35 | r_append_err_and_out | |
36 | }; | |
37 | ||
38 | /* Redirection flags; values for rflags */ | |
39 | #define REDIR_VARASSIGN 0x01 | |
40 | ||
41 | /* Redirection errors. */ | |
42 | #define AMBIGUOUS_REDIRECT -1 | |
43 | #define NOCLOBBER_REDIRECT -2 | |
44 | #define RESTRICTED_REDIRECT -3 /* can only happen in restricted shells. */ | |
45 | #define HEREDOC_REDIRECT -4 /* here-doc temp file can't be created */ | |
46 | #define BADVAR_REDIRECT -5 /* something wrong with {varname}redir */ | |
47 | ||
48 | #define CLOBBERING_REDIRECT(ri) \ | |
49 | (ri == r_output_direction || ri == r_err_and_out) | |
50 | ||
51 | #define OUTPUT_REDIRECT(ri) \ | |
52 | (ri == r_output_direction || ri == r_input_output || ri == r_err_and_out || ri == r_append_err_and_out) | |
53 | ||
54 | #define INPUT_REDIRECT(ri) \ | |
55 | (ri == r_input_direction || ri == r_inputa_direction || ri == r_input_output) | |
56 | ||
57 | #define WRITE_REDIRECT(ri) \ | |
58 | (ri == r_output_direction || \ | |
59 | ri == r_input_output || \ | |
60 | ri == r_err_and_out || \ | |
61 | ri == r_appending_to || \ | |
62 | ri == r_append_err_and_out || \ | |
63 | ri == r_output_force) | |
64 | ||
65 | /* redirection needs translation */ | |
66 | #define TRANSLATE_REDIRECT(ri) \ | |
67 | (ri == r_duplicating_input_word || ri == r_duplicating_output_word || \ | |
68 | ri == r_move_input_word || ri == r_move_output_word) | |
69 | ||
70 | /* Command Types: */ | |
71 | enum command_type { cm_for, cm_case, cm_while, cm_if, cm_simple, cm_select, | |
72 | cm_connection, cm_function_def, cm_until, cm_group, | |
73 | cm_arith, cm_cond, cm_arith_for, cm_subshell, cm_coproc }; | |
74 | ||
75 | /* Possible values for the `flags' field of a WORD_DESC. */ | |
76 | #define W_HASDOLLAR (1 << 0) /* Dollar sign present. */ | |
77 | #define W_QUOTED (1 << 1) /* Some form of quote character is present. */ | |
78 | #define W_ASSIGNMENT (1 << 2) /* This word is a variable assignment. */ | |
79 | #define W_SPLITSPACE (1 << 3) /* Split this word on " " regardless of IFS */ | |
80 | #define W_NOSPLIT (1 << 4) /* Do not perform word splitting on this word because ifs is empty string. */ | |
81 | #define W_NOGLOB (1 << 5) /* Do not perform globbing on this word. */ | |
82 | #define W_NOSPLIT2 (1 << 6) /* Don't split word except for $@ expansion (using spaces) because context does not allow it. */ | |
83 | #define W_TILDEEXP (1 << 7) /* Tilde expand this assignment word */ | |
84 | #define W_DOLLARAT (1 << 8) /* UNUSED - $@ and its special handling */ | |
85 | #define W_ARRAYREF (1 << 9) /* word is a valid array reference */ | |
86 | #define W_NOCOMSUB (1 << 10) /* Don't perform command substitution on this word */ | |
87 | #define W_ASSIGNRHS (1 << 11) /* Word is rhs of an assignment statement */ | |
88 | #define W_NOTILDE (1 << 12) /* Don't perform tilde expansion on this word */ | |
89 | #define W_NOASSNTILDE (1 << 13) /* don't do tilde expansion like an assignment statement */ | |
90 | #define W_EXPANDRHS (1 << 14) /* Expanding word in ${paramOPword} */ | |
91 | #define W_COMPASSIGN (1 << 15) /* Compound assignment */ | |
92 | #define W_ASSNBLTIN (1 << 16) /* word is a builtin command that takes assignments */ | |
93 | #define W_ASSIGNARG (1 << 17) /* word is assignment argument to command */ | |
94 | #define W_HASQUOTEDNULL (1 << 18) /* word contains a quoted null character */ | |
95 | #define W_DQUOTE (1 << 19) /* UNUSED - word should be treated as if double-quoted */ | |
96 | #define W_NOPROCSUB (1 << 20) /* don't perform process substitution */ | |
97 | #define W_SAWQUOTEDNULL (1 << 21) /* word contained a quoted null that was removed */ | |
98 | #define W_ASSIGNASSOC (1 << 22) /* word looks like associative array assignment */ | |
99 | #define W_ASSIGNARRAY (1 << 23) /* word looks like a compound indexed array assignment */ | |
100 | #define W_ARRAYIND (1 << 24) /* word is an array index being expanded */ | |
101 | #define W_ASSNGLOBAL (1 << 25) /* word is a global assignment to declare (declare/typeset -g) */ | |
102 | #define W_NOBRACE (1 << 26) /* Don't perform brace expansion */ | |
103 | #define W_COMPLETE (1 << 27) /* word is being expanded for completion */ | |
104 | #define W_CHKLOCAL (1 << 28) /* check for local vars on assignment */ | |
105 | #define W_FORCELOCAL (1 << 29) /* force assignments to be to local variables, non-fatal on assignment errors */ | |
106 | /* UNUSED (1 << 30) */ | |
107 | ||
108 | /* Flags for the `pflags' argument to param_expand() and various | |
109 | parameter_brace_expand_xxx functions; also used for string_list_dollar_at */ | |
110 | #define PF_NOCOMSUB 0x01 /* Do not perform command substitution */ | |
111 | #define PF_IGNUNBOUND 0x02 /* ignore unbound vars even if -u set */ | |
112 | #define PF_NOSPLIT2 0x04 /* same as W_NOSPLIT2 */ | |
113 | #define PF_ASSIGNRHS 0x08 /* same as W_ASSIGNRHS */ | |
114 | #define PF_COMPLETE 0x10 /* same as W_COMPLETE, sets SX_COMPLETE */ | |
115 | #define PF_EXPANDRHS 0x20 /* same as W_EXPANDRHS */ | |
116 | #define PF_ALLINDS 0x40 /* array, act as if [@] was supplied */ | |
117 | #define PF_BACKQUOTE 0x80 /* differentiate `` from $() for command_substitute */ | |
118 | ||
119 | /* Possible values for subshell_environment */ | |
120 | #define SUBSHELL_ASYNC 0x01 /* subshell caused by `command &' */ | |
121 | #define SUBSHELL_PAREN 0x02 /* subshell caused by ( ... ) */ | |
122 | #define SUBSHELL_COMSUB 0x04 /* subshell caused by `command` or $(command) */ | |
123 | #define SUBSHELL_FORK 0x08 /* subshell caused by executing a disk command */ | |
124 | #define SUBSHELL_PIPE 0x10 /* subshell from a pipeline element */ | |
125 | #define SUBSHELL_PROCSUB 0x20 /* subshell caused by <(command) or >(command) */ | |
126 | #define SUBSHELL_COPROC 0x40 /* subshell from a coproc pipeline */ | |
127 | #define SUBSHELL_RESETTRAP 0x80 /* subshell needs to reset trap strings on first call to trap */ | |
128 | #define SUBSHELL_IGNTRAP 0x100 /* subshell should reset trapped signals from trap_handler */ | |
129 | ||
130 | /* A structure which represents a word. */ | |
131 | typedef struct word_desc { | |
132 | char *word; /* Zero terminated string. */ | |
133 | int flags; /* Flags associated with this word. */ | |
134 | } WORD_DESC; | |
135 | ||
136 | /* A linked list of words. */ | |
137 | typedef struct word_list { | |
138 | struct word_list *next; | |
139 | WORD_DESC *word; | |
140 | } WORD_LIST; | |
141 | ||
142 | ||
143 | /* **************************************************************** */ | |
144 | /* */ | |
145 | /* Shell Command Structs */ | |
146 | /* */ | |
147 | /* **************************************************************** */ | |
148 | ||
149 | /* What a redirection descriptor looks like. If the redirection instruction | |
150 | is ri_duplicating_input or ri_duplicating_output, use DEST, otherwise | |
151 | use the file in FILENAME. Out-of-range descriptors are identified by a | |
152 | negative DEST. */ | |
153 | ||
154 | typedef union { | |
155 | int dest; /* Place to redirect REDIRECTOR to, or ... */ | |
156 | WORD_DESC *filename; /* filename to redirect to. */ | |
157 | } REDIRECTEE; | |
158 | ||
159 | /* Structure describing a redirection. If REDIRECTOR is negative, the parser | |
160 | (or translator in redir.c) encountered an out-of-range file descriptor. */ | |
161 | typedef struct redirect { | |
162 | struct redirect *next; /* Next element, or NULL. */ | |
163 | REDIRECTEE redirector; /* Descriptor or varname to be redirected. */ | |
164 | int rflags; /* Private flags for this redirection */ | |
165 | int flags; /* Flag value for `open'. */ | |
166 | enum r_instruction instruction; /* What to do with the information. */ | |
167 | REDIRECTEE redirectee; /* File descriptor or filename */ | |
168 | char *here_doc_eof; /* The word that appeared in <<foo. */ | |
169 | } REDIRECT; | |
170 | ||
171 | /* An element used in parsing. A single word or a single redirection. | |
172 | This is an ephemeral construct. */ | |
173 | typedef struct element { | |
174 | WORD_DESC *word; | |
175 | REDIRECT *redirect; | |
176 | } ELEMENT; | |
177 | ||
178 | /* Possible values for command->flags. */ | |
179 | #define CMD_WANT_SUBSHELL 0x01 /* User wants a subshell: ( command ) */ | |
180 | #define CMD_FORCE_SUBSHELL 0x02 /* Shell needs to force a subshell. */ | |
181 | #define CMD_INVERT_RETURN 0x04 /* Invert the exit value. */ | |
182 | #define CMD_IGNORE_RETURN 0x08 /* Ignore the exit value. For set -e. */ | |
183 | #define CMD_NO_FUNCTIONS 0x10 /* Ignore functions during command lookup. */ | |
184 | #define CMD_INHIBIT_EXPANSION 0x20 /* Do not expand the command words. */ | |
185 | #define CMD_NO_FORK 0x40 /* Don't fork; just call execve */ | |
186 | #define CMD_TIME_PIPELINE 0x80 /* Time a pipeline */ | |
187 | #define CMD_TIME_POSIX 0x100 /* time -p; use POSIX.2 time output spec. */ | |
188 | #define CMD_AMPERSAND 0x200 /* command & */ | |
189 | #define CMD_STDIN_REDIR 0x400 /* async command needs implicit </dev/null */ | |
190 | #define CMD_COMMAND_BUILTIN 0x0800 /* command executed by `command' builtin */ | |
191 | #define CMD_COPROC_SUBSHELL 0x1000 | |
192 | #define CMD_LASTPIPE 0x2000 | |
193 | #define CMD_STDPATH 0x4000 /* use standard path for command lookup */ | |
194 | #define CMD_TRY_OPTIMIZING 0x8000 /* try to optimize this simple command */ | |
195 | ||
196 | /* What a command looks like. */ | |
197 | typedef struct command { | |
198 | enum command_type type; /* FOR CASE WHILE IF CONNECTION or SIMPLE. */ | |
199 | int flags; /* Flags controlling execution environment. */ | |
200 | int line; /* line number the command starts on */ | |
201 | REDIRECT *redirects; /* Special redirects for FOR CASE, etc. */ | |
202 | union { | |
203 | struct for_com *For; | |
204 | struct case_com *Case; | |
205 | struct while_com *While; | |
206 | struct if_com *If; | |
207 | struct connection *Connection; | |
208 | struct simple_com *Simple; | |
209 | struct function_def *Function_def; | |
210 | struct group_com *Group; | |
211 | #if defined (SELECT_COMMAND) | |
212 | struct select_com *Select; | |
213 | #endif | |
214 | #if defined (DPAREN_ARITHMETIC) | |
215 | struct arith_com *Arith; | |
216 | #endif | |
217 | #if defined (COND_COMMAND) | |
218 | struct cond_com *Cond; | |
219 | #endif | |
220 | #if defined (ARITH_FOR_COMMAND) | |
221 | struct arith_for_com *ArithFor; | |
222 | #endif | |
223 | struct subshell_com *Subshell; | |
224 | struct coproc_com *Coproc; | |
225 | } value; | |
226 | } COMMAND; | |
227 | ||
228 | /* Structure used to represent the CONNECTION type. */ | |
229 | typedef struct connection { | |
230 | int ignore; /* Unused; simplifies make_command (). */ | |
231 | COMMAND *first; /* Pointer to the first command. */ | |
232 | COMMAND *second; /* Pointer to the second command. */ | |
233 | int connector; /* What separates this command from others. */ | |
234 | } CONNECTION; | |
235 | ||
236 | /* Structures used to represent the CASE command. */ | |
237 | ||
238 | /* Values for FLAGS word in a PATTERN_LIST */ | |
239 | #define CASEPAT_FALLTHROUGH 0x01 | |
240 | #define CASEPAT_TESTNEXT 0x02 | |
241 | ||
242 | /* Pattern/action structure for CASE_COM. */ | |
243 | typedef struct pattern_list { | |
244 | struct pattern_list *next; /* Clause to try in case this one failed. */ | |
245 | WORD_LIST *patterns; /* Linked list of patterns to test. */ | |
246 | COMMAND *action; /* Thing to execute if a pattern matches. */ | |
247 | int flags; | |
248 | } PATTERN_LIST; | |
249 | ||
250 | /* The CASE command. */ | |
251 | typedef struct case_com { | |
252 | int flags; /* See description of CMD flags. */ | |
253 | int line; /* line number the `case' keyword appears on */ | |
254 | WORD_DESC *word; /* The thing to test. */ | |
255 | PATTERN_LIST *clauses; /* The clauses to test against, or NULL. */ | |
256 | } CASE_COM; | |
257 | ||
258 | /* FOR command. */ | |
259 | typedef struct for_com { | |
260 | int flags; /* See description of CMD flags. */ | |
261 | int line; /* line number the `for' keyword appears on */ | |
262 | WORD_DESC *name; /* The variable name to get mapped over. */ | |
263 | WORD_LIST *map_list; /* The things to map over. This is never NULL. */ | |
264 | COMMAND *action; /* The action to execute. | |
265 | During execution, NAME is bound to successive | |
266 | members of MAP_LIST. */ | |
267 | } FOR_COM; | |
268 | ||
269 | #if defined (ARITH_FOR_COMMAND) | |
270 | typedef struct arith_for_com { | |
271 | int flags; | |
272 | int line; /* generally used for error messages */ | |
273 | WORD_LIST *init; | |
274 | WORD_LIST *test; | |
275 | WORD_LIST *step; | |
276 | COMMAND *action; | |
277 | } ARITH_FOR_COM; | |
278 | #endif | |
279 | ||
280 | #if defined (SELECT_COMMAND) | |
281 | /* KSH SELECT command. */ | |
282 | typedef struct select_com { | |
283 | int flags; /* See description of CMD flags. */ | |
284 | int line; /* line number the `select' keyword appears on */ | |
285 | WORD_DESC *name; /* The variable name to get mapped over. */ | |
286 | WORD_LIST *map_list; /* The things to map over. This is never NULL. */ | |
287 | COMMAND *action; /* The action to execute. | |
288 | During execution, NAME is bound to the member of | |
289 | MAP_LIST chosen by the user. */ | |
290 | } SELECT_COM; | |
291 | #endif /* SELECT_COMMAND */ | |
292 | ||
293 | /* IF command. */ | |
294 | typedef struct if_com { | |
295 | int flags; /* See description of CMD flags. */ | |
296 | COMMAND *test; /* Thing to test. */ | |
297 | COMMAND *true_case; /* What to do if the test returned non-zero. */ | |
298 | COMMAND *false_case; /* What to do if the test returned zero. */ | |
299 | } IF_COM; | |
300 | ||
301 | /* WHILE command. */ | |
302 | typedef struct while_com { | |
303 | int flags; /* See description of CMD flags. */ | |
304 | COMMAND *test; /* Thing to test. */ | |
305 | COMMAND *action; /* Thing to do while test is non-zero. */ | |
306 | } WHILE_COM; | |
307 | ||
308 | #if defined (DPAREN_ARITHMETIC) | |
309 | /* The arithmetic evaluation command, ((...)). Just a set of flags and | |
310 | a WORD_LIST, of which the first element is the only one used, for the | |
311 | time being. */ | |
312 | typedef struct arith_com { | |
313 | int flags; | |
314 | int line; | |
315 | WORD_LIST *exp; | |
316 | } ARITH_COM; | |
317 | #endif /* DPAREN_ARITHMETIC */ | |
318 | ||
319 | /* The conditional command, [[...]]. This is a binary tree -- we slipped | |
320 | a recursive-descent parser into the YACC grammar to parse it. */ | |
321 | #define COND_AND 1 | |
322 | #define COND_OR 2 | |
323 | #define COND_UNARY 3 | |
324 | #define COND_BINARY 4 | |
325 | #define COND_TERM 5 | |
326 | #define COND_EXPR 6 | |
327 | ||
328 | typedef struct cond_com { | |
329 | int flags; | |
330 | int line; | |
331 | int type; | |
332 | WORD_DESC *op; | |
333 | struct cond_com *left, *right; | |
334 | } COND_COM; | |
335 | ||
336 | /* The "simple" command. Just a collection of words and redirects. */ | |
337 | typedef struct simple_com { | |
338 | int flags; /* See description of CMD flags. */ | |
339 | int line; /* line number the command starts on */ | |
340 | WORD_LIST *words; /* The program name, the arguments, | |
341 | variable assignments, etc. */ | |
342 | REDIRECT *redirects; /* Redirections to perform. */ | |
343 | } SIMPLE_COM; | |
344 | ||
345 | /* The "function definition" command. */ | |
346 | typedef struct function_def { | |
347 | int flags; /* See description of CMD flags. */ | |
348 | int line; /* Line number the function def starts on. */ | |
349 | WORD_DESC *name; /* The name of the function. */ | |
350 | COMMAND *command; /* The parsed execution tree. */ | |
351 | char *source_file; /* file in which function was defined, if any */ | |
352 | } FUNCTION_DEF; | |
353 | ||
354 | /* A command that is `grouped' allows pipes and redirections to affect all | |
355 | commands in the group. */ | |
356 | typedef struct group_com { | |
357 | int ignore; /* See description of CMD flags. */ | |
358 | COMMAND *command; | |
359 | } GROUP_COM; | |
360 | ||
361 | typedef struct subshell_com { | |
362 | int flags; | |
363 | int line; | |
364 | COMMAND *command; | |
365 | } SUBSHELL_COM; | |
366 | ||
367 | #define COPROC_RUNNING 0x01 | |
368 | #define COPROC_DEAD 0x02 | |
369 | ||
370 | typedef struct coproc { | |
371 | char *c_name; | |
372 | pid_t c_pid; | |
373 | int c_rfd; | |
374 | int c_wfd; | |
375 | int c_rsave; | |
376 | int c_wsave; | |
377 | int c_flags; | |
378 | int c_status; | |
379 | int c_lock; | |
380 | } Coproc; | |
381 | ||
382 | typedef struct coproc_com { | |
383 | int flags; | |
384 | char *name; | |
385 | COMMAND *command; | |
386 | } COPROC_COM; | |
387 | ||
388 | extern COMMAND *global_command; | |
389 | extern Coproc sh_coproc; | |
390 | ||
391 | /* Possible command errors */ | |
392 | #define CMDERR_DEFAULT 0 | |
393 | #define CMDERR_BADTYPE 1 | |
394 | #define CMDERR_BADCONN 2 | |
395 | #define CMDERR_BADJUMP 3 | |
396 | ||
397 | #define CMDERR_LAST 3 | |
398 | ||
399 | /* Forward declarations of functions declared in copy_cmd.c. */ | |
400 | ||
401 | extern FUNCTION_DEF *copy_function_def_contents PARAMS((FUNCTION_DEF *, FUNCTION_DEF *)); | |
402 | extern FUNCTION_DEF *copy_function_def PARAMS((FUNCTION_DEF *)); | |
403 | ||
404 | extern WORD_DESC *copy_word PARAMS((WORD_DESC *)); | |
405 | extern WORD_LIST *copy_word_list PARAMS((WORD_LIST *)); | |
406 | extern REDIRECT *copy_redirect PARAMS((REDIRECT *)); | |
407 | extern REDIRECT *copy_redirects PARAMS((REDIRECT *)); | |
408 | extern COMMAND *copy_command PARAMS((COMMAND *)); | |
409 | ||
410 | #endif /* _COMMAND_H_ */ |