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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #if defined (HAVE_UNISTD_H)
23 # include <sys/types.h>
33 #include "../filecntl.h"
34 #include "../bashansi.h"
38 #include "../builtins.h"
41 #include "../execute_cmd.h"
45 # include "../bashhist.h"
54 extern void run_trap_cleanup ();
56 extern int interactive, interactive_shell;
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;
62 extern COMMAND *global_command;
64 int parse_and_execute_level = 0;
66 static int cat_file ();
68 /* How to force parse_and_execute () to clean up after itself. */
70 parse_and_execute_cleanup ()
74 run_trap_cleanup (running_trap - 1);
75 unfreeze_jobs_list ();
77 run_unwind_frame ("parse_and_execute_top");
80 /* Parse and execute the commands in STRING. Returns whatever
81 execute_command () returns. This frees STRING. FLAGS is a
82 flags word; look in common.h for the possible values. Actions
84 (flags & SEVAL_NONINT) -> interactive = 0;
85 (flags & SEVAL_INTERACT) -> interactive = 1;
86 (flags & SEVAL_NOHIST) -> call bash_history_disable ()
90 parse_and_execute (string, from_file, flags)
96 volatile int should_jump_to_top_level, last_result;
98 COMMAND *volatile command;
100 orig_string = string;
101 /* Unwind protect this invocation of parse_and_execute (). */
102 begin_unwind_frame ("parse_and_execute_top");
103 unwind_protect_int (parse_and_execute_level);
104 unwind_protect_jmp_buf (top_level);
105 unwind_protect_int (indirection_level);
106 unwind_protect_int (line_number);
107 if (flags & (SEVAL_NONINT|SEVAL_INTERACT))
108 unwind_protect_int (interactive);
110 #if defined (HISTORY)
111 unwind_protect_int (remember_on_history); /* can be used in scripts */
112 # if defined (BANG_HISTORY)
113 if (interactive_shell)
115 unwind_protect_int (history_expansion_inhibited);
117 # endif /* BANG_HISTORY */
120 add_unwind_protect (pop_stream, (char *)NULL);
122 add_unwind_protect (xfree, orig_string);
125 parse_and_execute_level++;
126 push_stream (1); /* reset the line number */
128 if (flags & (SEVAL_NONINT|SEVAL_INTERACT))
129 interactive = (flags & SEVAL_NONINT) ? 0 : 1;
131 #if defined (HISTORY)
132 if (flags & SEVAL_NOHIST)
133 bash_history_disable ();
136 code = should_jump_to_top_level = 0;
137 last_result = EXECUTION_SUCCESS;
138 command = (COMMAND *)NULL;
140 with_input_from_string (string, from_file);
141 while (*(bash_input.location.string))
145 last_result = EXECUTION_FAILURE;
149 /* Provide a location for functions which `longjmp (top_level)' to
150 jump to. This prevents errors in substitution from restarting
151 the reader loop directly, for example. */
152 code = setjmp (top_level);
156 should_jump_to_top_level = 0;
161 run_unwind_frame ("pe_dispose");
162 /* Remember to call longjmp (top_level) after the old
163 value for it is restored. */
164 should_jump_to_top_level = 1;
168 run_unwind_frame ("pe_dispose");
169 last_command_exit_value = 1; /* XXX */
170 if (subshell_environment)
172 should_jump_to_top_level = 1;
178 dispose_command (command); /* pe_dispose does this */
184 programming_error ("parse_and_execute: bad jump: code %d", code);
189 if (parse_command () == 0)
191 if (interactive_shell == 0 && read_but_dont_execute)
193 last_result = EXECUTION_SUCCESS;
194 dispose_command (global_command);
195 global_command = (COMMAND *)NULL;
197 else if (command = global_command)
199 struct fd_bitmap *bitmap;
201 bitmap = new_fd_bitmap (FD_BITMAP_SIZE);
202 begin_unwind_frame ("pe_dispose");
203 add_unwind_protect (dispose_fd_bitmap, bitmap);
204 add_unwind_protect (dispose_command, command); /* XXX */
206 global_command = (COMMAND *)NULL;
208 #if defined (ONESHOT)
209 if (startup_state == 2 && *bash_input.location.string == '\0' &&
210 command->type == cm_simple && !command->redirects &&
211 !command->value.Simple->redirects &&
212 ((command->flags & CMD_TIME_PIPELINE) == 0))
214 command->flags |= CMD_NO_FORK;
215 command->value.Simple->flags |= CMD_NO_FORK;
219 /* See if this is a candidate for $( <file ). */
220 if (startup_state == 2 &&
221 subshell_environment == SUBSHELL_COMSUB &&
222 *bash_input.location.string == '\0' &&
223 command->type == cm_simple && !command->redirects &&
224 (command->flags & CMD_TIME_PIPELINE) == 0 &&
225 command->value.Simple->words == 0 &&
226 command->value.Simple->redirects &&
227 command->value.Simple->redirects->next == 0 &&
228 command->value.Simple->redirects->instruction == r_input_direction)
231 r = cat_file (command->value.Simple->redirects);
232 last_result = (r < 0) ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
235 last_result = execute_command_internal
236 (command, 0, NO_PIPE, NO_PIPE, bitmap);
238 dispose_command (command);
239 dispose_fd_bitmap (bitmap);
240 discard_unwind_frame ("pe_dispose");
245 last_result = EXECUTION_FAILURE;
247 /* Since we are shell compatible, syntax errors in a script
248 abort the execution of the script. Right? */
255 run_unwind_frame ("parse_and_execute_top");
257 if (interrupt_state && parse_and_execute_level == 0)
259 /* An interrupt during non-interactive execution in an
260 interactive shell (e.g. via $PROMPT_COMMAND) should
261 not cause the shell to exit. */
262 interactive = interactive_shell;
263 throw_to_top_level ();
266 if (should_jump_to_top_level)
267 jump_to_top_level (code);
269 return (last_result);
272 /* Write NB bytes from BUF to file descriptor FD, retrying the write if
273 it is interrupted. We retry three times if we get a zero-length
274 write. Any other signal causes this function to return prematurely. */
283 for (n = nb, nt = 0;;)
285 i = write (fd, buf, n);
297 else if (errno != EINTR)
302 /* Handle a $( < file ) command substitution. This expands the filename,
303 returning errors as appropriate, then just cats the file to the standard
312 if (r->instruction != r_input_direction)
315 /* Get the filename. */
316 if (posixly_correct && !interactive_shell)
317 disallow_filename_globbing++;
318 fn = redirection_expand (r->redirectee.filename);
319 if (posixly_correct && !interactive_shell)
320 disallow_filename_globbing--;
324 redirection_error (r, AMBIGUOUS_REDIRECT);
328 fd = open(fn, O_RDONLY);
339 /* Retry the reads on EINTR. Any other error causes a break from the
341 while ((nr = read (fd, lbuf, sizeof(lbuf))) < 0 && errno == EINTR)
350 if (zwrite (1, lbuf, nr) < 0)