]>
Commit | Line | Data |
---|---|---|
726f6388 JA |
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 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 it under | |
9 | the terms of the GNU General Public License as published by the Free | |
10 | Software Foundation; either version 2, or (at your option) any later | |
11 | version. | |
12 | ||
13 | Bash is distributed in the hope that it will be useful, but WITHOUT ANY | |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License along | |
19 | with Bash; see the file COPYING. If not, write to the Free Software | |
bb70624e | 20 | Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ |
726f6388 | 21 | |
ccc6cda3 JA |
22 | #if !defined (_COMMAND_H_) |
23 | #define _COMMAND_H_ | |
726f6388 JA |
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, | |
7117c2d2 JA |
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 | |
726f6388 JA |
35 | }; |
36 | ||
ccc6cda3 JA |
37 | /* Redirection errors. */ |
38 | #define AMBIGUOUS_REDIRECT -1 | |
39 | #define NOCLOBBER_REDIRECT -2 | |
40 | #define RESTRICTED_REDIRECT -3 /* can only happen in restricted shells. */ | |
d166f048 | 41 | #define HEREDOC_REDIRECT -4 /* here-doc temp file can't be created */ |
ccc6cda3 | 42 | |
28ef6c31 JA |
43 | #define CLOBBERING_REDIRECT(ri) \ |
44 | (ri == r_output_direction || ri == r_err_and_out) | |
45 | ||
ccc6cda3 JA |
46 | #define OUTPUT_REDIRECT(ri) \ |
47 | (ri == r_output_direction || ri == r_input_output || ri == r_err_and_out) | |
48 | ||
49 | #define INPUT_REDIRECT(ri) \ | |
50 | (ri == r_input_direction || ri == r_inputa_direction || ri == r_input_output) | |
51 | ||
52 | #define WRITE_REDIRECT(ri) \ | |
53 | (ri == r_output_direction || \ | |
54 | ri == r_input_output || \ | |
55 | ri == r_err_and_out || \ | |
56 | ri == r_appending_to || \ | |
57 | ri == r_output_force) | |
58 | ||
7117c2d2 JA |
59 | /* redirection needs translation */ |
60 | #define TRANSLATE_REDIRECT(ri) \ | |
61 | (ri == r_duplicating_input_word || ri == r_duplicating_output_word || \ | |
62 | ri == r_move_input_word || ri == r_move_output_word) | |
63 | ||
726f6388 JA |
64 | /* Command Types: */ |
65 | enum command_type { cm_for, cm_case, cm_while, cm_if, cm_simple, cm_select, | |
cce855bc | 66 | cm_connection, cm_function_def, cm_until, cm_group, |
bb70624e | 67 | cm_arith, cm_cond, cm_arith_for, cm_subshell }; |
726f6388 | 68 | |
ccc6cda3 | 69 | /* Possible values for the `flags' field of a WORD_DESC. */ |
b80f6443 JA |
70 | #define W_HASDOLLAR 0x0001 /* Dollar sign present. */ |
71 | #define W_QUOTED 0x0002 /* Some form of quote character is present. */ | |
72 | #define W_ASSIGNMENT 0x0004 /* This word is a variable assignment. */ | |
73 | #define W_GLOBEXP 0x0008 /* This word is the result of a glob expansion. */ | |
74 | #define W_NOSPLIT 0x0010 /* Do not perform word splitting on this word. */ | |
75 | #define W_NOGLOB 0x0020 /* Do not perform globbing on this word. */ | |
76 | #define W_NOSPLIT2 0x0040 /* Don't split word except for $@ expansion. */ | |
77 | #define W_TILDEEXP 0x0080 /* Tilde expand this assignment word */ | |
78 | #define W_DOLLARAT 0x0100 /* $@ and its special handling */ | |
79 | #define W_DOLLARSTAR 0x0200 /* $* and its special handling */ | |
80 | #define W_NOCOMSUB 0x0400 /* Don't perform command substitution on this word */ | |
ccc6cda3 JA |
81 | |
82 | /* Possible values for subshell_environment */ | |
83 | #define SUBSHELL_ASYNC 0x01 /* subshell caused by `command &' */ | |
84 | #define SUBSHELL_PAREN 0x02 /* subshell caused by ( ... ) */ | |
85 | #define SUBSHELL_COMSUB 0x04 /* subshell caused by `command` or $(command) */ | |
86 | #define SUBSHELL_FORK 0x08 /* subshell caused by executing a disk command */ | |
28ef6c31 | 87 | #define SUBSHELL_PIPE 0x10 /* subshell from a pipeline element */ |
ccc6cda3 | 88 | |
726f6388 JA |
89 | /* A structure which represents a word. */ |
90 | typedef struct word_desc { | |
91 | char *word; /* Zero terminated string. */ | |
ccc6cda3 | 92 | int flags; /* Flags associated with this word. */ |
726f6388 JA |
93 | } WORD_DESC; |
94 | ||
95 | /* A linked list of words. */ | |
96 | typedef struct word_list { | |
97 | struct word_list *next; | |
98 | WORD_DESC *word; | |
99 | } WORD_LIST; | |
100 | ||
101 | ||
102 | /* **************************************************************** */ | |
103 | /* */ | |
104 | /* Shell Command Structs */ | |
105 | /* */ | |
106 | /* **************************************************************** */ | |
107 | ||
f73dda09 JA |
108 | /* What a redirection descriptor looks like. If the redirection instruction |
109 | is ri_duplicating_input or ri_duplicating_output, use DEST, otherwise | |
110 | use the file in FILENAME. Out-of-range descriptors are identified by a | |
111 | negative DEST. */ | |
726f6388 JA |
112 | |
113 | typedef union { | |
f73dda09 | 114 | int dest; /* Place to redirect REDIRECTOR to, or ... */ |
726f6388 JA |
115 | WORD_DESC *filename; /* filename to redirect to. */ |
116 | } REDIRECTEE; | |
117 | ||
f73dda09 JA |
118 | /* Structure describing a redirection. If REDIRECTOR is negative, the parser |
119 | (or translator in redir.c) encountered an out-of-range file descriptor. */ | |
726f6388 JA |
120 | typedef struct redirect { |
121 | struct redirect *next; /* Next element, or NULL. */ | |
122 | int redirector; /* Descriptor to be redirected. */ | |
123 | int flags; /* Flag value for `open'. */ | |
124 | enum r_instruction instruction; /* What to do with the information. */ | |
125 | REDIRECTEE redirectee; /* File descriptor or filename */ | |
126 | char *here_doc_eof; /* The word that appeared in <<foo. */ | |
127 | } REDIRECT; | |
128 | ||
129 | /* An element used in parsing. A single word or a single redirection. | |
130 | This is an ephemeral construct. */ | |
131 | typedef struct element { | |
132 | WORD_DESC *word; | |
133 | REDIRECT *redirect; | |
134 | } ELEMENT; | |
135 | ||
136 | /* Possible values for command->flags. */ | |
137 | #define CMD_WANT_SUBSHELL 0x01 /* User wants a subshell: ( command ) */ | |
138 | #define CMD_FORCE_SUBSHELL 0x02 /* Shell needs to force a subshell. */ | |
139 | #define CMD_INVERT_RETURN 0x04 /* Invert the exit value. */ | |
140 | #define CMD_IGNORE_RETURN 0x08 /* Ignore the exit value. For set -e. */ | |
141 | #define CMD_NO_FUNCTIONS 0x10 /* Ignore functions during command lookup. */ | |
142 | #define CMD_INHIBIT_EXPANSION 0x20 /* Do not expand the command words. */ | |
143 | #define CMD_NO_FORK 0x40 /* Don't fork; just call execve */ | |
ccc6cda3 JA |
144 | #define CMD_TIME_PIPELINE 0x80 /* Time a pipeline */ |
145 | #define CMD_TIME_POSIX 0x100 /* time -p; use POSIX.2 time output spec. */ | |
d166f048 JA |
146 | #define CMD_AMPERSAND 0x200 /* command & */ |
147 | #define CMD_STDIN_REDIR 0x400 /* async command needs implicit </dev/null */ | |
f73dda09 | 148 | #define CMD_COMMAND_BUILTIN 0x0800 /* command executed by `command' builtin */ |
726f6388 JA |
149 | |
150 | /* What a command looks like. */ | |
151 | typedef struct command { | |
152 | enum command_type type; /* FOR CASE WHILE IF CONNECTION or SIMPLE. */ | |
153 | int flags; /* Flags controlling execution environment. */ | |
154 | int line; /* line number the command starts on */ | |
155 | REDIRECT *redirects; /* Special redirects for FOR CASE, etc. */ | |
156 | union { | |
157 | struct for_com *For; | |
158 | struct case_com *Case; | |
159 | struct while_com *While; | |
160 | struct if_com *If; | |
161 | struct connection *Connection; | |
162 | struct simple_com *Simple; | |
163 | struct function_def *Function_def; | |
164 | struct group_com *Group; | |
165 | #if defined (SELECT_COMMAND) | |
166 | struct select_com *Select; | |
cce855bc JA |
167 | #endif |
168 | #if defined (DPAREN_ARITHMETIC) | |
169 | struct arith_com *Arith; | |
170 | #endif | |
171 | #if defined (COND_COMMAND) | |
172 | struct cond_com *Cond; | |
726f6388 | 173 | #endif |
bb70624e JA |
174 | #if defined (ARITH_FOR_COMMAND) |
175 | struct arith_for_com *ArithFor; | |
176 | #endif | |
177 | struct subshell_com *Subshell; | |
726f6388 JA |
178 | } value; |
179 | } COMMAND; | |
180 | ||
181 | /* Structure used to represent the CONNECTION type. */ | |
182 | typedef struct connection { | |
183 | int ignore; /* Unused; simplifies make_command (). */ | |
184 | COMMAND *first; /* Pointer to the first command. */ | |
185 | COMMAND *second; /* Pointer to the second command. */ | |
186 | int connector; /* What separates this command from others. */ | |
187 | } CONNECTION; | |
188 | ||
189 | /* Structures used to represent the CASE command. */ | |
190 | ||
191 | /* Pattern/action structure for CASE_COM. */ | |
192 | typedef struct pattern_list { | |
193 | struct pattern_list *next; /* Clause to try in case this one failed. */ | |
194 | WORD_LIST *patterns; /* Linked list of patterns to test. */ | |
195 | COMMAND *action; /* Thing to execute if a pattern matches. */ | |
196 | } PATTERN_LIST; | |
197 | ||
198 | /* The CASE command. */ | |
199 | typedef struct case_com { | |
200 | int flags; /* See description of CMD flags. */ | |
b80f6443 | 201 | int line; /* line number the `case' keyword appears on */ |
726f6388 JA |
202 | WORD_DESC *word; /* The thing to test. */ |
203 | PATTERN_LIST *clauses; /* The clauses to test against, or NULL. */ | |
204 | } CASE_COM; | |
205 | ||
206 | /* FOR command. */ | |
207 | typedef struct for_com { | |
208 | int flags; /* See description of CMD flags. */ | |
b80f6443 | 209 | int line; /* line number the `for' keyword appears on */ |
726f6388 JA |
210 | WORD_DESC *name; /* The variable name to get mapped over. */ |
211 | WORD_LIST *map_list; /* The things to map over. This is never NULL. */ | |
212 | COMMAND *action; /* The action to execute. | |
213 | During execution, NAME is bound to successive | |
214 | members of MAP_LIST. */ | |
215 | } FOR_COM; | |
216 | ||
bb70624e JA |
217 | #if defined (ARITH_FOR_COMMAND) |
218 | typedef struct arith_for_com { | |
219 | int flags; | |
220 | int line; /* generally used for error messages */ | |
221 | WORD_LIST *init; | |
222 | WORD_LIST *test; | |
223 | WORD_LIST *step; | |
224 | COMMAND *action; | |
225 | } ARITH_FOR_COM; | |
226 | #endif | |
227 | ||
726f6388 JA |
228 | #if defined (SELECT_COMMAND) |
229 | /* KSH SELECT command. */ | |
230 | typedef struct select_com { | |
231 | int flags; /* See description of CMD flags. */ | |
b80f6443 | 232 | int line; /* line number the `select' keyword appears on */ |
726f6388 JA |
233 | WORD_DESC *name; /* The variable name to get mapped over. */ |
234 | WORD_LIST *map_list; /* The things to map over. This is never NULL. */ | |
235 | COMMAND *action; /* The action to execute. | |
236 | During execution, NAME is bound to the member of | |
237 | MAP_LIST chosen by the user. */ | |
238 | } SELECT_COM; | |
239 | #endif /* SELECT_COMMAND */ | |
240 | ||
241 | /* IF command. */ | |
242 | typedef struct if_com { | |
243 | int flags; /* See description of CMD flags. */ | |
244 | COMMAND *test; /* Thing to test. */ | |
245 | COMMAND *true_case; /* What to do if the test returned non-zero. */ | |
246 | COMMAND *false_case; /* What to do if the test returned zero. */ | |
247 | } IF_COM; | |
248 | ||
249 | /* WHILE command. */ | |
250 | typedef struct while_com { | |
251 | int flags; /* See description of CMD flags. */ | |
252 | COMMAND *test; /* Thing to test. */ | |
253 | COMMAND *action; /* Thing to do while test is non-zero. */ | |
254 | } WHILE_COM; | |
255 | ||
cce855bc JA |
256 | #if defined (DPAREN_ARITHMETIC) |
257 | /* The arithmetic evaluation command, ((...)). Just a set of flags and | |
258 | a WORD_LIST, of which the first element is the only one used, for the | |
259 | time being. */ | |
260 | typedef struct arith_com { | |
261 | int flags; | |
cce855bc | 262 | int line; |
b80f6443 | 263 | WORD_LIST *exp; |
cce855bc JA |
264 | } ARITH_COM; |
265 | #endif /* DPAREN_ARITHMETIC */ | |
266 | ||
267 | /* The conditional command, [[...]]. This is a binary tree -- we slippped | |
268 | a recursive-descent parser into the YACC grammar to parse it. */ | |
269 | #define COND_AND 1 | |
270 | #define COND_OR 2 | |
271 | #define COND_UNARY 3 | |
272 | #define COND_BINARY 4 | |
273 | #define COND_TERM 5 | |
274 | #define COND_EXPR 6 | |
275 | ||
276 | typedef struct cond_com { | |
277 | int flags; | |
278 | int line; | |
279 | int type; | |
280 | WORD_DESC *op; | |
281 | struct cond_com *left, *right; | |
282 | } COND_COM; | |
283 | ||
726f6388 JA |
284 | /* The "simple" command. Just a collection of words and redirects. */ |
285 | typedef struct simple_com { | |
286 | int flags; /* See description of CMD flags. */ | |
b80f6443 | 287 | int line; /* line number the command starts on */ |
726f6388 JA |
288 | WORD_LIST *words; /* The program name, the arguments, |
289 | variable assignments, etc. */ | |
290 | REDIRECT *redirects; /* Redirections to perform. */ | |
726f6388 JA |
291 | } SIMPLE_COM; |
292 | ||
ccc6cda3 | 293 | /* The "function definition" command. */ |
726f6388 | 294 | typedef struct function_def { |
cce855bc | 295 | int flags; /* See description of CMD flags. */ |
b80f6443 | 296 | int line; /* Line number the function def starts on. */ |
726f6388 JA |
297 | WORD_DESC *name; /* The name of the function. */ |
298 | COMMAND *command; /* The parsed execution tree. */ | |
b80f6443 | 299 | char *source_file; /* file in which function was defined, if any */ |
726f6388 JA |
300 | } FUNCTION_DEF; |
301 | ||
ccc6cda3 JA |
302 | /* A command that is `grouped' allows pipes and redirections to affect all |
303 | commands in the group. */ | |
726f6388 JA |
304 | typedef struct group_com { |
305 | int ignore; /* See description of CMD flags. */ | |
306 | COMMAND *command; | |
307 | } GROUP_COM; | |
308 | ||
bb70624e JA |
309 | typedef struct subshell_com { |
310 | int flags; | |
311 | COMMAND *command; | |
312 | } SUBSHELL_COM; | |
313 | ||
726f6388 JA |
314 | extern COMMAND *global_command; |
315 | ||
b72432fd JA |
316 | /* Possible command errors */ |
317 | #define CMDERR_DEFAULT 0 | |
318 | #define CMDERR_BADTYPE 1 | |
319 | #define CMDERR_BADCONN 2 | |
320 | #define CMDERR_BADJUMP 3 | |
321 | ||
322 | #define CMDERR_LAST 3 | |
323 | ||
726f6388 JA |
324 | /* Forward declarations of functions declared in copy_cmd.c. */ |
325 | ||
b80f6443 JA |
326 | extern FUNCTION_DEF *copy_function_def_contents __P((FUNCTION_DEF *, FUNCTION_DEF *)); |
327 | extern FUNCTION_DEF *copy_function_def __P((FUNCTION_DEF *)); | |
328 | ||
726f6388 JA |
329 | extern WORD_DESC *copy_word __P((WORD_DESC *)); |
330 | extern WORD_LIST *copy_word_list __P((WORD_LIST *)); | |
331 | extern REDIRECT *copy_redirect __P((REDIRECT *)); | |
332 | extern REDIRECT *copy_redirects __P((REDIRECT *)); | |
333 | extern COMMAND *copy_command __P((COMMAND *)); | |
334 | ||
ccc6cda3 | 335 | #endif /* _COMMAND_H_ */ |