1 /* Copyright (C) 1996 Free Software Foundation, Inc.
3 This file is part of GNU Bash, the Bourne Again SHell.
5 Bash is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2, or (at your option) any later
10 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 You should have received a copy of the GNU General Public License along
16 with Bash; see the file COPYING. If not, write to the Free Software
17 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
21 #if defined (HAVE_UNISTD_H)
23 # include <sys/types.h>
34 #include "../bashansi.h"
38 #include "../builtins.h"
41 #include "../execute_cmd.h"
46 # include "../bashhist.h"
55 #define IS_BUILTIN(s) (builtin_address_internal(s, 0) != (struct builtin *)NULL)
57 extern int indirection_level, startup_state, subshell_environment;
58 extern int line_number;
59 extern int last_command_exit_value;
60 extern int running_trap;
61 extern int posixly_correct;
63 int parse_and_execute_level = 0;
65 static int cat_file __P((REDIRECT *));
67 /* How to force parse_and_execute () to clean up after itself. */
69 parse_and_execute_cleanup ()
73 run_trap_cleanup (running_trap - 1);
74 unfreeze_jobs_list ();
76 run_unwind_frame ("parse_and_execute_top");
79 /* Parse and execute the commands in STRING. Returns whatever
80 execute_command () returns. This frees STRING. FLAGS is a
81 flags word; look in common.h for the possible values. Actions
83 (flags & SEVAL_NONINT) -> interactive = 0;
84 (flags & SEVAL_INTERACT) -> interactive = 1;
85 (flags & SEVAL_NOHIST) -> call bash_history_disable ()
86 (flags & SEVAL_NOFREE) -> don't free STRING when finished
87 (flags & SEVAL_RESETLINE) -> reset line_number to 1
91 parse_and_execute (string, from_file, flags)
93 const char *from_file;
97 volatile int should_jump_to_top_level, last_result;
99 COMMAND *volatile command;
101 orig_string = string;
102 /* Unwind protect this invocation of parse_and_execute (). */
103 begin_unwind_frame ("parse_and_execute_top");
104 unwind_protect_int (parse_and_execute_level);
105 unwind_protect_jmp_buf (top_level);
106 unwind_protect_int (indirection_level);
107 unwind_protect_int (line_number);
108 if (flags & (SEVAL_NONINT|SEVAL_INTERACT))
109 unwind_protect_int (interactive);
111 lreset = flags & SEVAL_RESETLINE;
113 #if defined (HISTORY)
114 unwind_protect_int (remember_on_history); /* can be used in scripts */
115 # if defined (BANG_HISTORY)
116 if (interactive_shell)
118 unwind_protect_int (history_expansion_inhibited);
120 # endif /* BANG_HISTORY */
123 if (interactive_shell)
125 x = get_current_prompt_level ();
126 add_unwind_protect (set_current_prompt_level, x);
129 add_unwind_protect (pop_stream, (char *)NULL);
130 if (orig_string && ((flags & SEVAL_NOFREE) == 0))
131 add_unwind_protect (xfree, orig_string);
134 parse_and_execute_level++;
136 /* Reset the line number if the caller wants us to. If we don't reset the
137 line number, we have to subtract one, because we will add one just
138 before executing the next command (resetting the line number sets it to
139 0; the first line number is 1). */
140 push_stream (lreset);
145 if (flags & (SEVAL_NONINT|SEVAL_INTERACT))
146 interactive = (flags & SEVAL_NONINT) ? 0 : 1;
148 #if defined (HISTORY)
149 if (flags & SEVAL_NOHIST)
150 bash_history_disable ();
153 code = should_jump_to_top_level = 0;
154 last_result = EXECUTION_SUCCESS;
156 with_input_from_string (string, from_file);
157 while (*(bash_input.location.string))
159 command = (COMMAND *)NULL;
163 last_result = EXECUTION_FAILURE;
167 /* Provide a location for functions which `longjmp (top_level)' to
168 jump to. This prevents errors in substitution from restarting
169 the reader loop directly, for example. */
170 code = setjmp (top_level);
174 should_jump_to_top_level = 0;
181 run_unwind_frame ("pe_dispose");
182 /* Remember to call longjmp (top_level) after the old
183 value for it is restored. */
184 should_jump_to_top_level = 1;
189 run_unwind_frame ("pe_dispose");
190 last_result = last_command_exit_value = EXECUTION_FAILURE; /* XXX */
191 if (subshell_environment)
193 should_jump_to_top_level = 1;
199 dispose_command (command); /* pe_dispose does this */
205 command_error ("parse_and_execute", CMDERR_BADJUMP, code, 0);
210 if (parse_command () == 0)
212 if (interactive_shell == 0 && read_but_dont_execute)
214 last_result = EXECUTION_SUCCESS;
215 dispose_command (global_command);
216 global_command = (COMMAND *)NULL;
218 else if (command = global_command)
220 struct fd_bitmap *bitmap;
222 bitmap = new_fd_bitmap (FD_BITMAP_SIZE);
223 begin_unwind_frame ("pe_dispose");
224 add_unwind_protect (dispose_fd_bitmap, bitmap);
225 add_unwind_protect (dispose_command, command); /* XXX */
227 global_command = (COMMAND *)NULL;
229 #if defined (ONESHOT)
232 * we were invoked as `bash -c' (startup_state == 2) AND
233 * parse_and_execute has not been called recursively AND
234 * we have parsed the full command (string == '\0') AND
235 * we have a simple command without redirections AND
236 * the command is not being timed
238 * tell the execution code that we don't need to fork
240 if (startup_state == 2 && parse_and_execute_level == 1 &&
241 *bash_input.location.string == '\0' &&
242 command->type == cm_simple &&
243 !command->redirects && !command->value.Simple->redirects &&
244 ((command->flags & CMD_TIME_PIPELINE) == 0))
246 command->flags |= CMD_NO_FORK;
247 command->value.Simple->flags |= CMD_NO_FORK;
251 /* See if this is a candidate for $( <file ). */
252 if (startup_state == 2 &&
253 (subshell_environment & SUBSHELL_COMSUB) &&
254 *bash_input.location.string == '\0' &&
255 command->type == cm_simple && !command->redirects &&
256 (command->flags & CMD_TIME_PIPELINE) == 0 &&
257 command->value.Simple->words == 0 &&
258 command->value.Simple->redirects &&
259 command->value.Simple->redirects->next == 0 &&
260 command->value.Simple->redirects->instruction == r_input_direction)
263 r = cat_file (command->value.Simple->redirects);
264 last_result = (r < 0) ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
267 last_result = execute_command_internal
268 (command, 0, NO_PIPE, NO_PIPE, bitmap);
270 dispose_command (command);
271 dispose_fd_bitmap (bitmap);
272 discard_unwind_frame ("pe_dispose");
277 last_result = EXECUTION_FAILURE;
279 /* Since we are shell compatible, syntax errors in a script
280 abort the execution of the script. Right? */
287 run_unwind_frame ("parse_and_execute_top");
289 if (interrupt_state && parse_and_execute_level == 0)
291 /* An interrupt during non-interactive execution in an
292 interactive shell (e.g. via $PROMPT_COMMAND) should
293 not cause the shell to exit. */
294 interactive = interactive_shell;
295 throw_to_top_level ();
298 if (should_jump_to_top_level)
299 jump_to_top_level (code);
301 return (last_result);
304 /* Handle a $( < file ) command substitution. This expands the filename,
305 returning errors as appropriate, then just cats the file to the standard
315 if (r->instruction != r_input_direction)
318 /* Get the filename. */
319 if (posixly_correct && !interactive_shell)
320 disallow_filename_globbing++;
321 fn = redirection_expand (r->redirectee.filename);
322 if (posixly_correct && !interactive_shell)
323 disallow_filename_globbing--;
327 redirection_error (r, AMBIGUOUS_REDIRECT);
331 fd = open(fn, O_RDONLY);
339 rval = zcatfd (fd, 1, fn);