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>
41 #include "builtins/common.h"
44 #include "execute_cmd.h"
47 # include "bashhist.h"
50 extern int EOF_reached
;
51 extern int indirection_level
;
52 extern int posixly_correct
;
53 extern int subshell_environment
, running_under_emacs
;
54 extern int last_command_exit_value
, stdin_redir
;
55 extern int need_here_doc
;
56 extern int current_command_number
, current_command_line_count
, line_number
;
57 extern int expand_aliases
;
58 extern char *ps0_prompt
;
60 #if defined (HAVE_POSIX_SIGNALS)
61 extern sigset_t top_level_mask
;
64 static void send_pwd_to_eterm
__P((void));
65 static sighandler alrm_catcher
__P((int));
67 /* Read and execute commands until EOF is reached. This assumes that
68 the input source has already been initialized. */
72 int our_indirection_level
;
73 COMMAND
* volatile current_command
;
75 USE_VAR(current_command
);
77 current_command
= (COMMAND
*)NULL
;
79 our_indirection_level
= ++indirection_level
;
81 while (EOF_Reached
== 0)
85 code
= setjmp_nosigs (top_level
);
87 #if defined (PROCESS_SUBSTITUTION)
89 #endif /* PROCESS_SUBSTITUTION */
91 /* XXX - why do we set this every time through the loop? And why do
92 it if SIGINT is trapped in an interactive shell? */
93 if (interactive_shell
&& signal_is_ignored (SIGINT
) == 0)
94 set_signal_handler (SIGINT
, sigint_sighandler
);
96 if (code
!= NOT_JUMPED
)
98 indirection_level
= our_indirection_level
;
102 /* Some kind of throw to top_level has occurred. */
106 current_command
= (COMMAND
*)NULL
;
107 if (exit_immediately_on_error
)
108 variable_context
= 0; /* not in a function */
113 /* Make sure the exit status is reset to a non-zero value, but
114 leave existing non-zero values (e.g., > 128 on signal)
116 if (last_command_exit_value
== 0)
117 last_command_exit_value
= EXECUTION_FAILURE
;
118 if (subshell_environment
)
120 current_command
= (COMMAND
*)NULL
;
124 /* Obstack free command elements, etc. */
127 dispose_command (current_command
);
128 current_command
= (COMMAND
*)NULL
;
130 #if defined (HAVE_POSIX_SIGNALS)
131 sigprocmask (SIG_SETMASK
, &top_level_mask
, (sigset_t
*)NULL
);
136 command_error ("reader_loop", CMDERR_BADJUMP
, code
, 0);
142 dispose_used_env_vars ();
144 #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
145 /* Attempt to reclaim memory allocated with alloca (). */
149 if (read_command () == 0)
151 if (interactive_shell
== 0 && read_but_dont_execute
)
153 last_command_exit_value
= EXECUTION_SUCCESS
;
154 dispose_command (global_command
);
155 global_command
= (COMMAND
*)NULL
;
157 else if (current_command
= global_command
)
159 global_command
= (COMMAND
*)NULL
;
160 current_command_number
++;
165 /* If the shell is interactive, expand and display $PS0 after reading a
166 command (possibly a list or pipeline) and before executing it. */
167 if (interactive
&& ps0_prompt
)
171 ps0_string
= decode_prompt_string (ps0_prompt
);
172 if (ps0_string
&& *ps0_string
)
174 fprintf (stderr
, "%s", ps0_string
);
180 execute_command (current_command
);
187 dispose_command (current_command
);
188 current_command
= (COMMAND
*)NULL
;
194 /* Parse error, maybe discard rest of stream if not interactive. */
195 if (interactive
== 0)
198 if (just_one_command
)
202 return (last_command_exit_value
);
209 printf (_("\007timed out waiting for input: auto-logout\n"));
211 bash_logout (); /* run ~/.bash_logout if this is a login shell */
212 jump_to_top_level (EXITPROG
);
216 /* Send an escape sequence to emacs term mode to tell it the
217 current working directory. */
224 pwd
= get_string_value ("PWD");
226 f
= pwd
= get_working_directory ("eterm");
227 fprintf (stderr
, "\032/%s\n", pwd
);
231 /* Call the YACC-generated parser and return the status of the parse.
232 Input is read from the current input stream (bash_input). yyparse
233 leaves the parsed command in the global variable GLOBAL_COMMAND.
234 This is where PROMPT_COMMAND is executed. */
239 char *command_to_execute
;
242 run_pending_traps ();
244 /* Allow the execution of a random command just before the printing
245 of each primary prompt. If the shell variable PROMPT_COMMAND
246 is set then the value of it is the command to execute. */
247 /* The tests are a combination of SHOULD_PROMPT() and prompt_again()
248 from parse.y, which are the conditions under which the prompt is
250 if (interactive
&& bash_input
.type
!= st_string
&& parser_expanding_alias() == 0)
252 command_to_execute
= get_string_value ("PROMPT_COMMAND");
253 if (command_to_execute
)
254 execute_variable_command (command_to_execute
, "PROMPT_COMMAND");
256 if (running_under_emacs
== 2)
257 send_pwd_to_eterm (); /* Yuck */
260 current_command_line_count
= 0;
264 gather_here_documents ();
269 /* Read and parse a command, returning the status of the parse. The command
270 is left in the globval variable GLOBAL_COMMAND for use by reader_loop.
271 This is where the shell timeout code is executed. */
275 SHELL_VAR
*tmout_var
;
276 int tmout_len
, result
;
277 SigHandler
*old_alrm
;
279 set_current_prompt_level (1);
280 global_command
= (COMMAND
*)NULL
;
282 /* Only do timeouts if interactive. */
283 tmout_var
= (SHELL_VAR
*)NULL
;
285 old_alrm
= (SigHandler
*)NULL
;
289 tmout_var
= find_variable ("TMOUT");
291 if (tmout_var
&& var_isset (tmout_var
))
293 tmout_len
= atoi (value_cell (tmout_var
));
296 old_alrm
= set_signal_handler (SIGALRM
, alrm_catcher
);
304 current_command_line_count
= 0;
305 result
= parse_command ();
307 if (interactive
&& tmout_var
&& (tmout_len
> 0))
310 set_signal_handler (SIGALRM
, old_alrm
);