1 /* eval.c -- reading and evaluating commands. */
3 /* Copyright (C) 1996-2009 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>
39 #include "builtins/common.h"
42 #include "execute_cmd.h"
45 # include "bashhist.h"
48 extern int EOF_reached
;
49 extern int indirection_level
;
50 extern int posixly_correct
;
51 extern int subshell_environment
, running_under_emacs
;
52 extern int last_command_exit_value
, stdin_redir
;
53 extern int need_here_doc
;
54 extern int current_command_number
, current_command_line_count
, line_number
;
55 extern int expand_aliases
;
57 static void send_pwd_to_eterm
__P((void));
58 static sighandler alrm_catcher
__P((int));
60 /* Read and execute commands until EOF is reached. This assumes that
61 the input source has already been initialized. */
65 int our_indirection_level
;
66 COMMAND
* volatile current_command
;
68 USE_VAR(current_command
);
70 current_command
= (COMMAND
*)NULL
;
72 our_indirection_level
= ++indirection_level
;
74 while (EOF_Reached
== 0)
78 code
= setjmp (top_level
);
80 #if defined (PROCESS_SUBSTITUTION)
82 #endif /* PROCESS_SUBSTITUTION */
84 if (interactive_shell
&& signal_is_ignored (SIGINT
) == 0)
85 set_signal_handler (SIGINT
, sigint_sighandler
);
87 if (code
!= NOT_JUMPED
)
89 indirection_level
= our_indirection_level
;
93 /* Some kind of throw to top_level has occured. */
97 current_command
= (COMMAND
*)NULL
;
98 if (exit_immediately_on_error
)
99 variable_context
= 0; /* not in a function */
104 /* Make sure the exit status is reset to a non-zero value, but
105 leave existing non-zero values (e.g., > 128 on signal)
107 if (last_command_exit_value
== 0)
108 last_command_exit_value
= EXECUTION_FAILURE
;
109 if (subshell_environment
)
111 current_command
= (COMMAND
*)NULL
;
115 /* Obstack free command elements, etc. */
118 dispose_command (current_command
);
119 current_command
= (COMMAND
*)NULL
;
124 command_error ("reader_loop", CMDERR_BADJUMP
, code
, 0);
130 dispose_used_env_vars ();
132 #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
133 /* Attempt to reclaim memory allocated with alloca (). */
137 if (read_command () == 0)
139 if (interactive_shell
== 0 && read_but_dont_execute
)
141 last_command_exit_value
= EXECUTION_SUCCESS
;
142 dispose_command (global_command
);
143 global_command
= (COMMAND
*)NULL
;
145 else if (current_command
= global_command
)
147 global_command
= (COMMAND
*)NULL
;
148 current_command_number
++;
152 execute_command (current_command
);
159 dispose_command (current_command
);
160 current_command
= (COMMAND
*)NULL
;
166 /* Parse error, maybe discard rest of stream if not interactive. */
167 if (interactive
== 0)
170 if (just_one_command
)
174 return (last_command_exit_value
);
181 printf (_("\007timed out waiting for input: auto-logout\n"));
183 bash_logout (); /* run ~/.bash_logout if this is a login shell */
184 jump_to_top_level (EXITPROG
);
188 /* Send an escape sequence to emacs term mode to tell it the
189 current working directory. */
195 pwd
= get_string_value ("PWD");
197 pwd
= get_working_directory ("eterm");
198 fprintf (stderr
, "\032/%s\n", pwd
);
201 /* Call the YACC-generated parser and return the status of the parse.
202 Input is read from the current input stream (bash_input). yyparse
203 leaves the parsed command in the global variable GLOBAL_COMMAND.
204 This is where PROMPT_COMMAND is executed. */
209 char *command_to_execute
;
212 run_pending_traps ();
214 /* Allow the execution of a random command just before the printing
215 of each primary prompt. If the shell variable PROMPT_COMMAND
216 is set then the value of it is the command to execute. */
217 if (interactive
&& bash_input
.type
!= st_string
)
219 command_to_execute
= get_string_value ("PROMPT_COMMAND");
220 if (command_to_execute
)
221 execute_variable_command (command_to_execute
, "PROMPT_COMMAND");
223 if (running_under_emacs
== 2)
224 send_pwd_to_eterm (); /* Yuck */
227 current_command_line_count
= 0;
231 gather_here_documents ();
236 /* Read and parse a command, returning the status of the parse. The command
237 is left in the globval variable GLOBAL_COMMAND for use by reader_loop.
238 This is where the shell timeout code is executed. */
242 SHELL_VAR
*tmout_var
;
243 int tmout_len
, result
;
244 SigHandler
*old_alrm
;
246 set_current_prompt_level (1);
247 global_command
= (COMMAND
*)NULL
;
249 /* Only do timeouts if interactive. */
250 tmout_var
= (SHELL_VAR
*)NULL
;
252 old_alrm
= (SigHandler
*)NULL
;
256 tmout_var
= find_variable ("TMOUT");
258 if (tmout_var
&& var_isset (tmout_var
))
260 tmout_len
= atoi (value_cell (tmout_var
));
263 old_alrm
= set_signal_handler (SIGALRM
, alrm_catcher
);
271 current_command_line_count
= 0;
272 result
= parse_command ();
274 if (interactive
&& tmout_var
&& (tmout_len
> 0))
277 set_signal_handler (SIGALRM
, old_alrm
);