1 /* Evaluate a string as one or more shell commands.
3 Copyright (C) 1996-2005 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #if defined (HAVE_UNISTD_H)
25 # include <sys/types.h>
36 #include "../bashansi.h"
40 #include "../builtins.h"
43 #include "../execute_cmd.h"
48 # include "../bashhist.h"
57 #define IS_BUILTIN(s) (builtin_address_internal(s, 0) != (struct builtin *)NULL)
59 extern int indirection_level
, subshell_environment
;
60 extern int line_number
;
61 extern int last_command_exit_value
;
62 extern int running_trap
;
63 extern int loop_level
;
64 extern int posixly_correct
;
66 int parse_and_execute_level
= 0;
68 static int cat_file
__P((REDIRECT
*));
72 set_history_remembering ()
74 remember_on_history
= enable_history_list
;
78 /* How to force parse_and_execute () to clean up after itself. */
80 parse_and_execute_cleanup ()
84 run_trap_cleanup (running_trap
- 1);
85 unfreeze_jobs_list ();
87 run_unwind_frame ("parse_and_execute_top");
90 /* Parse and execute the commands in STRING. Returns whatever
91 execute_command () returns. This frees STRING. FLAGS is a
92 flags word; look in common.h for the possible values. Actions
94 (flags & SEVAL_NONINT) -> interactive = 0;
95 (flags & SEVAL_INTERACT) -> interactive = 1;
96 (flags & SEVAL_NOHIST) -> call bash_history_disable ()
97 (flags & SEVAL_NOFREE) -> don't free STRING when finished
98 (flags & SEVAL_RESETLINE) -> reset line_number to 1
102 parse_and_execute (string
, from_file
, flags
)
104 const char *from_file
;
108 volatile int should_jump_to_top_level
, last_result
;
110 COMMAND
*volatile command
;
112 orig_string
= string
;
113 /* Unwind protect this invocation of parse_and_execute (). */
114 begin_unwind_frame ("parse_and_execute_top");
115 unwind_protect_int (parse_and_execute_level
);
116 unwind_protect_jmp_buf (top_level
);
117 unwind_protect_int (indirection_level
);
118 unwind_protect_int (line_number
);
119 unwind_protect_int (loop_level
);
120 if (flags
& (SEVAL_NONINT
|SEVAL_INTERACT
))
121 unwind_protect_int (interactive
);
123 lreset
= flags
& SEVAL_RESETLINE
;
125 #if defined (HISTORY)
126 if (parse_and_execute_level
== 0)
127 add_unwind_protect (set_history_remembering
, (char *)NULL
);
129 unwind_protect_int (remember_on_history
); /* can be used in scripts */
130 # if defined (BANG_HISTORY)
131 if (interactive_shell
)
133 unwind_protect_int (history_expansion_inhibited
);
135 # endif /* BANG_HISTORY */
138 if (interactive_shell
)
140 x
= get_current_prompt_level ();
141 add_unwind_protect (set_current_prompt_level
, x
);
144 add_unwind_protect (pop_stream
, (char *)NULL
);
145 if (orig_string
&& ((flags
& SEVAL_NOFREE
) == 0))
146 add_unwind_protect (xfree
, orig_string
);
149 parse_and_execute_level
++;
151 /* Reset the line number if the caller wants us to. If we don't reset the
152 line number, we have to subtract one, because we will add one just
153 before executing the next command (resetting the line number sets it to
154 0; the first line number is 1). */
155 push_stream (lreset
);
160 if (flags
& (SEVAL_NONINT
|SEVAL_INTERACT
))
161 interactive
= (flags
& SEVAL_NONINT
) ? 0 : 1;
163 #if defined (HISTORY)
164 if (flags
& SEVAL_NOHIST
)
165 bash_history_disable ();
168 code
= should_jump_to_top_level
= 0;
169 last_result
= EXECUTION_SUCCESS
;
171 with_input_from_string (string
, from_file
);
172 while (*(bash_input
.location
.string
))
174 command
= (COMMAND
*)NULL
;
178 last_result
= EXECUTION_FAILURE
;
182 /* Provide a location for functions which `longjmp (top_level)' to
183 jump to. This prevents errors in substitution from restarting
184 the reader loop directly, for example. */
185 code
= setjmp (top_level
);
189 should_jump_to_top_level
= 0;
196 run_unwind_frame ("pe_dispose");
197 /* Remember to call longjmp (top_level) after the old
198 value for it is restored. */
199 should_jump_to_top_level
= 1;
204 run_unwind_frame ("pe_dispose");
205 last_result
= last_command_exit_value
= EXECUTION_FAILURE
; /* XXX */
206 if (subshell_environment
)
208 should_jump_to_top_level
= 1;
214 dispose_command (command
); /* pe_dispose does this */
220 command_error ("parse_and_execute", CMDERR_BADJUMP
, code
, 0);
225 if (parse_command () == 0)
227 if (interactive_shell
== 0 && read_but_dont_execute
)
229 last_result
= EXECUTION_SUCCESS
;
230 dispose_command (global_command
);
231 global_command
= (COMMAND
*)NULL
;
233 else if (command
= global_command
)
235 struct fd_bitmap
*bitmap
;
237 bitmap
= new_fd_bitmap (FD_BITMAP_SIZE
);
238 begin_unwind_frame ("pe_dispose");
239 add_unwind_protect (dispose_fd_bitmap
, bitmap
);
240 add_unwind_protect (dispose_command
, command
); /* XXX */
242 global_command
= (COMMAND
*)NULL
;
244 #if defined (ONESHOT)
247 * we were invoked as `bash -c' (startup_state == 2) AND
248 * parse_and_execute has not been called recursively AND
249 * we're not running a trap AND
250 * we have parsed the full command (string == '\0') AND
251 * we're not going to run the exit trap AND
252 * we have a simple command without redirections AND
253 * the command is not being timed AND
254 * the command's return status is not being inverted
256 * tell the execution code that we don't need to fork
258 if (startup_state
== 2 && parse_and_execute_level
== 1 &&
260 *bash_input
.location
.string
== '\0' &&
261 command
->type
== cm_simple
&&
262 signal_is_trapped (EXIT_TRAP
) == 0 &&
263 command
->redirects
== 0 && command
->value
.Simple
->redirects
== 0 &&
264 ((command
->flags
& CMD_TIME_PIPELINE
) == 0) &&
265 ((command
->flags
& CMD_INVERT_RETURN
) == 0))
267 command
->flags
|= CMD_NO_FORK
;
268 command
->value
.Simple
->flags
|= CMD_NO_FORK
;
272 /* See if this is a candidate for $( <file ). */
273 if (startup_state
== 2 &&
274 (subshell_environment
& SUBSHELL_COMSUB
) &&
275 *bash_input
.location
.string
== '\0' &&
276 command
->type
== cm_simple
&& !command
->redirects
&&
277 (command
->flags
& CMD_TIME_PIPELINE
) == 0 &&
278 command
->value
.Simple
->words
== 0 &&
279 command
->value
.Simple
->redirects
&&
280 command
->value
.Simple
->redirects
->next
== 0 &&
281 command
->value
.Simple
->redirects
->instruction
== r_input_direction
)
284 r
= cat_file (command
->value
.Simple
->redirects
);
285 last_result
= (r
< 0) ? EXECUTION_FAILURE
: EXECUTION_SUCCESS
;
288 last_result
= execute_command_internal
289 (command
, 0, NO_PIPE
, NO_PIPE
, bitmap
);
291 dispose_command (command
);
292 dispose_fd_bitmap (bitmap
);
293 discard_unwind_frame ("pe_dispose");
298 last_result
= EXECUTION_FAILURE
;
300 /* Since we are shell compatible, syntax errors in a script
301 abort the execution of the script. Right? */
308 run_unwind_frame ("parse_and_execute_top");
310 if (interrupt_state
&& parse_and_execute_level
== 0)
312 /* An interrupt during non-interactive execution in an
313 interactive shell (e.g. via $PROMPT_COMMAND) should
314 not cause the shell to exit. */
315 interactive
= interactive_shell
;
316 throw_to_top_level ();
319 if (should_jump_to_top_level
)
320 jump_to_top_level (code
);
322 return (last_result
);
325 /* Handle a $( < file ) command substitution. This expands the filename,
326 returning errors as appropriate, then just cats the file to the standard
335 if (r
->instruction
!= r_input_direction
)
338 /* Get the filename. */
339 if (posixly_correct
&& !interactive_shell
)
340 disallow_filename_globbing
++;
341 fn
= redirection_expand (r
->redirectee
.filename
);
342 if (posixly_correct
&& !interactive_shell
)
343 disallow_filename_globbing
--;
347 redirection_error (r
, AMBIGUOUS_REDIRECT
);
351 fd
= open(fn
, O_RDONLY
);
359 rval
= zcatfd (fd
, 1, fn
);