]>
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 | |
20 | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
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, | |
30 | r_appending_to, r_reading_until, r_duplicating_input, | |
31 | r_duplicating_output, r_deblank_reading_until, r_close_this, | |
32 | r_err_and_out, r_input_output, r_output_force, | |
33 | r_duplicating_input_word, r_duplicating_output_word | |
34 | }; | |
35 | ||
ccc6cda3 JA |
36 | /* Redirection errors. */ |
37 | #define AMBIGUOUS_REDIRECT -1 | |
38 | #define NOCLOBBER_REDIRECT -2 | |
39 | #define RESTRICTED_REDIRECT -3 /* can only happen in restricted shells. */ | |
d166f048 | 40 | #define HEREDOC_REDIRECT -4 /* here-doc temp file can't be created */ |
ccc6cda3 JA |
41 | |
42 | #define OUTPUT_REDIRECT(ri) \ | |
43 | (ri == r_output_direction || ri == r_input_output || ri == r_err_and_out) | |
44 | ||
45 | #define INPUT_REDIRECT(ri) \ | |
46 | (ri == r_input_direction || ri == r_inputa_direction || ri == r_input_output) | |
47 | ||
48 | #define WRITE_REDIRECT(ri) \ | |
49 | (ri == r_output_direction || \ | |
50 | ri == r_input_output || \ | |
51 | ri == r_err_and_out || \ | |
52 | ri == r_appending_to || \ | |
53 | ri == r_output_force) | |
54 | ||
726f6388 JA |
55 | /* Command Types: */ |
56 | enum command_type { cm_for, cm_case, cm_while, cm_if, cm_simple, cm_select, | |
cce855bc JA |
57 | cm_connection, cm_function_def, cm_until, cm_group, |
58 | cm_arith, cm_cond }; | |
726f6388 | 59 | |
ccc6cda3 JA |
60 | /* Possible values for the `flags' field of a WORD_DESC. */ |
61 | #define W_HASDOLLAR 0x01 /* Dollar sign present. */ | |
62 | #define W_QUOTED 0x02 /* Some form of quote character is present. */ | |
63 | #define W_ASSIGNMENT 0x04 /* This word is a variable assignment. */ | |
64 | #define W_GLOBEXP 0x08 /* This word is the result of a glob expansion. */ | |
65 | #define W_NOSPLIT 0x10 /* Do not perform word splitting on this word. */ | |
b72432fd | 66 | #define W_NOGLOB 0x20 /* Do not perform globbing on this word. */ |
ccc6cda3 JA |
67 | |
68 | /* Possible values for subshell_environment */ | |
69 | #define SUBSHELL_ASYNC 0x01 /* subshell caused by `command &' */ | |
70 | #define SUBSHELL_PAREN 0x02 /* subshell caused by ( ... ) */ | |
71 | #define SUBSHELL_COMSUB 0x04 /* subshell caused by `command` or $(command) */ | |
72 | #define SUBSHELL_FORK 0x08 /* subshell caused by executing a disk command */ | |
73 | ||
726f6388 JA |
74 | /* A structure which represents a word. */ |
75 | typedef struct word_desc { | |
76 | char *word; /* Zero terminated string. */ | |
ccc6cda3 | 77 | int flags; /* Flags associated with this word. */ |
726f6388 JA |
78 | } WORD_DESC; |
79 | ||
80 | /* A linked list of words. */ | |
81 | typedef struct word_list { | |
82 | struct word_list *next; | |
83 | WORD_DESC *word; | |
84 | } WORD_LIST; | |
85 | ||
86 | ||
87 | /* **************************************************************** */ | |
88 | /* */ | |
89 | /* Shell Command Structs */ | |
90 | /* */ | |
91 | /* **************************************************************** */ | |
92 | ||
93 | /* What a redirection descriptor looks like. If FLAGS is IS_DESCRIPTOR, | |
94 | then we use REDIRECTEE.DEST, else we use the file specified. */ | |
95 | ||
96 | typedef union { | |
97 | long dest; /* Place to redirect REDIRECTOR to, or ... */ | |
98 | WORD_DESC *filename; /* filename to redirect to. */ | |
99 | } REDIRECTEE; | |
100 | ||
101 | typedef struct redirect { | |
102 | struct redirect *next; /* Next element, or NULL. */ | |
103 | int redirector; /* Descriptor to be redirected. */ | |
104 | int flags; /* Flag value for `open'. */ | |
105 | enum r_instruction instruction; /* What to do with the information. */ | |
106 | REDIRECTEE redirectee; /* File descriptor or filename */ | |
107 | char *here_doc_eof; /* The word that appeared in <<foo. */ | |
108 | } REDIRECT; | |
109 | ||
110 | /* An element used in parsing. A single word or a single redirection. | |
111 | This is an ephemeral construct. */ | |
112 | typedef struct element { | |
113 | WORD_DESC *word; | |
114 | REDIRECT *redirect; | |
115 | } ELEMENT; | |
116 | ||
117 | /* Possible values for command->flags. */ | |
118 | #define CMD_WANT_SUBSHELL 0x01 /* User wants a subshell: ( command ) */ | |
119 | #define CMD_FORCE_SUBSHELL 0x02 /* Shell needs to force a subshell. */ | |
120 | #define CMD_INVERT_RETURN 0x04 /* Invert the exit value. */ | |
121 | #define CMD_IGNORE_RETURN 0x08 /* Ignore the exit value. For set -e. */ | |
122 | #define CMD_NO_FUNCTIONS 0x10 /* Ignore functions during command lookup. */ | |
123 | #define CMD_INHIBIT_EXPANSION 0x20 /* Do not expand the command words. */ | |
124 | #define CMD_NO_FORK 0x40 /* Don't fork; just call execve */ | |
ccc6cda3 JA |
125 | #define CMD_TIME_PIPELINE 0x80 /* Time a pipeline */ |
126 | #define CMD_TIME_POSIX 0x100 /* time -p; use POSIX.2 time output spec. */ | |
d166f048 JA |
127 | #define CMD_AMPERSAND 0x200 /* command & */ |
128 | #define CMD_STDIN_REDIR 0x400 /* async command needs implicit </dev/null */ | |
726f6388 JA |
129 | |
130 | /* What a command looks like. */ | |
131 | typedef struct command { | |
132 | enum command_type type; /* FOR CASE WHILE IF CONNECTION or SIMPLE. */ | |
133 | int flags; /* Flags controlling execution environment. */ | |
134 | int line; /* line number the command starts on */ | |
135 | REDIRECT *redirects; /* Special redirects for FOR CASE, etc. */ | |
136 | union { | |
137 | struct for_com *For; | |
138 | struct case_com *Case; | |
139 | struct while_com *While; | |
140 | struct if_com *If; | |
141 | struct connection *Connection; | |
142 | struct simple_com *Simple; | |
143 | struct function_def *Function_def; | |
144 | struct group_com *Group; | |
145 | #if defined (SELECT_COMMAND) | |
146 | struct select_com *Select; | |
cce855bc JA |
147 | #endif |
148 | #if defined (DPAREN_ARITHMETIC) | |
149 | struct arith_com *Arith; | |
150 | #endif | |
151 | #if defined (COND_COMMAND) | |
152 | struct cond_com *Cond; | |
726f6388 JA |
153 | #endif |
154 | } value; | |
155 | } COMMAND; | |
156 | ||
157 | /* Structure used to represent the CONNECTION type. */ | |
158 | typedef struct connection { | |
159 | int ignore; /* Unused; simplifies make_command (). */ | |
160 | COMMAND *first; /* Pointer to the first command. */ | |
161 | COMMAND *second; /* Pointer to the second command. */ | |
162 | int connector; /* What separates this command from others. */ | |
163 | } CONNECTION; | |
164 | ||
165 | /* Structures used to represent the CASE command. */ | |
166 | ||
167 | /* Pattern/action structure for CASE_COM. */ | |
168 | typedef struct pattern_list { | |
169 | struct pattern_list *next; /* Clause to try in case this one failed. */ | |
170 | WORD_LIST *patterns; /* Linked list of patterns to test. */ | |
171 | COMMAND *action; /* Thing to execute if a pattern matches. */ | |
172 | } PATTERN_LIST; | |
173 | ||
174 | /* The CASE command. */ | |
175 | typedef struct case_com { | |
176 | int flags; /* See description of CMD flags. */ | |
177 | WORD_DESC *word; /* The thing to test. */ | |
178 | PATTERN_LIST *clauses; /* The clauses to test against, or NULL. */ | |
179 | } CASE_COM; | |
180 | ||
181 | /* FOR command. */ | |
182 | typedef struct for_com { | |
183 | int flags; /* See description of CMD flags. */ | |
184 | WORD_DESC *name; /* The variable name to get mapped over. */ | |
185 | WORD_LIST *map_list; /* The things to map over. This is never NULL. */ | |
186 | COMMAND *action; /* The action to execute. | |
187 | During execution, NAME is bound to successive | |
188 | members of MAP_LIST. */ | |
189 | } FOR_COM; | |
190 | ||
191 | #if defined (SELECT_COMMAND) | |
192 | /* KSH SELECT command. */ | |
193 | typedef struct select_com { | |
194 | int flags; /* See description of CMD flags. */ | |
195 | WORD_DESC *name; /* The variable name to get mapped over. */ | |
196 | WORD_LIST *map_list; /* The things to map over. This is never NULL. */ | |
197 | COMMAND *action; /* The action to execute. | |
198 | During execution, NAME is bound to the member of | |
199 | MAP_LIST chosen by the user. */ | |
200 | } SELECT_COM; | |
201 | #endif /* SELECT_COMMAND */ | |
202 | ||
203 | /* IF command. */ | |
204 | typedef struct if_com { | |
205 | int flags; /* See description of CMD flags. */ | |
206 | COMMAND *test; /* Thing to test. */ | |
207 | COMMAND *true_case; /* What to do if the test returned non-zero. */ | |
208 | COMMAND *false_case; /* What to do if the test returned zero. */ | |
209 | } IF_COM; | |
210 | ||
211 | /* WHILE command. */ | |
212 | typedef struct while_com { | |
213 | int flags; /* See description of CMD flags. */ | |
214 | COMMAND *test; /* Thing to test. */ | |
215 | COMMAND *action; /* Thing to do while test is non-zero. */ | |
216 | } WHILE_COM; | |
217 | ||
cce855bc JA |
218 | #if defined (DPAREN_ARITHMETIC) |
219 | /* The arithmetic evaluation command, ((...)). Just a set of flags and | |
220 | a WORD_LIST, of which the first element is the only one used, for the | |
221 | time being. */ | |
222 | typedef struct arith_com { | |
223 | int flags; | |
224 | WORD_LIST *exp; | |
225 | int line; | |
226 | } ARITH_COM; | |
227 | #endif /* DPAREN_ARITHMETIC */ | |
228 | ||
229 | /* The conditional command, [[...]]. This is a binary tree -- we slippped | |
230 | a recursive-descent parser into the YACC grammar to parse it. */ | |
231 | #define COND_AND 1 | |
232 | #define COND_OR 2 | |
233 | #define COND_UNARY 3 | |
234 | #define COND_BINARY 4 | |
235 | #define COND_TERM 5 | |
236 | #define COND_EXPR 6 | |
237 | ||
238 | typedef struct cond_com { | |
239 | int flags; | |
240 | int line; | |
241 | int type; | |
242 | WORD_DESC *op; | |
243 | struct cond_com *left, *right; | |
244 | } COND_COM; | |
245 | ||
726f6388 JA |
246 | /* The "simple" command. Just a collection of words and redirects. */ |
247 | typedef struct simple_com { | |
248 | int flags; /* See description of CMD flags. */ | |
249 | WORD_LIST *words; /* The program name, the arguments, | |
250 | variable assignments, etc. */ | |
251 | REDIRECT *redirects; /* Redirections to perform. */ | |
252 | int line; /* line number the command starts on */ | |
253 | } SIMPLE_COM; | |
254 | ||
ccc6cda3 | 255 | /* The "function definition" command. */ |
726f6388 | 256 | typedef struct function_def { |
cce855bc | 257 | int flags; /* See description of CMD flags. */ |
726f6388 JA |
258 | WORD_DESC *name; /* The name of the function. */ |
259 | COMMAND *command; /* The parsed execution tree. */ | |
ccc6cda3 | 260 | int line; /* Line number the function def starts on. */ |
726f6388 JA |
261 | } FUNCTION_DEF; |
262 | ||
ccc6cda3 JA |
263 | /* A command that is `grouped' allows pipes and redirections to affect all |
264 | commands in the group. */ | |
726f6388 JA |
265 | typedef struct group_com { |
266 | int ignore; /* See description of CMD flags. */ | |
267 | COMMAND *command; | |
268 | } GROUP_COM; | |
269 | ||
270 | extern COMMAND *global_command; | |
271 | ||
b72432fd JA |
272 | /* Possible command errors */ |
273 | #define CMDERR_DEFAULT 0 | |
274 | #define CMDERR_BADTYPE 1 | |
275 | #define CMDERR_BADCONN 2 | |
276 | #define CMDERR_BADJUMP 3 | |
277 | ||
278 | #define CMDERR_LAST 3 | |
279 | ||
726f6388 JA |
280 | /* Forward declarations of functions declared in copy_cmd.c. */ |
281 | ||
282 | extern WORD_DESC *copy_word __P((WORD_DESC *)); | |
283 | extern WORD_LIST *copy_word_list __P((WORD_LIST *)); | |
284 | extern REDIRECT *copy_redirect __P((REDIRECT *)); | |
285 | extern REDIRECT *copy_redirects __P((REDIRECT *)); | |
286 | extern COMMAND *copy_command __P((COMMAND *)); | |
287 | ||
ccc6cda3 | 288 | #endif /* _COMMAND_H_ */ |