]> git.ipfire.org Git - thirdparty/bash.git/blame - command.h
fix for SIGINT in sourced script
[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
a0c0a00f 4/* Copyright (C) 1993-2016 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
0001803f
CR
38/* Redirection flags; values for rflags */
39#define REDIR_VARASSIGN 0x01
40
ccc6cda3
JA
41/* Redirection errors. */
42#define AMBIGUOUS_REDIRECT -1
43#define NOCLOBBER_REDIRECT -2
44#define RESTRICTED_REDIRECT -3 /* can only happen in restricted shells. */
d166f048 45#define HEREDOC_REDIRECT -4 /* here-doc temp file can't be created */
0001803f 46#define BADVAR_REDIRECT -5 /* something wrong with {varname}redir */
ccc6cda3 47
28ef6c31
JA
48#define CLOBBERING_REDIRECT(ri) \
49 (ri == r_output_direction || ri == r_err_and_out)
50
ccc6cda3 51#define OUTPUT_REDIRECT(ri) \
3185942a 52 (ri == r_output_direction || ri == r_input_output || ri == r_err_and_out || ri == r_append_err_and_out)
ccc6cda3
JA
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 || \
3185942a 62 ri == r_append_err_and_out || \
ccc6cda3
JA
63 ri == r_output_force)
64
7117c2d2
JA
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
726f6388
JA
70/* Command Types: */
71enum command_type { cm_for, cm_case, cm_while, cm_if, cm_simple, cm_select,
cce855bc 72 cm_connection, cm_function_def, cm_until, cm_group,
3185942a 73 cm_arith, cm_cond, cm_arith_for, cm_subshell, cm_coproc };
726f6388 74
ccc6cda3 75/* Possible values for the `flags' field of a WORD_DESC. */
0628567a
JA
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. */
ac50fbac 79#define W_SPLITSPACE 0x000008 /* Split this word on " " regardless of IFS */
0001803f 80#define W_NOSPLIT 0x000010 /* Do not perform word splitting on this word because ifs is empty string. */
0628567a 81#define W_NOGLOB 0x000020 /* Do not perform globbing on this word. */
0001803f 82#define W_NOSPLIT2 0x000040 /* Don't split word except for $@ expansion (using spaces) because context does not allow it. */
0628567a
JA
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 */
3185942a
JA
97#define W_HASCTLESC 0x200000 /* word contains literal CTLESC characters */
98#define W_ASSIGNASSOC 0x400000 /* word looks like associative array assignment */
ac50fbac
CR
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 */
a0c0a00f
CR
103#define W_COMPLETE 0x8000000 /* word is being expanded for completion */
104
105/* Flags for the `pflags' argument to param_expand() and various
106 parameter_brace_expand_xxx functions; also used for string_list_dollar_at */
107#define PF_NOCOMSUB 0x01 /* Do not perform command substitution */
108#define PF_IGNUNBOUND 0x02 /* ignore unbound vars even if -u set */
109#define PF_NOSPLIT2 0x04 /* same as W_NOSPLIT2 */
110#define PF_ASSIGNRHS 0x08 /* same as W_ASSIGNRHS */
111#define PF_COMPLETE 0x10 /* same as W_COMPLETE, sets SX_COMPLETE */
ccc6cda3
JA
112
113/* Possible values for subshell_environment */
114#define SUBSHELL_ASYNC 0x01 /* subshell caused by `command &' */
115#define SUBSHELL_PAREN 0x02 /* subshell caused by ( ... ) */
116#define SUBSHELL_COMSUB 0x04 /* subshell caused by `command` or $(command) */
117#define SUBSHELL_FORK 0x08 /* subshell caused by executing a disk command */
28ef6c31 118#define SUBSHELL_PIPE 0x10 /* subshell from a pipeline element */
3185942a
JA
119#define SUBSHELL_PROCSUB 0x20 /* subshell caused by <(command) or >(command) */
120#define SUBSHELL_COPROC 0x40 /* subshell from a coproc pipeline */
495aee44 121#define SUBSHELL_RESETTRAP 0x80 /* subshell needs to reset trap strings on first call to trap */
ccc6cda3 122
726f6388
JA
123/* A structure which represents a word. */
124typedef struct word_desc {
125 char *word; /* Zero terminated string. */
ccc6cda3 126 int flags; /* Flags associated with this word. */
726f6388
JA
127} WORD_DESC;
128
129/* A linked list of words. */
130typedef struct word_list {
131 struct word_list *next;
132 WORD_DESC *word;
133} WORD_LIST;
134
135
136/* **************************************************************** */
137/* */
138/* Shell Command Structs */
139/* */
140/* **************************************************************** */
141
f73dda09
JA
142/* What a redirection descriptor looks like. If the redirection instruction
143 is ri_duplicating_input or ri_duplicating_output, use DEST, otherwise
144 use the file in FILENAME. Out-of-range descriptors are identified by a
145 negative DEST. */
726f6388
JA
146
147typedef union {
f73dda09 148 int dest; /* Place to redirect REDIRECTOR to, or ... */
726f6388
JA
149 WORD_DESC *filename; /* filename to redirect to. */
150} REDIRECTEE;
151
f73dda09
JA
152/* Structure describing a redirection. If REDIRECTOR is negative, the parser
153 (or translator in redir.c) encountered an out-of-range file descriptor. */
726f6388
JA
154typedef struct redirect {
155 struct redirect *next; /* Next element, or NULL. */
0001803f
CR
156 REDIRECTEE redirector; /* Descriptor or varname to be redirected. */
157 int rflags; /* Private flags for this redirection */
726f6388
JA
158 int flags; /* Flag value for `open'. */
159 enum r_instruction instruction; /* What to do with the information. */
160 REDIRECTEE redirectee; /* File descriptor or filename */
161 char *here_doc_eof; /* The word that appeared in <<foo. */
162} REDIRECT;
163
164/* An element used in parsing. A single word or a single redirection.
165 This is an ephemeral construct. */
166typedef struct element {
167 WORD_DESC *word;
168 REDIRECT *redirect;
169} ELEMENT;
170
171/* Possible values for command->flags. */
172#define CMD_WANT_SUBSHELL 0x01 /* User wants a subshell: ( command ) */
173#define CMD_FORCE_SUBSHELL 0x02 /* Shell needs to force a subshell. */
174#define CMD_INVERT_RETURN 0x04 /* Invert the exit value. */
175#define CMD_IGNORE_RETURN 0x08 /* Ignore the exit value. For set -e. */
176#define CMD_NO_FUNCTIONS 0x10 /* Ignore functions during command lookup. */
177#define CMD_INHIBIT_EXPANSION 0x20 /* Do not expand the command words. */
178#define CMD_NO_FORK 0x40 /* Don't fork; just call execve */
ccc6cda3
JA
179#define CMD_TIME_PIPELINE 0x80 /* Time a pipeline */
180#define CMD_TIME_POSIX 0x100 /* time -p; use POSIX.2 time output spec. */
d166f048
JA
181#define CMD_AMPERSAND 0x200 /* command & */
182#define CMD_STDIN_REDIR 0x400 /* async command needs implicit </dev/null */
f73dda09 183#define CMD_COMMAND_BUILTIN 0x0800 /* command executed by `command' builtin */
3185942a 184#define CMD_COPROC_SUBSHELL 0x1000
495aee44 185#define CMD_LASTPIPE 0x2000
a0c0a00f 186#define CMD_STDPATH 0x4000 /* use standard path for command lookup */
726f6388
JA
187
188/* What a command looks like. */
189typedef struct command {
190 enum command_type type; /* FOR CASE WHILE IF CONNECTION or SIMPLE. */
191 int flags; /* Flags controlling execution environment. */
192 int line; /* line number the command starts on */
193 REDIRECT *redirects; /* Special redirects for FOR CASE, etc. */
194 union {
195 struct for_com *For;
196 struct case_com *Case;
197 struct while_com *While;
198 struct if_com *If;
199 struct connection *Connection;
200 struct simple_com *Simple;
201 struct function_def *Function_def;
202 struct group_com *Group;
203#if defined (SELECT_COMMAND)
204 struct select_com *Select;
cce855bc
JA
205#endif
206#if defined (DPAREN_ARITHMETIC)
207 struct arith_com *Arith;
208#endif
209#if defined (COND_COMMAND)
210 struct cond_com *Cond;
726f6388 211#endif
bb70624e
JA
212#if defined (ARITH_FOR_COMMAND)
213 struct arith_for_com *ArithFor;
214#endif
215 struct subshell_com *Subshell;
3185942a 216 struct coproc_com *Coproc;
726f6388
JA
217 } value;
218} COMMAND;
219
220/* Structure used to represent the CONNECTION type. */
221typedef struct connection {
222 int ignore; /* Unused; simplifies make_command (). */
223 COMMAND *first; /* Pointer to the first command. */
224 COMMAND *second; /* Pointer to the second command. */
225 int connector; /* What separates this command from others. */
226} CONNECTION;
227
228/* Structures used to represent the CASE command. */
229
3185942a
JA
230/* Values for FLAGS word in a PATTERN_LIST */
231#define CASEPAT_FALLTHROUGH 0x01
232#define CASEPAT_TESTNEXT 0x02
233
726f6388
JA
234/* Pattern/action structure for CASE_COM. */
235typedef struct pattern_list {
236 struct pattern_list *next; /* Clause to try in case this one failed. */
237 WORD_LIST *patterns; /* Linked list of patterns to test. */
238 COMMAND *action; /* Thing to execute if a pattern matches. */
95732b49 239 int flags;
726f6388
JA
240} PATTERN_LIST;
241
242/* The CASE command. */
243typedef struct case_com {
244 int flags; /* See description of CMD flags. */
b80f6443 245 int line; /* line number the `case' keyword appears on */
726f6388
JA
246 WORD_DESC *word; /* The thing to test. */
247 PATTERN_LIST *clauses; /* The clauses to test against, or NULL. */
248} CASE_COM;
249
250/* FOR command. */
251typedef struct for_com {
252 int flags; /* See description of CMD flags. */
b80f6443 253 int line; /* line number the `for' keyword appears on */
726f6388
JA
254 WORD_DESC *name; /* The variable name to get mapped over. */
255 WORD_LIST *map_list; /* The things to map over. This is never NULL. */
256 COMMAND *action; /* The action to execute.
257 During execution, NAME is bound to successive
258 members of MAP_LIST. */
259} FOR_COM;
260
bb70624e
JA
261#if defined (ARITH_FOR_COMMAND)
262typedef struct arith_for_com {
263 int flags;
264 int line; /* generally used for error messages */
265 WORD_LIST *init;
266 WORD_LIST *test;
267 WORD_LIST *step;
268 COMMAND *action;
269} ARITH_FOR_COM;
270#endif
271
726f6388
JA
272#if defined (SELECT_COMMAND)
273/* KSH SELECT command. */
274typedef struct select_com {
275 int flags; /* See description of CMD flags. */
b80f6443 276 int line; /* line number the `select' keyword appears on */
726f6388
JA
277 WORD_DESC *name; /* The variable name to get mapped over. */
278 WORD_LIST *map_list; /* The things to map over. This is never NULL. */
279 COMMAND *action; /* The action to execute.
280 During execution, NAME is bound to the member of
281 MAP_LIST chosen by the user. */
282} SELECT_COM;
283#endif /* SELECT_COMMAND */
284
285/* IF command. */
286typedef struct if_com {
287 int flags; /* See description of CMD flags. */
288 COMMAND *test; /* Thing to test. */
289 COMMAND *true_case; /* What to do if the test returned non-zero. */
290 COMMAND *false_case; /* What to do if the test returned zero. */
291} IF_COM;
292
293/* WHILE command. */
294typedef struct while_com {
295 int flags; /* See description of CMD flags. */
296 COMMAND *test; /* Thing to test. */
297 COMMAND *action; /* Thing to do while test is non-zero. */
298} WHILE_COM;
299
cce855bc
JA
300#if defined (DPAREN_ARITHMETIC)
301/* The arithmetic evaluation command, ((...)). Just a set of flags and
302 a WORD_LIST, of which the first element is the only one used, for the
303 time being. */
304typedef struct arith_com {
305 int flags;
cce855bc 306 int line;
b80f6443 307 WORD_LIST *exp;
cce855bc
JA
308} ARITH_COM;
309#endif /* DPAREN_ARITHMETIC */
310
ac50fbac 311/* The conditional command, [[...]]. This is a binary tree -- we slipped
cce855bc
JA
312 a recursive-descent parser into the YACC grammar to parse it. */
313#define COND_AND 1
314#define COND_OR 2
315#define COND_UNARY 3
316#define COND_BINARY 4
317#define COND_TERM 5
318#define COND_EXPR 6
319
320typedef struct cond_com {
321 int flags;
322 int line;
323 int type;
324 WORD_DESC *op;
325 struct cond_com *left, *right;
326} COND_COM;
327
726f6388
JA
328/* The "simple" command. Just a collection of words and redirects. */
329typedef struct simple_com {
330 int flags; /* See description of CMD flags. */
b80f6443 331 int line; /* line number the command starts on */
726f6388
JA
332 WORD_LIST *words; /* The program name, the arguments,
333 variable assignments, etc. */
334 REDIRECT *redirects; /* Redirections to perform. */
726f6388
JA
335} SIMPLE_COM;
336
ccc6cda3 337/* The "function definition" command. */
726f6388 338typedef struct function_def {
cce855bc 339 int flags; /* See description of CMD flags. */
b80f6443 340 int line; /* Line number the function def starts on. */
726f6388
JA
341 WORD_DESC *name; /* The name of the function. */
342 COMMAND *command; /* The parsed execution tree. */
b80f6443 343 char *source_file; /* file in which function was defined, if any */
726f6388
JA
344} FUNCTION_DEF;
345
ccc6cda3
JA
346/* A command that is `grouped' allows pipes and redirections to affect all
347 commands in the group. */
726f6388
JA
348typedef struct group_com {
349 int ignore; /* See description of CMD flags. */
350 COMMAND *command;
351} GROUP_COM;
352
bb70624e
JA
353typedef struct subshell_com {
354 int flags;
355 COMMAND *command;
356} SUBSHELL_COM;
357
17345e5a
JA
358#define COPROC_RUNNING 0x01
359#define COPROC_DEAD 0x02
360
3185942a
JA
361typedef struct coproc {
362 char *c_name;
363 pid_t c_pid;
364 int c_rfd;
365 int c_wfd;
366 int c_rsave;
367 int c_wsave;
368 int c_flags;
369 int c_status;
ac50fbac 370 int c_lock;
3185942a
JA
371} Coproc;
372
373typedef struct coproc_com {
374 int flags;
375 char *name;
376 COMMAND *command;
377} COPROC_COM;
378
726f6388 379extern COMMAND *global_command;
3185942a 380extern Coproc sh_coproc;
726f6388 381
b72432fd
JA
382/* Possible command errors */
383#define CMDERR_DEFAULT 0
384#define CMDERR_BADTYPE 1
385#define CMDERR_BADCONN 2
386#define CMDERR_BADJUMP 3
387
388#define CMDERR_LAST 3
389
726f6388
JA
390/* Forward declarations of functions declared in copy_cmd.c. */
391
b80f6443
JA
392extern FUNCTION_DEF *copy_function_def_contents __P((FUNCTION_DEF *, FUNCTION_DEF *));
393extern FUNCTION_DEF *copy_function_def __P((FUNCTION_DEF *));
394
726f6388
JA
395extern WORD_DESC *copy_word __P((WORD_DESC *));
396extern WORD_LIST *copy_word_list __P((WORD_LIST *));
397extern REDIRECT *copy_redirect __P((REDIRECT *));
398extern REDIRECT *copy_redirects __P((REDIRECT *));
399extern COMMAND *copy_command __P((COMMAND *));
400
ccc6cda3 401#endif /* _COMMAND_H_ */