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)
28 #include "../bashansi.h"
32 #include "../builtins.h"
35 #include "../execute_cmd.h"
38 # include "../bashhist.h"
43 extern void run_trap_cleanup ();
45 extern int interactive
, interactive_shell
;
46 extern int indirection_level
, startup_state
, subshell_environment
;
47 extern int line_number
;
48 extern int last_command_exit_value
;
49 extern int running_trap
;
50 extern COMMAND
*global_command
;
52 int parse_and_execute_level
= 0;
54 /* How to force parse_and_execute () to clean up after itself. */
56 parse_and_execute_cleanup ()
60 run_trap_cleanup (running_trap
- 1);
61 unfreeze_jobs_list ();
63 run_unwind_frame ("parse_and_execute_top");
66 /* Parse and execute the commands in STRING. Returns whatever
67 execute_command () returns. This frees STRING. FLAGS is a
68 flags word; look in common.h for the possible values. Actions
70 (flags & SEVAL_NONINT) -> interactive = 0;
71 (flags & SEVAL_INTERACT) -> interactive = 1;
72 (flags & SEVAL_NOHIST) -> call bash_history_disable ()
76 parse_and_execute (string
, from_file
, flags
)
82 volatile int should_jump_to_top_level
, last_result
;
84 COMMAND
*volatile command
;
87 /* Unwind protect this invocation of parse_and_execute (). */
88 begin_unwind_frame ("parse_and_execute_top");
89 unwind_protect_int (parse_and_execute_level
);
90 unwind_protect_jmp_buf (top_level
);
91 unwind_protect_int (indirection_level
);
92 unwind_protect_int (line_number
);
93 if (flags
& (SEVAL_NONINT
|SEVAL_INTERACT
))
94 unwind_protect_int (interactive
);
97 unwind_protect_int (remember_on_history
); /* can be used in scripts */
98 # if defined (BANG_HISTORY)
99 if (interactive_shell
)
101 unwind_protect_int (history_expansion_inhibited
);
103 # endif /* BANG_HISTORY */
106 add_unwind_protect (pop_stream
, (char *)NULL
);
108 add_unwind_protect (xfree
, orig_string
);
111 parse_and_execute_level
++;
112 push_stream (1); /* reset the line number */
114 if (flags
& (SEVAL_NONINT
|SEVAL_INTERACT
))
115 interactive
= (flags
& SEVAL_NONINT
) ? 0 : 1;
117 #if defined (HISTORY)
118 if (flags
& SEVAL_NOHIST
)
119 bash_history_disable ();
122 code
= should_jump_to_top_level
= 0;
123 last_result
= EXECUTION_SUCCESS
;
124 command
= (COMMAND
*)NULL
;
126 with_input_from_string (string
, from_file
);
127 while (*(bash_input
.location
.string
))
131 last_result
= EXECUTION_FAILURE
;
135 /* Provide a location for functions which `longjmp (top_level)' to
136 jump to. This prevents errors in substitution from restarting
137 the reader loop directly, for example. */
138 code
= setjmp (top_level
);
142 should_jump_to_top_level
= 0;
147 run_unwind_frame ("pe_dispose");
148 /* Remember to call longjmp (top_level) after the old
149 value for it is restored. */
150 should_jump_to_top_level
= 1;
154 run_unwind_frame ("pe_dispose");
155 last_command_exit_value
= 1; /* XXX */
156 if (subshell_environment
)
158 should_jump_to_top_level
= 1;
164 dispose_command (command
); /* pe_dispose does this */
170 programming_error ("parse_and_execute: bad jump: code %d", code
);
175 if (parse_command () == 0)
177 if (interactive_shell
== 0 && read_but_dont_execute
)
179 last_result
= EXECUTION_SUCCESS
;
180 dispose_command (global_command
);
181 global_command
= (COMMAND
*)NULL
;
183 else if (command
= global_command
)
185 struct fd_bitmap
*bitmap
;
187 bitmap
= new_fd_bitmap (FD_BITMAP_SIZE
);
188 begin_unwind_frame ("pe_dispose");
189 add_unwind_protect (dispose_fd_bitmap
, bitmap
);
190 add_unwind_protect (dispose_command
, command
); /* XXX */
192 global_command
= (COMMAND
*)NULL
;
194 #if defined (ONESHOT)
195 if (startup_state
== 2 && *bash_input
.location
.string
== '\0' &&
196 command
->type
== cm_simple
&& !command
->redirects
&&
197 !command
->value
.Simple
->redirects
&&
198 ((command
->flags
& CMD_TIME_PIPELINE
) == 0))
200 command
->flags
|= CMD_NO_FORK
;
201 command
->value
.Simple
->flags
|= CMD_NO_FORK
;
205 last_result
= execute_command_internal
206 (command
, 0, NO_PIPE
, NO_PIPE
, bitmap
);
208 dispose_command (command
);
209 dispose_fd_bitmap (bitmap
);
210 discard_unwind_frame ("pe_dispose");
215 last_result
= EXECUTION_FAILURE
;
217 /* Since we are shell compatible, syntax errors in a script
218 abort the execution of the script. Right? */
225 run_unwind_frame ("parse_and_execute_top");
227 if (interrupt_state
&& parse_and_execute_level
== 0)
229 /* An interrupt during non-interactive execution in an
230 interactive shell (e.g. via $PROMPT_COMMAND) should
231 not cause the shell to exit. */
232 interactive
= interactive_shell
;
233 throw_to_top_level ();
236 if (should_jump_to_top_level
)
237 jump_to_top_level (code
);
239 return (last_result
);