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