| 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-2005 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     0x00001 /* Dollar sign present. */ | 
|---|
| 71 | #define W_QUOTED        0x00002 /* Some form of quote character is present. */ | 
|---|
| 72 | #define W_ASSIGNMENT    0x00004 /* This word is a variable assignment. */ | 
|---|
| 73 | #define W_GLOBEXP       0x00008 /* This word is the result of a glob expansion. */ | 
|---|
| 74 | #define W_NOSPLIT       0x00010 /* Do not perform word splitting on this word. */ | 
|---|
| 75 | #define W_NOGLOB        0x00020 /* Do not perform globbing on this word. */ | 
|---|
| 76 | #define W_NOSPLIT2      0x00040 /* Don't split word except for $@ expansion. */ | 
|---|
| 77 | #define W_TILDEEXP      0x00080 /* Tilde expand this assignment word */ | 
|---|
| 78 | #define W_DOLLARAT      0x00100 /* $@ and its special handling */ | 
|---|
| 79 | #define W_DOLLARSTAR    0x00200 /* $* and its special handling */ | 
|---|
| 80 | #define W_NOCOMSUB      0x00400 /* Don't perform command substitution on this word */ | 
|---|
| 81 | #define W_ASSIGNRHS     0x00800 /* Word is rhs of an assignment statement */ | 
|---|
| 82 | #define W_NOTILDE       0x01000 /* Don't perform tilde expansion on this word */ | 
|---|
| 83 | #define W_ITILDE        0x02000 /* Internal flag for word expansion */ | 
|---|
| 84 | #define W_NOEXPAND      0x04000 /* Don't expand at all -- do quote removal */ | 
|---|
| 85 | #define W_COMPASSIGN    0x08000 /* Compound assignment */ | 
|---|
| 86 | #define W_ASSNBLTIN     0x10000 /* word is a builtin command that takes assignments */ | 
|---|
| 87 | #define W_ASSIGNARG     0x20000 /* word is assignment argument to command */ | 
|---|
| 88 | #define W_HASQUOTEDNULL 0x40000 /* word contains a quoted null character */ | 
|---|
| 89 | #define W_DQUOTE        0x80000 /* word should be treated as if double-quoted */ | 
|---|
| 90 |  | 
|---|
| 91 | /* Possible values for subshell_environment */ | 
|---|
| 92 | #define SUBSHELL_ASYNC  0x01    /* subshell caused by `command &' */ | 
|---|
| 93 | #define SUBSHELL_PAREN  0x02    /* subshell caused by ( ... ) */ | 
|---|
| 94 | #define SUBSHELL_COMSUB 0x04    /* subshell caused by `command` or $(command) */ | 
|---|
| 95 | #define SUBSHELL_FORK   0x08    /* subshell caused by executing a disk command */ | 
|---|
| 96 | #define SUBSHELL_PIPE   0x10    /* subshell from a pipeline element */ | 
|---|
| 97 |  | 
|---|
| 98 | /* A structure which represents a word. */ | 
|---|
| 99 | typedef struct word_desc { | 
|---|
| 100 | char *word;           /* Zero terminated string. */ | 
|---|
| 101 | int flags;            /* Flags associated with this word. */ | 
|---|
| 102 | } WORD_DESC; | 
|---|
| 103 |  | 
|---|
| 104 | /* A linked list of words. */ | 
|---|
| 105 | typedef struct word_list { | 
|---|
| 106 | struct word_list *next; | 
|---|
| 107 | WORD_DESC *word; | 
|---|
| 108 | } WORD_LIST; | 
|---|
| 109 |  | 
|---|
| 110 |  | 
|---|
| 111 | /* **************************************************************** */ | 
|---|
| 112 | /*                                                                  */ | 
|---|
| 113 | /*                      Shell Command Structs                       */ | 
|---|
| 114 | /*                                                                  */ | 
|---|
| 115 | /* **************************************************************** */ | 
|---|
| 116 |  | 
|---|
| 117 | /* What a redirection descriptor looks like.  If the redirection instruction | 
|---|
| 118 | is ri_duplicating_input or ri_duplicating_output, use DEST, otherwise | 
|---|
| 119 | use the file in FILENAME.  Out-of-range descriptors are identified by a | 
|---|
| 120 | negative DEST. */ | 
|---|
| 121 |  | 
|---|
| 122 | typedef union { | 
|---|
| 123 | int dest;                     /* Place to redirect REDIRECTOR to, or ... */ | 
|---|
| 124 | WORD_DESC *filename;          /* filename to redirect to. */ | 
|---|
| 125 | } REDIRECTEE; | 
|---|
| 126 |  | 
|---|
| 127 | /* Structure describing a redirection.  If REDIRECTOR is negative, the parser | 
|---|
| 128 | (or translator in redir.c) encountered an out-of-range file descriptor. */ | 
|---|
| 129 | typedef struct redirect { | 
|---|
| 130 | struct redirect *next;        /* Next element, or NULL. */ | 
|---|
| 131 | int redirector;               /* Descriptor to be redirected. */ | 
|---|
| 132 | int flags;                    /* Flag value for `open'. */ | 
|---|
| 133 | enum r_instruction  instruction; /* What to do with the information. */ | 
|---|
| 134 | REDIRECTEE redirectee;        /* File descriptor or filename */ | 
|---|
| 135 | char *here_doc_eof;           /* The word that appeared in <<foo. */ | 
|---|
| 136 | } REDIRECT; | 
|---|
| 137 |  | 
|---|
| 138 | /* An element used in parsing.  A single word or a single redirection. | 
|---|
| 139 | This is an ephemeral construct. */ | 
|---|
| 140 | typedef struct element { | 
|---|
| 141 | WORD_DESC *word; | 
|---|
| 142 | REDIRECT *redirect; | 
|---|
| 143 | } ELEMENT; | 
|---|
| 144 |  | 
|---|
| 145 | /* Possible values for command->flags. */ | 
|---|
| 146 | #define CMD_WANT_SUBSHELL  0x01 /* User wants a subshell: ( command ) */ | 
|---|
| 147 | #define CMD_FORCE_SUBSHELL 0x02 /* Shell needs to force a subshell. */ | 
|---|
| 148 | #define CMD_INVERT_RETURN  0x04 /* Invert the exit value. */ | 
|---|
| 149 | #define CMD_IGNORE_RETURN  0x08 /* Ignore the exit value.  For set -e. */ | 
|---|
| 150 | #define CMD_NO_FUNCTIONS   0x10 /* Ignore functions during command lookup. */ | 
|---|
| 151 | #define CMD_INHIBIT_EXPANSION 0x20 /* Do not expand the command words. */ | 
|---|
| 152 | #define CMD_NO_FORK        0x40 /* Don't fork; just call execve */ | 
|---|
| 153 | #define CMD_TIME_PIPELINE  0x80 /* Time a pipeline */ | 
|---|
| 154 | #define CMD_TIME_POSIX     0x100 /* time -p; use POSIX.2 time output spec. */ | 
|---|
| 155 | #define CMD_AMPERSAND      0x200 /* command & */ | 
|---|
| 156 | #define CMD_STDIN_REDIR    0x400 /* async command needs implicit </dev/null */ | 
|---|
| 157 | #define CMD_COMMAND_BUILTIN 0x0800 /* command executed by `command' builtin */ | 
|---|
| 158 |  | 
|---|
| 159 | /* What a command looks like. */ | 
|---|
| 160 | typedef struct command { | 
|---|
| 161 | enum command_type type;       /* FOR CASE WHILE IF CONNECTION or SIMPLE. */ | 
|---|
| 162 | int flags;                    /* Flags controlling execution environment. */ | 
|---|
| 163 | int line;                     /* line number the command starts on */ | 
|---|
| 164 | REDIRECT *redirects;          /* Special redirects for FOR CASE, etc. */ | 
|---|
| 165 | union { | 
|---|
| 166 | struct for_com *For; | 
|---|
| 167 | struct case_com *Case; | 
|---|
| 168 | struct while_com *While; | 
|---|
| 169 | struct if_com *If; | 
|---|
| 170 | struct connection *Connection; | 
|---|
| 171 | struct simple_com *Simple; | 
|---|
| 172 | struct function_def *Function_def; | 
|---|
| 173 | struct group_com *Group; | 
|---|
| 174 | #if defined (SELECT_COMMAND) | 
|---|
| 175 | struct select_com *Select; | 
|---|
| 176 | #endif | 
|---|
| 177 | #if defined (DPAREN_ARITHMETIC) | 
|---|
| 178 | struct arith_com *Arith; | 
|---|
| 179 | #endif | 
|---|
| 180 | #if defined (COND_COMMAND) | 
|---|
| 181 | struct cond_com *Cond; | 
|---|
| 182 | #endif | 
|---|
| 183 | #if defined (ARITH_FOR_COMMAND) | 
|---|
| 184 | struct arith_for_com *ArithFor; | 
|---|
| 185 | #endif | 
|---|
| 186 | struct subshell_com *Subshell; | 
|---|
| 187 | } value; | 
|---|
| 188 | } COMMAND; | 
|---|
| 189 |  | 
|---|
| 190 | /* Structure used to represent the CONNECTION type. */ | 
|---|
| 191 | typedef struct connection { | 
|---|
| 192 | int ignore;                   /* Unused; simplifies make_command (). */ | 
|---|
| 193 | COMMAND *first;               /* Pointer to the first command. */ | 
|---|
| 194 | COMMAND *second;              /* Pointer to the second command. */ | 
|---|
| 195 | int connector;                /* What separates this command from others. */ | 
|---|
| 196 | } CONNECTION; | 
|---|
| 197 |  | 
|---|
| 198 | /* Structures used to represent the CASE command. */ | 
|---|
| 199 |  | 
|---|
| 200 | /* Pattern/action structure for CASE_COM. */ | 
|---|
| 201 | typedef struct pattern_list { | 
|---|
| 202 | struct pattern_list *next;    /* Clause to try in case this one failed. */ | 
|---|
| 203 | WORD_LIST *patterns;          /* Linked list of patterns to test. */ | 
|---|
| 204 | COMMAND *action;              /* Thing to execute if a pattern matches. */ | 
|---|
| 205 | int flags; | 
|---|
| 206 | } PATTERN_LIST; | 
|---|
| 207 |  | 
|---|
| 208 | /* The CASE command. */ | 
|---|
| 209 | typedef struct case_com { | 
|---|
| 210 | int flags;                    /* See description of CMD flags. */ | 
|---|
| 211 | int line;                     /* line number the `case' keyword appears on */ | 
|---|
| 212 | WORD_DESC *word;              /* The thing to test. */ | 
|---|
| 213 | PATTERN_LIST *clauses;        /* The clauses to test against, or NULL. */ | 
|---|
| 214 | } CASE_COM; | 
|---|
| 215 |  | 
|---|
| 216 | /* FOR command. */ | 
|---|
| 217 | typedef struct for_com { | 
|---|
| 218 | int flags;            /* See description of CMD flags. */ | 
|---|
| 219 | int line;             /* line number the `for' keyword appears on */ | 
|---|
| 220 | WORD_DESC *name;      /* The variable name to get mapped over. */ | 
|---|
| 221 | WORD_LIST *map_list;  /* The things to map over.  This is never NULL. */ | 
|---|
| 222 | COMMAND *action;      /* The action to execute. | 
|---|
| 223 | During execution, NAME is bound to successive | 
|---|
| 224 | members of MAP_LIST. */ | 
|---|
| 225 | } FOR_COM; | 
|---|
| 226 |  | 
|---|
| 227 | #if defined (ARITH_FOR_COMMAND) | 
|---|
| 228 | typedef struct arith_for_com { | 
|---|
| 229 | int flags; | 
|---|
| 230 | int line;     /* generally used for error messages */ | 
|---|
| 231 | WORD_LIST *init; | 
|---|
| 232 | WORD_LIST *test; | 
|---|
| 233 | WORD_LIST *step; | 
|---|
| 234 | COMMAND *action; | 
|---|
| 235 | } ARITH_FOR_COM; | 
|---|
| 236 | #endif | 
|---|
| 237 |  | 
|---|
| 238 | #if defined (SELECT_COMMAND) | 
|---|
| 239 | /* KSH SELECT command. */ | 
|---|
| 240 | typedef struct select_com { | 
|---|
| 241 | int flags;            /* See description of CMD flags. */ | 
|---|
| 242 | int line;             /* line number the `select' keyword appears on */ | 
|---|
| 243 | WORD_DESC *name;      /* The variable name to get mapped over. */ | 
|---|
| 244 | WORD_LIST *map_list;  /* The things to map over.  This is never NULL. */ | 
|---|
| 245 | COMMAND *action;      /* The action to execute. | 
|---|
| 246 | During execution, NAME is bound to the member of | 
|---|
| 247 | MAP_LIST chosen by the user. */ | 
|---|
| 248 | } SELECT_COM; | 
|---|
| 249 | #endif /* SELECT_COMMAND */ | 
|---|
| 250 |  | 
|---|
| 251 | /* IF command. */ | 
|---|
| 252 | typedef struct if_com { | 
|---|
| 253 | int flags;                    /* See description of CMD flags. */ | 
|---|
| 254 | COMMAND *test;                /* Thing to test. */ | 
|---|
| 255 | COMMAND *true_case;           /* What to do if the test returned non-zero. */ | 
|---|
| 256 | COMMAND *false_case;          /* What to do if the test returned zero. */ | 
|---|
| 257 | } IF_COM; | 
|---|
| 258 |  | 
|---|
| 259 | /* WHILE command. */ | 
|---|
| 260 | typedef struct while_com { | 
|---|
| 261 | int flags;                    /* See description of CMD flags. */ | 
|---|
| 262 | COMMAND *test;                /* Thing to test. */ | 
|---|
| 263 | COMMAND *action;              /* Thing to do while test is non-zero. */ | 
|---|
| 264 | } WHILE_COM; | 
|---|
| 265 |  | 
|---|
| 266 | #if defined (DPAREN_ARITHMETIC) | 
|---|
| 267 | /* The arithmetic evaluation command, ((...)).  Just a set of flags and | 
|---|
| 268 | a WORD_LIST, of which the first element is the only one used, for the | 
|---|
| 269 | time being. */ | 
|---|
| 270 | typedef struct arith_com { | 
|---|
| 271 | int flags; | 
|---|
| 272 | int line; | 
|---|
| 273 | WORD_LIST *exp; | 
|---|
| 274 | } ARITH_COM; | 
|---|
| 275 | #endif /* DPAREN_ARITHMETIC */ | 
|---|
| 276 |  | 
|---|
| 277 | /* The conditional command, [[...]].  This is a binary tree -- we slippped | 
|---|
| 278 | a recursive-descent parser into the YACC grammar to parse it. */ | 
|---|
| 279 | #define COND_AND        1 | 
|---|
| 280 | #define COND_OR         2 | 
|---|
| 281 | #define COND_UNARY      3 | 
|---|
| 282 | #define COND_BINARY     4 | 
|---|
| 283 | #define COND_TERM       5 | 
|---|
| 284 | #define COND_EXPR       6 | 
|---|
| 285 |  | 
|---|
| 286 | typedef struct cond_com { | 
|---|
| 287 | int flags; | 
|---|
| 288 | int line; | 
|---|
| 289 | int type; | 
|---|
| 290 | WORD_DESC *op; | 
|---|
| 291 | struct cond_com *left, *right; | 
|---|
| 292 | } COND_COM; | 
|---|
| 293 |  | 
|---|
| 294 | /* The "simple" command.  Just a collection of words and redirects. */ | 
|---|
| 295 | typedef struct simple_com { | 
|---|
| 296 | int flags;                    /* See description of CMD flags. */ | 
|---|
| 297 | int line;                     /* line number the command starts on */ | 
|---|
| 298 | WORD_LIST *words;             /* The program name, the arguments, | 
|---|
| 299 | variable assignments, etc. */ | 
|---|
| 300 | REDIRECT *redirects;          /* Redirections to perform. */ | 
|---|
| 301 | } SIMPLE_COM; | 
|---|
| 302 |  | 
|---|
| 303 | /* The "function definition" command. */ | 
|---|
| 304 | typedef struct function_def { | 
|---|
| 305 | int flags;                    /* See description of CMD flags. */ | 
|---|
| 306 | int line;                     /* Line number the function def starts on. */ | 
|---|
| 307 | WORD_DESC *name;              /* The name of the function. */ | 
|---|
| 308 | COMMAND *command;             /* The parsed execution tree. */ | 
|---|
| 309 | char *source_file;            /* file in which function was defined, if any */ | 
|---|
| 310 | } FUNCTION_DEF; | 
|---|
| 311 |  | 
|---|
| 312 | /* A command that is `grouped' allows pipes and redirections to affect all | 
|---|
| 313 | commands in the group. */ | 
|---|
| 314 | typedef struct group_com { | 
|---|
| 315 | int ignore;                   /* See description of CMD flags. */ | 
|---|
| 316 | COMMAND *command; | 
|---|
| 317 | } GROUP_COM; | 
|---|
| 318 |  | 
|---|
| 319 | typedef struct subshell_com { | 
|---|
| 320 | int flags; | 
|---|
| 321 | COMMAND *command; | 
|---|
| 322 | } SUBSHELL_COM; | 
|---|
| 323 |  | 
|---|
| 324 | extern COMMAND *global_command; | 
|---|
| 325 |  | 
|---|
| 326 | /* Possible command errors */ | 
|---|
| 327 | #define CMDERR_DEFAULT  0 | 
|---|
| 328 | #define CMDERR_BADTYPE  1 | 
|---|
| 329 | #define CMDERR_BADCONN  2 | 
|---|
| 330 | #define CMDERR_BADJUMP  3 | 
|---|
| 331 |  | 
|---|
| 332 | #define CMDERR_LAST     3 | 
|---|
| 333 |  | 
|---|
| 334 | /* Forward declarations of functions declared in copy_cmd.c. */ | 
|---|
| 335 |  | 
|---|
| 336 | extern FUNCTION_DEF *copy_function_def_contents __P((FUNCTION_DEF *, FUNCTION_DEF *)); | 
|---|
| 337 | extern FUNCTION_DEF *copy_function_def __P((FUNCTION_DEF *)); | 
|---|
| 338 |  | 
|---|
| 339 | extern WORD_DESC *copy_word __P((WORD_DESC *)); | 
|---|
| 340 | extern WORD_LIST *copy_word_list __P((WORD_LIST *)); | 
|---|
| 341 | extern REDIRECT *copy_redirect __P((REDIRECT *)); | 
|---|
| 342 | extern REDIRECT *copy_redirects __P((REDIRECT *)); | 
|---|
| 343 | extern COMMAND *copy_command __P((COMMAND *)); | 
|---|
| 344 |  | 
|---|
| 345 | #endif /* _COMMAND_H_ */ | 
|---|