Imported from ../bash-2.01.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, 675 Mass Ave, Cambridge, MA 02139, 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 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
55 /* Command Types: */
56 enum command_type { cm_for, cm_case, cm_while, cm_if, cm_simple, cm_select,
57                     cm_connection, cm_function_def, cm_until, cm_group };
58
59 /* Possible values for the `flags' field of a WORD_DESC. */
60 #define W_HASDOLLAR     0x01    /* Dollar sign present. */
61 #define W_QUOTED        0x02    /* Some form of quote character is present. */
62 #define W_ASSIGNMENT    0x04    /* This word is a variable assignment. */
63 #define W_GLOBEXP       0x08    /* This word is the result of a glob expansion. */
64 #define W_NOSPLIT       0x10    /* Do not perform word splitting on this word. */
65
66 /* Possible values for subshell_environment */
67 #define SUBSHELL_ASYNC  0x01    /* subshell caused by `command &' */
68 #define SUBSHELL_PAREN  0x02    /* subshell caused by ( ... ) */
69 #define SUBSHELL_COMSUB 0x04    /* subshell caused by `command` or $(command) */
70 #define SUBSHELL_FORK   0x08    /* subshell caused by executing a disk command */
71
72 /* A structure which represents a word. */
73 typedef struct word_desc {
74   char *word;           /* Zero terminated string. */
75   int flags;            /* Flags associated with this word. */
76 } WORD_DESC;
77
78 /* A linked list of words. */
79 typedef struct word_list {
80   struct word_list *next;
81   WORD_DESC *word;
82 } WORD_LIST;
83
84
85 /* **************************************************************** */
86 /*                                                                  */
87 /*                      Shell Command Structs                       */
88 /*                                                                  */
89 /* **************************************************************** */
90
91 /* What a redirection descriptor looks like.  If FLAGS is IS_DESCRIPTOR,
92    then we use REDIRECTEE.DEST, else we use the file specified. */
93
94 typedef union {
95   long dest;                    /* Place to redirect REDIRECTOR to, or ... */
96   WORD_DESC *filename;          /* filename to redirect to. */
97 } REDIRECTEE;
98
99 typedef struct redirect {
100   struct redirect *next;        /* Next element, or NULL. */
101   int redirector;               /* Descriptor to be redirected. */
102   int flags;                    /* Flag value for `open'. */
103   enum r_instruction  instruction; /* What to do with the information. */
104   REDIRECTEE redirectee;        /* File descriptor or filename */
105   char *here_doc_eof;           /* The word that appeared in <<foo. */
106 } REDIRECT;
107
108 /* An element used in parsing.  A single word or a single redirection.
109    This is an ephemeral construct. */
110 typedef struct element {
111   WORD_DESC *word;
112   REDIRECT *redirect;
113 } ELEMENT;
114
115 /* Possible values for command->flags. */
116 #define CMD_WANT_SUBSHELL  0x01 /* User wants a subshell: ( command ) */
117 #define CMD_FORCE_SUBSHELL 0x02 /* Shell needs to force a subshell. */
118 #define CMD_INVERT_RETURN  0x04 /* Invert the exit value. */
119 #define CMD_IGNORE_RETURN  0x08 /* Ignore the exit value.  For set -e. */
120 #define CMD_NO_FUNCTIONS   0x10 /* Ignore functions during command lookup. */
121 #define CMD_INHIBIT_EXPANSION 0x20 /* Do not expand the command words. */
122 #define CMD_NO_FORK        0x40 /* Don't fork; just call execve */
123 #define CMD_TIME_PIPELINE  0x80 /* Time a pipeline */
124 #define CMD_TIME_POSIX     0x100 /* time -p; use POSIX.2 time output spec. */
125 #define CMD_AMPERSAND      0x200 /* command & */
126 #define CMD_STDIN_REDIR    0x400 /* async command needs implicit </dev/null */
127
128 /* What a command looks like. */
129 typedef struct command {
130   enum command_type type;       /* FOR CASE WHILE IF CONNECTION or SIMPLE. */
131   int flags;                    /* Flags controlling execution environment. */
132   int line;                     /* line number the command starts on */
133   REDIRECT *redirects;          /* Special redirects for FOR CASE, etc. */
134   union {
135     struct for_com *For;
136     struct case_com *Case;
137     struct while_com *While;
138     struct if_com *If;
139     struct connection *Connection;
140     struct simple_com *Simple;
141     struct function_def *Function_def;
142     struct group_com *Group;
143 #if defined (SELECT_COMMAND)
144     struct select_com *Select;
145 #endif
146   } value;
147 } COMMAND;
148
149 /* Structure used to represent the CONNECTION type. */
150 typedef struct connection {
151   int ignore;                   /* Unused; simplifies make_command (). */
152   COMMAND *first;               /* Pointer to the first command. */
153   COMMAND *second;              /* Pointer to the second command. */
154   int connector;                /* What separates this command from others. */
155 } CONNECTION;
156
157 /* Structures used to represent the CASE command. */
158
159 /* Pattern/action structure for CASE_COM. */
160 typedef struct pattern_list {
161   struct pattern_list *next;    /* Clause to try in case this one failed. */
162   WORD_LIST *patterns;          /* Linked list of patterns to test. */
163   COMMAND *action;              /* Thing to execute if a pattern matches. */
164 } PATTERN_LIST;
165
166 /* The CASE command. */
167 typedef struct case_com {
168   int flags;                    /* See description of CMD flags. */
169   WORD_DESC *word;              /* The thing to test. */
170   PATTERN_LIST *clauses;        /* The clauses to test against, or NULL. */
171 } CASE_COM;
172
173 /* FOR command. */
174 typedef struct for_com {
175   int flags;            /* See description of CMD flags. */
176   WORD_DESC *name;      /* The variable name to get mapped over. */
177   WORD_LIST *map_list;  /* The things to map over.  This is never NULL. */
178   COMMAND *action;      /* The action to execute.
179                            During execution, NAME is bound to successive
180                            members of MAP_LIST. */
181 } FOR_COM;
182
183 #if defined (SELECT_COMMAND)
184 /* KSH SELECT command. */
185 typedef struct select_com {
186   int flags;            /* See description of CMD flags. */
187   WORD_DESC *name;      /* The variable name to get mapped over. */
188   WORD_LIST *map_list;  /* The things to map over.  This is never NULL. */
189   COMMAND *action;      /* The action to execute.
190                            During execution, NAME is bound to the member of
191                            MAP_LIST chosen by the user. */
192 } SELECT_COM;
193 #endif /* SELECT_COMMAND */
194
195 /* IF command. */
196 typedef struct if_com {
197   int flags;                    /* See description of CMD flags. */
198   COMMAND *test;                /* Thing to test. */
199   COMMAND *true_case;           /* What to do if the test returned non-zero. */
200   COMMAND *false_case;          /* What to do if the test returned zero. */
201 } IF_COM;
202
203 /* WHILE command. */
204 typedef struct while_com {
205   int flags;                    /* See description of CMD flags. */
206   COMMAND *test;                /* Thing to test. */
207   COMMAND *action;              /* Thing to do while test is non-zero. */
208 } WHILE_COM;
209
210 /* The "simple" command.  Just a collection of words and redirects. */
211 typedef struct simple_com {
212   int flags;                    /* See description of CMD flags. */
213   WORD_LIST *words;             /* The program name, the arguments,
214                                    variable assignments, etc. */
215   REDIRECT *redirects;          /* Redirections to perform. */
216   int line;                     /* line number the command starts on */
217 } SIMPLE_COM;
218
219 /* The "function definition" command. */
220 typedef struct function_def {
221   int ignore;                   /* See description of CMD flags. */
222   WORD_DESC *name;              /* The name of the function. */
223   COMMAND *command;             /* The parsed execution tree. */
224   int line;                     /* Line number the function def starts on. */
225 } FUNCTION_DEF;
226
227 /* A command that is `grouped' allows pipes and redirections to affect all
228    commands in the group. */
229 typedef struct group_com {
230   int ignore;                   /* See description of CMD flags. */
231   COMMAND *command;
232 } GROUP_COM;
233
234 extern COMMAND *global_command;
235
236 /* Forward declarations of functions declared in copy_cmd.c. */
237
238 extern WORD_DESC *copy_word __P((WORD_DESC *));
239 extern WORD_LIST *copy_word_list __P((WORD_LIST *));
240 extern REDIRECT *copy_redirect __P((REDIRECT *));
241 extern REDIRECT *copy_redirects __P((REDIRECT *));
242 extern COMMAND *copy_command __P((COMMAND *));
243
244 #endif /* _COMMAND_H_ */