]> git.ipfire.org Git - thirdparty/bash.git/blame - command.h
Imported from ../bash-4.0-rc1.tar.gz.
[thirdparty/bash.git] / command.h
CommitLineData
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
3185942a 4/* Copyright (C) 1993-2009 Free Software Foundation, Inc.
726f6388
JA
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
3185942a
JA
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.
726f6388 12
3185942a
JA
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.
726f6388 17
3185942a
JA
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*/
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. */
28enum 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,
3185942a
JA
34 r_move_input, r_move_output, r_move_input_word, r_move_output_word,
35 r_append_err_and_out
726f6388
JA
36};
37
ccc6cda3
JA
38/* Redirection errors. */
39#define AMBIGUOUS_REDIRECT -1
40#define NOCLOBBER_REDIRECT -2
41#define RESTRICTED_REDIRECT -3 /* can only happen in restricted shells. */
d166f048 42#define HEREDOC_REDIRECT -4 /* here-doc temp file can't be created */
ccc6cda3 43
28ef6c31
JA
44#define CLOBBERING_REDIRECT(ri) \
45 (ri == r_output_direction || ri == r_err_and_out)
46
ccc6cda3 47#define OUTPUT_REDIRECT(ri) \
3185942a 48 (ri == r_output_direction || ri == r_input_output || ri == r_err_and_out || ri == r_append_err_and_out)
ccc6cda3
JA
49
50#define INPUT_REDIRECT(ri) \
51 (ri == r_input_direction || ri == r_inputa_direction || ri == r_input_output)
52
53#define WRITE_REDIRECT(ri) \
54 (ri == r_output_direction || \
55 ri == r_input_output || \
56 ri == r_err_and_out || \
57 ri == r_appending_to || \
3185942a 58 ri == r_append_err_and_out || \
ccc6cda3
JA
59 ri == r_output_force)
60
7117c2d2
JA
61/* redirection needs translation */
62#define TRANSLATE_REDIRECT(ri) \
63 (ri == r_duplicating_input_word || ri == r_duplicating_output_word || \
64 ri == r_move_input_word || ri == r_move_output_word)
65
726f6388
JA
66/* Command Types: */
67enum command_type { cm_for, cm_case, cm_while, cm_if, cm_simple, cm_select,
cce855bc 68 cm_connection, cm_function_def, cm_until, cm_group,
3185942a 69 cm_arith, cm_cond, cm_arith_for, cm_subshell, cm_coproc };
726f6388 70
ccc6cda3 71/* Possible values for the `flags' field of a WORD_DESC. */
0628567a
JA
72#define W_HASDOLLAR 0x000001 /* Dollar sign present. */
73#define W_QUOTED 0x000002 /* Some form of quote character is present. */
74#define W_ASSIGNMENT 0x000004 /* This word is a variable assignment. */
75#define W_GLOBEXP 0x000008 /* This word is the result of a glob expansion. */
76#define W_NOSPLIT 0x000010 /* Do not perform word splitting on this word. */
77#define W_NOGLOB 0x000020 /* Do not perform globbing on this word. */
78#define W_NOSPLIT2 0x000040 /* Don't split word except for $@ expansion. */
79#define W_TILDEEXP 0x000080 /* Tilde expand this assignment word */
80#define W_DOLLARAT 0x000100 /* $@ and its special handling */
81#define W_DOLLARSTAR 0x000200 /* $* and its special handling */
82#define W_NOCOMSUB 0x000400 /* Don't perform command substitution on this word */
83#define W_ASSIGNRHS 0x000800 /* Word is rhs of an assignment statement */
84#define W_NOTILDE 0x001000 /* Don't perform tilde expansion on this word */
85#define W_ITILDE 0x002000 /* Internal flag for word expansion */
86#define W_NOEXPAND 0x004000 /* Don't expand at all -- do quote removal */
87#define W_COMPASSIGN 0x008000 /* Compound assignment */
88#define W_ASSNBLTIN 0x010000 /* word is a builtin command that takes assignments */
89#define W_ASSIGNARG 0x020000 /* word is assignment argument to command */
90#define W_HASQUOTEDNULL 0x040000 /* word contains a quoted null character */
91#define W_DQUOTE 0x080000 /* word should be treated as if double-quoted */
92#define W_NOPROCSUB 0x100000 /* don't perform process substitution */
3185942a
JA
93#define W_HASCTLESC 0x200000 /* word contains literal CTLESC characters */
94#define W_ASSIGNASSOC 0x400000 /* word looks like associative array assignment */
ccc6cda3
JA
95
96/* Possible values for subshell_environment */
97#define SUBSHELL_ASYNC 0x01 /* subshell caused by `command &' */
98#define SUBSHELL_PAREN 0x02 /* subshell caused by ( ... ) */
99#define SUBSHELL_COMSUB 0x04 /* subshell caused by `command` or $(command) */
100#define SUBSHELL_FORK 0x08 /* subshell caused by executing a disk command */
28ef6c31 101#define SUBSHELL_PIPE 0x10 /* subshell from a pipeline element */
3185942a
JA
102#define SUBSHELL_PROCSUB 0x20 /* subshell caused by <(command) or >(command) */
103#define SUBSHELL_COPROC 0x40 /* subshell from a coproc pipeline */
ccc6cda3 104
726f6388
JA
105/* A structure which represents a word. */
106typedef struct word_desc {
107 char *word; /* Zero terminated string. */
ccc6cda3 108 int flags; /* Flags associated with this word. */
726f6388
JA
109} WORD_DESC;
110
111/* A linked list of words. */
112typedef struct word_list {
113 struct word_list *next;
114 WORD_DESC *word;
115} WORD_LIST;
116
117
118/* **************************************************************** */
119/* */
120/* Shell Command Structs */
121/* */
122/* **************************************************************** */
123
f73dda09
JA
124/* What a redirection descriptor looks like. If the redirection instruction
125 is ri_duplicating_input or ri_duplicating_output, use DEST, otherwise
126 use the file in FILENAME. Out-of-range descriptors are identified by a
127 negative DEST. */
726f6388
JA
128
129typedef union {
f73dda09 130 int dest; /* Place to redirect REDIRECTOR to, or ... */
726f6388
JA
131 WORD_DESC *filename; /* filename to redirect to. */
132} REDIRECTEE;
133
f73dda09
JA
134/* Structure describing a redirection. If REDIRECTOR is negative, the parser
135 (or translator in redir.c) encountered an out-of-range file descriptor. */
726f6388
JA
136typedef struct redirect {
137 struct redirect *next; /* Next element, or NULL. */
138 int redirector; /* Descriptor to be redirected. */
139 int flags; /* Flag value for `open'. */
140 enum r_instruction instruction; /* What to do with the information. */
141 REDIRECTEE redirectee; /* File descriptor or filename */
142 char *here_doc_eof; /* The word that appeared in <<foo. */
143} REDIRECT;
144
145/* An element used in parsing. A single word or a single redirection.
146 This is an ephemeral construct. */
147typedef struct element {
148 WORD_DESC *word;
149 REDIRECT *redirect;
150} ELEMENT;
151
152/* Possible values for command->flags. */
153#define CMD_WANT_SUBSHELL 0x01 /* User wants a subshell: ( command ) */
154#define CMD_FORCE_SUBSHELL 0x02 /* Shell needs to force a subshell. */
155#define CMD_INVERT_RETURN 0x04 /* Invert the exit value. */
156#define CMD_IGNORE_RETURN 0x08 /* Ignore the exit value. For set -e. */
157#define CMD_NO_FUNCTIONS 0x10 /* Ignore functions during command lookup. */
158#define CMD_INHIBIT_EXPANSION 0x20 /* Do not expand the command words. */
159#define CMD_NO_FORK 0x40 /* Don't fork; just call execve */
ccc6cda3
JA
160#define CMD_TIME_PIPELINE 0x80 /* Time a pipeline */
161#define CMD_TIME_POSIX 0x100 /* time -p; use POSIX.2 time output spec. */
d166f048
JA
162#define CMD_AMPERSAND 0x200 /* command & */
163#define CMD_STDIN_REDIR 0x400 /* async command needs implicit </dev/null */
f73dda09 164#define CMD_COMMAND_BUILTIN 0x0800 /* command executed by `command' builtin */
3185942a 165#define CMD_COPROC_SUBSHELL 0x1000
726f6388
JA
166
167/* What a command looks like. */
168typedef struct command {
169 enum command_type type; /* FOR CASE WHILE IF CONNECTION or SIMPLE. */
170 int flags; /* Flags controlling execution environment. */
171 int line; /* line number the command starts on */
172 REDIRECT *redirects; /* Special redirects for FOR CASE, etc. */
173 union {
174 struct for_com *For;
175 struct case_com *Case;
176 struct while_com *While;
177 struct if_com *If;
178 struct connection *Connection;
179 struct simple_com *Simple;
180 struct function_def *Function_def;
181 struct group_com *Group;
182#if defined (SELECT_COMMAND)
183 struct select_com *Select;
cce855bc
JA
184#endif
185#if defined (DPAREN_ARITHMETIC)
186 struct arith_com *Arith;
187#endif
188#if defined (COND_COMMAND)
189 struct cond_com *Cond;
726f6388 190#endif
bb70624e
JA
191#if defined (ARITH_FOR_COMMAND)
192 struct arith_for_com *ArithFor;
193#endif
194 struct subshell_com *Subshell;
3185942a 195 struct coproc_com *Coproc;
726f6388
JA
196 } value;
197} COMMAND;
198
199/* Structure used to represent the CONNECTION type. */
200typedef struct connection {
201 int ignore; /* Unused; simplifies make_command (). */
202 COMMAND *first; /* Pointer to the first command. */
203 COMMAND *second; /* Pointer to the second command. */
204 int connector; /* What separates this command from others. */
205} CONNECTION;
206
207/* Structures used to represent the CASE command. */
208
3185942a
JA
209/* Values for FLAGS word in a PATTERN_LIST */
210#define CASEPAT_FALLTHROUGH 0x01
211#define CASEPAT_TESTNEXT 0x02
212
726f6388
JA
213/* Pattern/action structure for CASE_COM. */
214typedef struct pattern_list {
215 struct pattern_list *next; /* Clause to try in case this one failed. */
216 WORD_LIST *patterns; /* Linked list of patterns to test. */
217 COMMAND *action; /* Thing to execute if a pattern matches. */
95732b49 218 int flags;
726f6388
JA
219} PATTERN_LIST;
220
221/* The CASE command. */
222typedef struct case_com {
223 int flags; /* See description of CMD flags. */
b80f6443 224 int line; /* line number the `case' keyword appears on */
726f6388
JA
225 WORD_DESC *word; /* The thing to test. */
226 PATTERN_LIST *clauses; /* The clauses to test against, or NULL. */
227} CASE_COM;
228
229/* FOR command. */
230typedef struct for_com {
231 int flags; /* See description of CMD flags. */
b80f6443 232 int line; /* line number the `for' 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 successive
237 members of MAP_LIST. */
238} FOR_COM;
239
bb70624e
JA
240#if defined (ARITH_FOR_COMMAND)
241typedef struct arith_for_com {
242 int flags;
243 int line; /* generally used for error messages */
244 WORD_LIST *init;
245 WORD_LIST *test;
246 WORD_LIST *step;
247 COMMAND *action;
248} ARITH_FOR_COM;
249#endif
250
726f6388
JA
251#if defined (SELECT_COMMAND)
252/* KSH SELECT command. */
253typedef struct select_com {
254 int flags; /* See description of CMD flags. */
b80f6443 255 int line; /* line number the `select' keyword appears on */
726f6388
JA
256 WORD_DESC *name; /* The variable name to get mapped over. */
257 WORD_LIST *map_list; /* The things to map over. This is never NULL. */
258 COMMAND *action; /* The action to execute.
259 During execution, NAME is bound to the member of
260 MAP_LIST chosen by the user. */
261} SELECT_COM;
262#endif /* SELECT_COMMAND */
263
264/* IF command. */
265typedef struct if_com {
266 int flags; /* See description of CMD flags. */
267 COMMAND *test; /* Thing to test. */
268 COMMAND *true_case; /* What to do if the test returned non-zero. */
269 COMMAND *false_case; /* What to do if the test returned zero. */
270} IF_COM;
271
272/* WHILE command. */
273typedef struct while_com {
274 int flags; /* See description of CMD flags. */
275 COMMAND *test; /* Thing to test. */
276 COMMAND *action; /* Thing to do while test is non-zero. */
277} WHILE_COM;
278
cce855bc
JA
279#if defined (DPAREN_ARITHMETIC)
280/* The arithmetic evaluation command, ((...)). Just a set of flags and
281 a WORD_LIST, of which the first element is the only one used, for the
282 time being. */
283typedef struct arith_com {
284 int flags;
cce855bc 285 int line;
b80f6443 286 WORD_LIST *exp;
cce855bc
JA
287} ARITH_COM;
288#endif /* DPAREN_ARITHMETIC */
289
290/* The conditional command, [[...]]. This is a binary tree -- we slippped
291 a recursive-descent parser into the YACC grammar to parse it. */
292#define COND_AND 1
293#define COND_OR 2
294#define COND_UNARY 3
295#define COND_BINARY 4
296#define COND_TERM 5
297#define COND_EXPR 6
298
299typedef struct cond_com {
300 int flags;
301 int line;
302 int type;
303 WORD_DESC *op;
304 struct cond_com *left, *right;
305} COND_COM;
306
726f6388
JA
307/* The "simple" command. Just a collection of words and redirects. */
308typedef struct simple_com {
309 int flags; /* See description of CMD flags. */
b80f6443 310 int line; /* line number the command starts on */
726f6388
JA
311 WORD_LIST *words; /* The program name, the arguments,
312 variable assignments, etc. */
313 REDIRECT *redirects; /* Redirections to perform. */
726f6388
JA
314} SIMPLE_COM;
315
ccc6cda3 316/* The "function definition" command. */
726f6388 317typedef struct function_def {
cce855bc 318 int flags; /* See description of CMD flags. */
b80f6443 319 int line; /* Line number the function def starts on. */
726f6388
JA
320 WORD_DESC *name; /* The name of the function. */
321 COMMAND *command; /* The parsed execution tree. */
b80f6443 322 char *source_file; /* file in which function was defined, if any */
726f6388
JA
323} FUNCTION_DEF;
324
ccc6cda3
JA
325/* A command that is `grouped' allows pipes and redirections to affect all
326 commands in the group. */
726f6388
JA
327typedef struct group_com {
328 int ignore; /* See description of CMD flags. */
329 COMMAND *command;
330} GROUP_COM;
331
bb70624e
JA
332typedef struct subshell_com {
333 int flags;
334 COMMAND *command;
335} SUBSHELL_COM;
336
3185942a
JA
337typedef struct coproc {
338 char *c_name;
339 pid_t c_pid;
340 int c_rfd;
341 int c_wfd;
342 int c_rsave;
343 int c_wsave;
344 int c_flags;
345 int c_status;
346} Coproc;
347
348typedef struct coproc_com {
349 int flags;
350 char *name;
351 COMMAND *command;
352} COPROC_COM;
353
726f6388 354extern COMMAND *global_command;
3185942a 355extern Coproc sh_coproc;
726f6388 356
b72432fd
JA
357/* Possible command errors */
358#define CMDERR_DEFAULT 0
359#define CMDERR_BADTYPE 1
360#define CMDERR_BADCONN 2
361#define CMDERR_BADJUMP 3
362
363#define CMDERR_LAST 3
364
726f6388
JA
365/* Forward declarations of functions declared in copy_cmd.c. */
366
b80f6443
JA
367extern FUNCTION_DEF *copy_function_def_contents __P((FUNCTION_DEF *, FUNCTION_DEF *));
368extern FUNCTION_DEF *copy_function_def __P((FUNCTION_DEF *));
369
726f6388
JA
370extern WORD_DESC *copy_word __P((WORD_DESC *));
371extern WORD_LIST *copy_word_list __P((WORD_LIST *));
372extern REDIRECT *copy_redirect __P((REDIRECT *));
373extern REDIRECT *copy_redirects __P((REDIRECT *));
374extern COMMAND *copy_command __P((COMMAND *));
375
ccc6cda3 376#endif /* _COMMAND_H_ */