1 /* eval.c -- reading and evaluating commands. */
3 /* Copyright (C) 1996-2011 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
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 #if defined (HAVE_UNISTD_H)
25 # include <sys/types.h>
42 #include "builtins/common.h"
45 #include "execute_cmd.h"
48 # include "bashhist.h"
51 #if defined (HAVE_POSIX_SIGNALS)
52 extern sigset_t top_level_mask
;
55 static void send_pwd_to_eterm
__P((void));
56 static sighandler alrm_catcher
__P((int));
58 /* Read and execute commands until EOF is reached. This assumes that
59 the input source has already been initialized. */
63 int our_indirection_level
;
64 COMMAND
* volatile current_command
;
66 USE_VAR(current_command
);
68 current_command
= (COMMAND
*)NULL
;
70 our_indirection_level
= ++indirection_level
;
73 reset_readahead_token ();
75 while (EOF_Reached
== 0)
79 code
= setjmp_nosigs (top_level
);
81 #if defined (PROCESS_SUBSTITUTION)
83 #endif /* PROCESS_SUBSTITUTION */
85 /* XXX - why do we set this every time through the loop? And why do
86 it if SIGINT is trapped in an interactive shell? */
87 if (interactive_shell
&& signal_is_ignored (SIGINT
) == 0 && signal_is_trapped (SIGINT
) == 0)
88 set_signal_handler (SIGINT
, sigint_sighandler
);
90 if (code
!= NOT_JUMPED
)
92 indirection_level
= our_indirection_level
;
96 /* Some kind of throw to top_level has occurred. */
100 current_command
= (COMMAND
*)NULL
;
101 if (exit_immediately_on_error
)
102 variable_context
= 0; /* not in a function */
107 /* Make sure the exit status is reset to a non-zero value, but
108 leave existing non-zero values (e.g., > 128 on signal)
110 if (last_command_exit_value
== 0)
111 last_command_exit_value
= EXECUTION_FAILURE
;
112 if (subshell_environment
)
114 current_command
= (COMMAND
*)NULL
;
118 /* Obstack free command elements, etc. */
121 dispose_command (current_command
);
122 current_command
= (COMMAND
*)NULL
;
124 #if defined (HAVE_POSIX_SIGNALS)
125 sigprocmask (SIG_SETMASK
, &top_level_mask
, (sigset_t
*)NULL
);
130 command_error ("reader_loop", CMDERR_BADJUMP
, code
, 0);
136 dispose_used_env_vars ();
138 #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
139 /* Attempt to reclaim memory allocated with alloca (). */
143 if (read_command () == 0)
145 if (interactive_shell
== 0 && read_but_dont_execute
)
147 last_command_exit_value
= EXECUTION_SUCCESS
;
148 dispose_command (global_command
);
149 global_command
= (COMMAND
*)NULL
;
151 else if (current_command
= global_command
)
153 global_command
= (COMMAND
*)NULL
;
155 /* If the shell is interactive, expand and display $PS0 after reading a
156 command (possibly a list or pipeline) and before executing it. */
157 if (interactive
&& ps0_prompt
)
161 ps0_string
= decode_prompt_string (ps0_prompt
);
162 if (ps0_string
&& *ps0_string
)
164 fprintf (stderr
, "%s", ps0_string
);
170 current_command_number
++;
175 execute_command (current_command
);
182 dispose_command (current_command
);
183 current_command
= (COMMAND
*)NULL
;
189 /* Parse error, maybe discard rest of stream if not interactive. */
190 if (interactive
== 0)
193 if (just_one_command
)
197 return (last_command_exit_value
);
200 /* Pretty print shell scripts */
204 COMMAND
*current_command
;
205 char *command_to_print
;
207 int global_posix_mode
, last_was_newline
;
209 global_posix_mode
= posixly_correct
;
210 last_was_newline
= 0;
211 while (EOF_Reached
== 0)
213 code
= setjmp_nosigs (top_level
);
215 return (EXECUTION_FAILURE
);
216 if (read_command() == 0)
218 current_command
= global_command
;
220 posixly_correct
= 1; /* print posix-conformant */
221 if (current_command
&& (command_to_print
= make_command_string (current_command
)))
223 printf ("%s\n", command_to_print
); /* for now */
224 last_was_newline
= 0;
226 else if (last_was_newline
== 0)
229 last_was_newline
= 1;
231 posixly_correct
= global_posix_mode
;
232 dispose_command (current_command
);
235 return (EXECUTION_FAILURE
);
238 return (EXECUTION_SUCCESS
);
245 printf (_("\007timed out waiting for input: auto-logout\n"));
247 bash_logout (); /* run ~/.bash_logout if this is a login shell */
248 jump_to_top_level (EXITPROG
);
252 /* Send an escape sequence to emacs term mode to tell it the
253 current working directory. */
260 pwd
= get_string_value ("PWD");
262 f
= pwd
= get_working_directory ("eterm");
263 fprintf (stderr
, "\032/%s\n", pwd
);
268 execute_prompt_command ()
270 char *command_to_execute
;
272 command_to_execute
= get_string_value ("PROMPT_COMMAND");
273 if (command_to_execute
)
274 execute_variable_command (command_to_execute
, "PROMPT_COMMAND");
276 /* Call the YACC-generated parser and return the status of the parse.
277 Input is read from the current input stream (bash_input). yyparse
278 leaves the parsed command in the global variable GLOBAL_COMMAND.
279 This is where PROMPT_COMMAND is executed. */
286 run_pending_traps ();
288 /* Allow the execution of a random command just before the printing
289 of each primary prompt. If the shell variable PROMPT_COMMAND
290 is set then the value of it is the command to execute. */
291 /* The tests are a combination of SHOULD_PROMPT() and prompt_again()
292 from parse.y, which are the conditions under which the prompt is
294 if (interactive
&& bash_input
.type
!= st_string
&& parser_expanding_alias() == 0)
296 execute_prompt_command ();
298 if (running_under_emacs
== 2)
299 send_pwd_to_eterm (); /* Yuck */
302 current_command_line_count
= 0;
306 gather_here_documents ();
311 /* Read and parse a command, returning the status of the parse. The command
312 is left in the globval variable GLOBAL_COMMAND for use by reader_loop.
313 This is where the shell timeout code is executed. */
317 SHELL_VAR
*tmout_var
;
318 int tmout_len
, result
;
319 SigHandler
*old_alrm
;
321 set_current_prompt_level (1);
322 global_command
= (COMMAND
*)NULL
;
324 /* Only do timeouts if interactive. */
325 tmout_var
= (SHELL_VAR
*)NULL
;
327 old_alrm
= (SigHandler
*)NULL
;
331 tmout_var
= find_variable ("TMOUT");
333 if (tmout_var
&& var_isset (tmout_var
))
335 tmout_len
= atoi (value_cell (tmout_var
));
338 old_alrm
= set_signal_handler (SIGALRM
, alrm_catcher
);
346 current_command_line_count
= 0;
347 result
= parse_command ();
349 if (interactive
&& tmout_var
&& (tmout_len
> 0))
352 set_signal_handler (SIGALRM
, old_alrm
);