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