]>
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-2016 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 0x000001 /* Dollar sign present. */ | |
77 | #define W_QUOTED 0x000002 /* Some form of quote character is present. */ | |
78 | #define W_ASSIGNMENT 0x000004 /* This word is a variable assignment. */ | |
79 | #define W_SPLITSPACE 0x000008 /* Split this word on " " regardless of IFS */ | |
80 | #define W_NOSPLIT 0x000010 /* Do not perform word splitting on this word because ifs is empty string. */ | |
81 | #define W_NOGLOB 0x000020 /* Do not perform globbing on this word. */ | |
82 | #define W_NOSPLIT2 0x000040 /* Don't split word except for $@ expansion (using spaces) because context does not allow it. */ | |
83 | #define W_TILDEEXP 0x000080 /* Tilde expand this assignment word */ | |
84 | #define W_DOLLARAT 0x000100 /* $@ and its special handling */ | |
85 | #define W_DOLLARSTAR 0x000200 /* $* and its special handling */ | |
86 | #define W_NOCOMSUB 0x000400 /* Don't perform command substitution on this word */ | |
87 | #define W_ASSIGNRHS 0x000800 /* Word is rhs of an assignment statement */ | |
88 | #define W_NOTILDE 0x001000 /* Don't perform tilde expansion on this word */ | |
89 | #define W_ITILDE 0x002000 /* Internal flag for word expansion */ | |
90 | #define W_NOEXPAND 0x004000 /* Don't expand at all -- do quote removal */ | |
91 | #define W_COMPASSIGN 0x008000 /* Compound assignment */ | |
92 | #define W_ASSNBLTIN 0x010000 /* word is a builtin command that takes assignments */ | |
93 | #define W_ASSIGNARG 0x020000 /* word is assignment argument to command */ | |
94 | #define W_HASQUOTEDNULL 0x040000 /* word contains a quoted null character */ | |
95 | #define W_DQUOTE 0x080000 /* word should be treated as if double-quoted */ | |
96 | #define W_NOPROCSUB 0x100000 /* don't perform process substitution */ | |
97 | #define W_SAWQUOTEDNULL 0x200000 /* word contained a quoted null that was removed */ | |
98 | #define W_ASSIGNASSOC 0x400000 /* word looks like associative array assignment */ | |
99 | #define W_ASSIGNARRAY 0x800000 /* word looks like a compound indexed array assignment */ | |
100 | #define W_ARRAYIND 0x1000000 /* word is an array index being expanded */ | |
101 | #define W_ASSNGLOBAL 0x2000000 /* word is a global assignment to declare (declare/typeset -g) */ | |
102 | #define W_NOBRACE 0x4000000 /* Don't perform brace expansion */ | |
103 | #define W_COMPLETE 0x8000000 /* word is being expanded for completion */ | |
104 | #define W_CHKLOCAL 0x10000000 /* check for local vars on assignment */ | |
105 | #define W_NOASSNTILDE 0x20000000 /* don't do tilde expansion like an assignment statement */ | |
106 | ||
107 | /* Flags for the `pflags' argument to param_expand() and various | |
108 | parameter_brace_expand_xxx functions; also used for string_list_dollar_at */ | |
109 | #define PF_NOCOMSUB 0x01 /* Do not perform command substitution */ | |
110 | #define PF_IGNUNBOUND 0x02 /* ignore unbound vars even if -u set */ | |
111 | #define PF_NOSPLIT2 0x04 /* same as W_NOSPLIT2 */ | |
112 | #define PF_ASSIGNRHS 0x08 /* same as W_ASSIGNRHS */ | |
113 | #define PF_COMPLETE 0x10 /* same as W_COMPLETE, sets SX_COMPLETE */ | |
114 | ||
115 | /* Possible values for subshell_environment */ | |
116 | #define SUBSHELL_ASYNC 0x01 /* subshell caused by `command &' */ | |
117 | #define SUBSHELL_PAREN 0x02 /* subshell caused by ( ... ) */ | |
118 | #define SUBSHELL_COMSUB 0x04 /* subshell caused by `command` or $(command) */ | |
119 | #define SUBSHELL_FORK 0x08 /* subshell caused by executing a disk command */ | |
120 | #define SUBSHELL_PIPE 0x10 /* subshell from a pipeline element */ | |
121 | #define SUBSHELL_PROCSUB 0x20 /* subshell caused by <(command) or >(command) */ | |
122 | #define SUBSHELL_COPROC 0x40 /* subshell from a coproc pipeline */ | |
123 | #define SUBSHELL_RESETTRAP 0x80 /* subshell needs to reset trap strings on first call to trap */ | |
124 | ||
125 | /* A structure which represents a word. */ | |
126 | typedef struct word_desc { | |
127 | char *word; /* Zero terminated string. */ | |
128 | int flags; /* Flags associated with this word. */ | |
129 | } WORD_DESC; | |
130 | ||
131 | /* A linked list of words. */ | |
132 | typedef struct word_list { | |
133 | struct word_list *next; | |
134 | WORD_DESC *word; | |
135 | } WORD_LIST; | |
136 | ||
137 | ||
138 | /* **************************************************************** */ | |
139 | /* */ | |
140 | /* Shell Command Structs */ | |
141 | /* */ | |
142 | /* **************************************************************** */ | |
143 | ||
144 | /* What a redirection descriptor looks like. If the redirection instruction | |
145 | is ri_duplicating_input or ri_duplicating_output, use DEST, otherwise | |
146 | use the file in FILENAME. Out-of-range descriptors are identified by a | |
147 | negative DEST. */ | |
148 | ||
149 | typedef union { | |
150 | int dest; /* Place to redirect REDIRECTOR to, or ... */ | |
151 | WORD_DESC *filename; /* filename to redirect to. */ | |
152 | } REDIRECTEE; | |
153 | ||
154 | /* Structure describing a redirection. If REDIRECTOR is negative, the parser | |
155 | (or translator in redir.c) encountered an out-of-range file descriptor. */ | |
156 | typedef struct redirect { | |
157 | struct redirect *next; /* Next element, or NULL. */ | |
158 | REDIRECTEE redirector; /* Descriptor or varname to be redirected. */ | |
159 | int rflags; /* Private flags for this redirection */ | |
160 | int flags; /* Flag value for `open'. */ | |
161 | enum r_instruction instruction; /* What to do with the information. */ | |
162 | REDIRECTEE redirectee; /* File descriptor or filename */ | |
163 | char *here_doc_eof; /* The word that appeared in <<foo. */ | |
164 | } REDIRECT; | |
165 | ||
166 | /* An element used in parsing. A single word or a single redirection. | |
167 | This is an ephemeral construct. */ | |
168 | typedef struct element { | |
169 | WORD_DESC *word; | |
170 | REDIRECT *redirect; | |
171 | } ELEMENT; | |
172 | ||
173 | /* Possible values for command->flags. */ | |
174 | #define CMD_WANT_SUBSHELL 0x01 /* User wants a subshell: ( command ) */ | |
175 | #define CMD_FORCE_SUBSHELL 0x02 /* Shell needs to force a subshell. */ | |
176 | #define CMD_INVERT_RETURN 0x04 /* Invert the exit value. */ | |
177 | #define CMD_IGNORE_RETURN 0x08 /* Ignore the exit value. For set -e. */ | |
178 | #define CMD_NO_FUNCTIONS 0x10 /* Ignore functions during command lookup. */ | |
179 | #define CMD_INHIBIT_EXPANSION 0x20 /* Do not expand the command words. */ | |
180 | #define CMD_NO_FORK 0x40 /* Don't fork; just call execve */ | |
181 | #define CMD_TIME_PIPELINE 0x80 /* Time a pipeline */ | |
182 | #define CMD_TIME_POSIX 0x100 /* time -p; use POSIX.2 time output spec. */ | |
183 | #define CMD_AMPERSAND 0x200 /* command & */ | |
184 | #define CMD_STDIN_REDIR 0x400 /* async command needs implicit </dev/null */ | |
185 | #define CMD_COMMAND_BUILTIN 0x0800 /* command executed by `command' builtin */ | |
186 | #define CMD_COPROC_SUBSHELL 0x1000 | |
187 | #define CMD_LASTPIPE 0x2000 | |
188 | #define CMD_STDPATH 0x4000 /* use standard path for command lookup */ | |
189 | #define CMD_TRY_OPTIMIZING 0x8000 /* try to optimize this simple command */ | |
190 | ||
191 | /* What a command looks like. */ | |
192 | typedef struct command { | |
193 | enum command_type type; /* FOR CASE WHILE IF CONNECTION or SIMPLE. */ | |
194 | int flags; /* Flags controlling execution environment. */ | |
195 | int line; /* line number the command starts on */ | |
196 | REDIRECT *redirects; /* Special redirects for FOR CASE, etc. */ | |
197 | union { | |
198 | struct for_com *For; | |
199 | struct case_com *Case; | |
200 | struct while_com *While; | |
201 | struct if_com *If; | |
202 | struct connection *Connection; | |
203 | struct simple_com *Simple; | |
204 | struct function_def *Function_def; | |
205 | struct group_com *Group; | |
206 | #if defined (SELECT_COMMAND) | |
207 | struct select_com *Select; | |
208 | #endif | |
209 | #if defined (DPAREN_ARITHMETIC) | |
210 | struct arith_com *Arith; | |
211 | #endif | |
212 | #if defined (COND_COMMAND) | |
213 | struct cond_com *Cond; | |
214 | #endif | |
215 | #if defined (ARITH_FOR_COMMAND) | |
216 | struct arith_for_com *ArithFor; | |
217 | #endif | |
218 | struct subshell_com *Subshell; | |
219 | struct coproc_com *Coproc; | |
220 | } value; | |
221 | } COMMAND; | |
222 | ||
223 | /* Structure used to represent the CONNECTION type. */ | |
224 | typedef struct connection { | |
225 | int ignore; /* Unused; simplifies make_command (). */ | |
226 | COMMAND *first; /* Pointer to the first command. */ | |
227 | COMMAND *second; /* Pointer to the second command. */ | |
228 | int connector; /* What separates this command from others. */ | |
229 | } CONNECTION; | |
230 | ||
231 | /* Structures used to represent the CASE command. */ | |
232 | ||
233 | /* Values for FLAGS word in a PATTERN_LIST */ | |
234 | #define CASEPAT_FALLTHROUGH 0x01 | |
235 | #define CASEPAT_TESTNEXT 0x02 | |
236 | ||
237 | /* Pattern/action structure for CASE_COM. */ | |
238 | typedef struct pattern_list { | |
239 | struct pattern_list *next; /* Clause to try in case this one failed. */ | |
240 | WORD_LIST *patterns; /* Linked list of patterns to test. */ | |
241 | COMMAND *action; /* Thing to execute if a pattern matches. */ | |
242 | int flags; | |
243 | } PATTERN_LIST; | |
244 | ||
245 | /* The CASE command. */ | |
246 | typedef struct case_com { | |
247 | int flags; /* See description of CMD flags. */ | |
248 | int line; /* line number the `case' keyword appears on */ | |
249 | WORD_DESC *word; /* The thing to test. */ | |
250 | PATTERN_LIST *clauses; /* The clauses to test against, or NULL. */ | |
251 | } CASE_COM; | |
252 | ||
253 | /* FOR command. */ | |
254 | typedef struct for_com { | |
255 | int flags; /* See description of CMD flags. */ | |
256 | int line; /* line number the `for' keyword appears on */ | |
257 | WORD_DESC *name; /* The variable name to get mapped over. */ | |
258 | WORD_LIST *map_list; /* The things to map over. This is never NULL. */ | |
259 | COMMAND *action; /* The action to execute. | |
260 | During execution, NAME is bound to successive | |
261 | members of MAP_LIST. */ | |
262 | } FOR_COM; | |
263 | ||
264 | #if defined (ARITH_FOR_COMMAND) | |
265 | typedef struct arith_for_com { | |
266 | int flags; | |
267 | int line; /* generally used for error messages */ | |
268 | WORD_LIST *init; | |
269 | WORD_LIST *test; | |
270 | WORD_LIST *step; | |
271 | COMMAND *action; | |
272 | } ARITH_FOR_COM; | |
273 | #endif | |
274 | ||
275 | #if defined (SELECT_COMMAND) | |
276 | /* KSH SELECT command. */ | |
277 | typedef struct select_com { | |
278 | int flags; /* See description of CMD flags. */ | |
279 | int line; /* line number the `select' keyword appears on */ | |
280 | WORD_DESC *name; /* The variable name to get mapped over. */ | |
281 | WORD_LIST *map_list; /* The things to map over. This is never NULL. */ | |
282 | COMMAND *action; /* The action to execute. | |
283 | During execution, NAME is bound to the member of | |
284 | MAP_LIST chosen by the user. */ | |
285 | } SELECT_COM; | |
286 | #endif /* SELECT_COMMAND */ | |
287 | ||
288 | /* IF command. */ | |
289 | typedef struct if_com { | |
290 | int flags; /* See description of CMD flags. */ | |
291 | COMMAND *test; /* Thing to test. */ | |
292 | COMMAND *true_case; /* What to do if the test returned non-zero. */ | |
293 | COMMAND *false_case; /* What to do if the test returned zero. */ | |
294 | } IF_COM; | |
295 | ||
296 | /* WHILE command. */ | |
297 | typedef struct while_com { | |
298 | int flags; /* See description of CMD flags. */ | |
299 | COMMAND *test; /* Thing to test. */ | |
300 | COMMAND *action; /* Thing to do while test is non-zero. */ | |
301 | } WHILE_COM; | |
302 | ||
303 | #if defined (DPAREN_ARITHMETIC) | |
304 | /* The arithmetic evaluation command, ((...)). Just a set of flags and | |
305 | a WORD_LIST, of which the first element is the only one used, for the | |
306 | time being. */ | |
307 | typedef struct arith_com { | |
308 | int flags; | |
309 | int line; | |
310 | WORD_LIST *exp; | |
311 | } ARITH_COM; | |
312 | #endif /* DPAREN_ARITHMETIC */ | |
313 | ||
314 | /* The conditional command, [[...]]. This is a binary tree -- we slipped | |
315 | a recursive-descent parser into the YACC grammar to parse it. */ | |
316 | #define COND_AND 1 | |
317 | #define COND_OR 2 | |
318 | #define COND_UNARY 3 | |
319 | #define COND_BINARY 4 | |
320 | #define COND_TERM 5 | |
321 | #define COND_EXPR 6 | |
322 | ||
323 | typedef struct cond_com { | |
324 | int flags; | |
325 | int line; | |
326 | int type; | |
327 | WORD_DESC *op; | |
328 | struct cond_com *left, *right; | |
329 | } COND_COM; | |
330 | ||
331 | /* The "simple" command. Just a collection of words and redirects. */ | |
332 | typedef struct simple_com { | |
333 | int flags; /* See description of CMD flags. */ | |
334 | int line; /* line number the command starts on */ | |
335 | WORD_LIST *words; /* The program name, the arguments, | |
336 | variable assignments, etc. */ | |
337 | REDIRECT *redirects; /* Redirections to perform. */ | |
338 | } SIMPLE_COM; | |
339 | ||
340 | /* The "function definition" command. */ | |
341 | typedef struct function_def { | |
342 | int flags; /* See description of CMD flags. */ | |
343 | int line; /* Line number the function def starts on. */ | |
344 | WORD_DESC *name; /* The name of the function. */ | |
345 | COMMAND *command; /* The parsed execution tree. */ | |
346 | char *source_file; /* file in which function was defined, if any */ | |
347 | } FUNCTION_DEF; | |
348 | ||
349 | /* A command that is `grouped' allows pipes and redirections to affect all | |
350 | commands in the group. */ | |
351 | typedef struct group_com { | |
352 | int ignore; /* See description of CMD flags. */ | |
353 | COMMAND *command; | |
354 | } GROUP_COM; | |
355 | ||
356 | typedef struct subshell_com { | |
357 | int flags; | |
358 | int line; | |
359 | COMMAND *command; | |
360 | } SUBSHELL_COM; | |
361 | ||
362 | #define COPROC_RUNNING 0x01 | |
363 | #define COPROC_DEAD 0x02 | |
364 | ||
365 | typedef struct coproc { | |
366 | char *c_name; | |
367 | pid_t c_pid; | |
368 | int c_rfd; | |
369 | int c_wfd; | |
370 | int c_rsave; | |
371 | int c_wsave; | |
372 | int c_flags; | |
373 | int c_status; | |
374 | int c_lock; | |
375 | } Coproc; | |
376 | ||
377 | typedef struct coproc_com { | |
378 | int flags; | |
379 | char *name; | |
380 | COMMAND *command; | |
381 | } COPROC_COM; | |
382 | ||
383 | extern COMMAND *global_command; | |
384 | extern Coproc sh_coproc; | |
385 | ||
386 | /* Possible command errors */ | |
387 | #define CMDERR_DEFAULT 0 | |
388 | #define CMDERR_BADTYPE 1 | |
389 | #define CMDERR_BADCONN 2 | |
390 | #define CMDERR_BADJUMP 3 | |
391 | ||
392 | #define CMDERR_LAST 3 | |
393 | ||
394 | /* Forward declarations of functions declared in copy_cmd.c. */ | |
395 | ||
396 | extern FUNCTION_DEF *copy_function_def_contents __P((FUNCTION_DEF *, FUNCTION_DEF *)); | |
397 | extern FUNCTION_DEF *copy_function_def __P((FUNCTION_DEF *)); | |
398 | ||
399 | extern WORD_DESC *copy_word __P((WORD_DESC *)); | |
400 | extern WORD_LIST *copy_word_list __P((WORD_LIST *)); | |
401 | extern REDIRECT *copy_redirect __P((REDIRECT *)); | |
402 | extern REDIRECT *copy_redirects __P((REDIRECT *)); | |
403 | extern COMMAND *copy_command __P((COMMAND *)); | |
404 | ||
405 | #endif /* _COMMAND_H_ */ |