]> git.ipfire.org Git - thirdparty/bash.git/blame - command.h
Bash-5.1 patch 16: fix interpretation of multiple instances of ! in [[ conditional...
[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
8868edaf 4/* Copyright (C) 1993-2020 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. */
8868edaf
CR
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) /* $@ and its special handling -- UNUSED */
85#define W_DOLLARSTAR (1 << 9) /* $* and its special handling -- UNUSED */
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_ITILDE (1 << 13) /* Internal flag for word expansion */
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) /* 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_NOASSNTILDE (1 << 29) /* don't do tilde expansion like an assignment statement */
106#define W_FORCELOCAL (1 << 30) /* force assignments to be to local variables, non-fatal on assignment errors */
a0c0a00f
CR
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 */
8868edaf
CR
115#define PF_EXPANDRHS 0x20 /* same as W_EXPANDRHS */
116#define PF_ALLINDS 0x40 /* array, act as if [@] was supplied */
ccc6cda3
JA
117
118/* Possible values for subshell_environment */
119#define SUBSHELL_ASYNC 0x01 /* subshell caused by `command &' */
120#define SUBSHELL_PAREN 0x02 /* subshell caused by ( ... ) */
121#define SUBSHELL_COMSUB 0x04 /* subshell caused by `command` or $(command) */
122#define SUBSHELL_FORK 0x08 /* subshell caused by executing a disk command */
28ef6c31 123#define SUBSHELL_PIPE 0x10 /* subshell from a pipeline element */
3185942a
JA
124#define SUBSHELL_PROCSUB 0x20 /* subshell caused by <(command) or >(command) */
125#define SUBSHELL_COPROC 0x40 /* subshell from a coproc pipeline */
495aee44 126#define SUBSHELL_RESETTRAP 0x80 /* subshell needs to reset trap strings on first call to trap */
15409324 127#define SUBSHELL_IGNTRAP 0x100 /* subshell should reset trapped signals from trap_handler */
ccc6cda3 128
726f6388
JA
129/* A structure which represents a word. */
130typedef struct word_desc {
131 char *word; /* Zero terminated string. */
ccc6cda3 132 int flags; /* Flags associated with this word. */
726f6388
JA
133} WORD_DESC;
134
135/* A linked list of words. */
136typedef struct word_list {
137 struct word_list *next;
138 WORD_DESC *word;
139} WORD_LIST;
140
141
142/* **************************************************************** */
143/* */
144/* Shell Command Structs */
145/* */
146/* **************************************************************** */
147
f73dda09
JA
148/* What a redirection descriptor looks like. If the redirection instruction
149 is ri_duplicating_input or ri_duplicating_output, use DEST, otherwise
150 use the file in FILENAME. Out-of-range descriptors are identified by a
151 negative DEST. */
726f6388
JA
152
153typedef union {
f73dda09 154 int dest; /* Place to redirect REDIRECTOR to, or ... */
726f6388
JA
155 WORD_DESC *filename; /* filename to redirect to. */
156} REDIRECTEE;
157
f73dda09
JA
158/* Structure describing a redirection. If REDIRECTOR is negative, the parser
159 (or translator in redir.c) encountered an out-of-range file descriptor. */
726f6388
JA
160typedef struct redirect {
161 struct redirect *next; /* Next element, or NULL. */
0001803f
CR
162 REDIRECTEE redirector; /* Descriptor or varname to be redirected. */
163 int rflags; /* Private flags for this redirection */
726f6388
JA
164 int flags; /* Flag value for `open'. */
165 enum r_instruction instruction; /* What to do with the information. */
166 REDIRECTEE redirectee; /* File descriptor or filename */
167 char *here_doc_eof; /* The word that appeared in <<foo. */
168} REDIRECT;
169
170/* An element used in parsing. A single word or a single redirection.
171 This is an ephemeral construct. */
172typedef struct element {
173 WORD_DESC *word;
174 REDIRECT *redirect;
175} ELEMENT;
176
177/* Possible values for command->flags. */
178#define CMD_WANT_SUBSHELL 0x01 /* User wants a subshell: ( command ) */
179#define CMD_FORCE_SUBSHELL 0x02 /* Shell needs to force a subshell. */
180#define CMD_INVERT_RETURN 0x04 /* Invert the exit value. */
181#define CMD_IGNORE_RETURN 0x08 /* Ignore the exit value. For set -e. */
182#define CMD_NO_FUNCTIONS 0x10 /* Ignore functions during command lookup. */
183#define CMD_INHIBIT_EXPANSION 0x20 /* Do not expand the command words. */
184#define CMD_NO_FORK 0x40 /* Don't fork; just call execve */
ccc6cda3
JA
185#define CMD_TIME_PIPELINE 0x80 /* Time a pipeline */
186#define CMD_TIME_POSIX 0x100 /* time -p; use POSIX.2 time output spec. */
d166f048
JA
187#define CMD_AMPERSAND 0x200 /* command & */
188#define CMD_STDIN_REDIR 0x400 /* async command needs implicit </dev/null */
f73dda09 189#define CMD_COMMAND_BUILTIN 0x0800 /* command executed by `command' builtin */
3185942a 190#define CMD_COPROC_SUBSHELL 0x1000
495aee44 191#define CMD_LASTPIPE 0x2000
a0c0a00f 192#define CMD_STDPATH 0x4000 /* use standard path for command lookup */
41f5420d 193#define CMD_TRY_OPTIMIZING 0x8000 /* try to optimize this simple command */
726f6388
JA
194
195/* What a command looks like. */
196typedef struct command {
197 enum command_type type; /* FOR CASE WHILE IF CONNECTION or SIMPLE. */
198 int flags; /* Flags controlling execution environment. */
199 int line; /* line number the command starts on */
200 REDIRECT *redirects; /* Special redirects for FOR CASE, etc. */
201 union {
202 struct for_com *For;
203 struct case_com *Case;
204 struct while_com *While;
205 struct if_com *If;
206 struct connection *Connection;
207 struct simple_com *Simple;
208 struct function_def *Function_def;
209 struct group_com *Group;
210#if defined (SELECT_COMMAND)
211 struct select_com *Select;
cce855bc
JA
212#endif
213#if defined (DPAREN_ARITHMETIC)
214 struct arith_com *Arith;
215#endif
216#if defined (COND_COMMAND)
217 struct cond_com *Cond;
726f6388 218#endif
bb70624e
JA
219#if defined (ARITH_FOR_COMMAND)
220 struct arith_for_com *ArithFor;
221#endif
222 struct subshell_com *Subshell;
3185942a 223 struct coproc_com *Coproc;
726f6388
JA
224 } value;
225} COMMAND;
226
227/* Structure used to represent the CONNECTION type. */
228typedef struct connection {
229 int ignore; /* Unused; simplifies make_command (). */
230 COMMAND *first; /* Pointer to the first command. */
231 COMMAND *second; /* Pointer to the second command. */
232 int connector; /* What separates this command from others. */
233} CONNECTION;
234
235/* Structures used to represent the CASE command. */
236
3185942a
JA
237/* Values for FLAGS word in a PATTERN_LIST */
238#define CASEPAT_FALLTHROUGH 0x01
239#define CASEPAT_TESTNEXT 0x02
240
726f6388
JA
241/* Pattern/action structure for CASE_COM. */
242typedef struct pattern_list {
243 struct pattern_list *next; /* Clause to try in case this one failed. */
244 WORD_LIST *patterns; /* Linked list of patterns to test. */
245 COMMAND *action; /* Thing to execute if a pattern matches. */
95732b49 246 int flags;
726f6388
JA
247} PATTERN_LIST;
248
249/* The CASE command. */
250typedef struct case_com {
251 int flags; /* See description of CMD flags. */
b80f6443 252 int line; /* line number the `case' keyword appears on */
726f6388
JA
253 WORD_DESC *word; /* The thing to test. */
254 PATTERN_LIST *clauses; /* The clauses to test against, or NULL. */
255} CASE_COM;
256
257/* FOR command. */
258typedef struct for_com {
259 int flags; /* See description of CMD flags. */
b80f6443 260 int line; /* line number the `for' keyword appears on */
726f6388
JA
261 WORD_DESC *name; /* The variable name to get mapped over. */
262 WORD_LIST *map_list; /* The things to map over. This is never NULL. */
263 COMMAND *action; /* The action to execute.
264 During execution, NAME is bound to successive
265 members of MAP_LIST. */
266} FOR_COM;
267
bb70624e
JA
268#if defined (ARITH_FOR_COMMAND)
269typedef struct arith_for_com {
270 int flags;
271 int line; /* generally used for error messages */
272 WORD_LIST *init;
273 WORD_LIST *test;
274 WORD_LIST *step;
275 COMMAND *action;
276} ARITH_FOR_COM;
277#endif
278
726f6388
JA
279#if defined (SELECT_COMMAND)
280/* KSH SELECT command. */
281typedef struct select_com {
282 int flags; /* See description of CMD flags. */
b80f6443 283 int line; /* line number the `select' keyword appears on */
726f6388
JA
284 WORD_DESC *name; /* The variable name to get mapped over. */
285 WORD_LIST *map_list; /* The things to map over. This is never NULL. */
286 COMMAND *action; /* The action to execute.
287 During execution, NAME is bound to the member of
288 MAP_LIST chosen by the user. */
289} SELECT_COM;
290#endif /* SELECT_COMMAND */
291
292/* IF command. */
293typedef struct if_com {
294 int flags; /* See description of CMD flags. */
295 COMMAND *test; /* Thing to test. */
296 COMMAND *true_case; /* What to do if the test returned non-zero. */
297 COMMAND *false_case; /* What to do if the test returned zero. */
298} IF_COM;
299
300/* WHILE command. */
301typedef struct while_com {
302 int flags; /* See description of CMD flags. */
303 COMMAND *test; /* Thing to test. */
304 COMMAND *action; /* Thing to do while test is non-zero. */
305} WHILE_COM;
306
cce855bc
JA
307#if defined (DPAREN_ARITHMETIC)
308/* The arithmetic evaluation command, ((...)). Just a set of flags and
309 a WORD_LIST, of which the first element is the only one used, for the
310 time being. */
311typedef struct arith_com {
312 int flags;
cce855bc 313 int line;
b80f6443 314 WORD_LIST *exp;
cce855bc
JA
315} ARITH_COM;
316#endif /* DPAREN_ARITHMETIC */
317
ac50fbac 318/* The conditional command, [[...]]. This is a binary tree -- we slipped
cce855bc
JA
319 a recursive-descent parser into the YACC grammar to parse it. */
320#define COND_AND 1
321#define COND_OR 2
322#define COND_UNARY 3
323#define COND_BINARY 4
324#define COND_TERM 5
325#define COND_EXPR 6
326
327typedef struct cond_com {
328 int flags;
329 int line;
330 int type;
331 WORD_DESC *op;
332 struct cond_com *left, *right;
333} COND_COM;
334
726f6388
JA
335/* The "simple" command. Just a collection of words and redirects. */
336typedef struct simple_com {
337 int flags; /* See description of CMD flags. */
b80f6443 338 int line; /* line number the command starts on */
726f6388
JA
339 WORD_LIST *words; /* The program name, the arguments,
340 variable assignments, etc. */
341 REDIRECT *redirects; /* Redirections to perform. */
726f6388
JA
342} SIMPLE_COM;
343
ccc6cda3 344/* The "function definition" command. */
726f6388 345typedef struct function_def {
cce855bc 346 int flags; /* See description of CMD flags. */
b80f6443 347 int line; /* Line number the function def starts on. */
726f6388
JA
348 WORD_DESC *name; /* The name of the function. */
349 COMMAND *command; /* The parsed execution tree. */
b80f6443 350 char *source_file; /* file in which function was defined, if any */
726f6388
JA
351} FUNCTION_DEF;
352
ccc6cda3
JA
353/* A command that is `grouped' allows pipes and redirections to affect all
354 commands in the group. */
726f6388
JA
355typedef struct group_com {
356 int ignore; /* See description of CMD flags. */
357 COMMAND *command;
358} GROUP_COM;
359
bb70624e
JA
360typedef struct subshell_com {
361 int flags;
d233b485 362 int line;
bb70624e
JA
363 COMMAND *command;
364} SUBSHELL_COM;
365
17345e5a
JA
366#define COPROC_RUNNING 0x01
367#define COPROC_DEAD 0x02
368
3185942a
JA
369typedef struct coproc {
370 char *c_name;
371 pid_t c_pid;
372 int c_rfd;
373 int c_wfd;
374 int c_rsave;
375 int c_wsave;
376 int c_flags;
377 int c_status;
ac50fbac 378 int c_lock;
3185942a
JA
379} Coproc;
380
381typedef struct coproc_com {
382 int flags;
383 char *name;
384 COMMAND *command;
385} COPROC_COM;
386
726f6388 387extern COMMAND *global_command;
3185942a 388extern Coproc sh_coproc;
726f6388 389
b72432fd
JA
390/* Possible command errors */
391#define CMDERR_DEFAULT 0
392#define CMDERR_BADTYPE 1
393#define CMDERR_BADCONN 2
394#define CMDERR_BADJUMP 3
395
396#define CMDERR_LAST 3
397
726f6388
JA
398/* Forward declarations of functions declared in copy_cmd.c. */
399
8868edaf
CR
400extern FUNCTION_DEF *copy_function_def_contents PARAMS((FUNCTION_DEF *, FUNCTION_DEF *));
401extern FUNCTION_DEF *copy_function_def PARAMS((FUNCTION_DEF *));
b80f6443 402
8868edaf
CR
403extern WORD_DESC *copy_word PARAMS((WORD_DESC *));
404extern WORD_LIST *copy_word_list PARAMS((WORD_LIST *));
405extern REDIRECT *copy_redirect PARAMS((REDIRECT *));
406extern REDIRECT *copy_redirects PARAMS((REDIRECT *));
407extern COMMAND *copy_command PARAMS((COMMAND *));
726f6388 408
ccc6cda3 409#endif /* _COMMAND_H_ */