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>
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 #if defined (HAVE_POSIX_SIGNALS)
58 extern sigset_t top_level_mask
;
61 static void send_pwd_to_eterm
__P((void));
62 static sighandler alrm_catcher
__P((int));
64 /* Read and execute commands until EOF is reached. This assumes that
65 the input source has already been initialized. */
69 int our_indirection_level
;
70 COMMAND
* volatile current_command
;
72 USE_VAR(current_command
);
74 current_command
= (COMMAND
*)NULL
;
76 our_indirection_level
= ++indirection_level
;
78 while (EOF_Reached
== 0)
82 code
= setjmp_nosigs (top_level
);
84 #if defined (PROCESS_SUBSTITUTION)
86 #endif /* PROCESS_SUBSTITUTION */
88 /* XXX - why do we set this every time through the loop? */
89 if (interactive_shell
&& signal_is_ignored (SIGINT
) == 0)
90 set_signal_handler (SIGINT
, sigint_sighandler
);
92 if (code
!= NOT_JUMPED
)
94 indirection_level
= our_indirection_level
;
98 /* Some kind of throw to top_level has occurred. */
102 current_command
= (COMMAND
*)NULL
;
103 if (exit_immediately_on_error
)
104 variable_context
= 0; /* not in a function */
109 /* Make sure the exit status is reset to a non-zero value, but
110 leave existing non-zero values (e.g., > 128 on signal)
112 if (last_command_exit_value
== 0)
113 last_command_exit_value
= EXECUTION_FAILURE
;
114 if (subshell_environment
)
116 current_command
= (COMMAND
*)NULL
;
120 /* Obstack free command elements, etc. */
123 dispose_command (current_command
);
124 current_command
= (COMMAND
*)NULL
;
126 #if defined (HAVE_POSIX_SIGNALS)
127 sigprocmask (SIG_SETMASK
, &top_level_mask
, (sigset_t
*)NULL
);
132 command_error ("reader_loop", CMDERR_BADJUMP
, code
, 0);
138 dispose_used_env_vars ();
140 #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
141 /* Attempt to reclaim memory allocated with alloca (). */
145 if (read_command () == 0)
147 if (interactive_shell
== 0 && read_but_dont_execute
)
149 last_command_exit_value
= EXECUTION_SUCCESS
;
150 dispose_command (global_command
);
151 global_command
= (COMMAND
*)NULL
;
153 else if (current_command
= global_command
)
155 global_command
= (COMMAND
*)NULL
;
156 current_command_number
++;
160 execute_command (current_command
);
167 dispose_command (current_command
);
168 current_command
= (COMMAND
*)NULL
;
174 /* Parse error, maybe discard rest of stream if not interactive. */
175 if (interactive
== 0)
178 if (just_one_command
)
182 return (last_command_exit_value
);
189 printf (_("\007timed out waiting for input: auto-logout\n"));
191 bash_logout (); /* run ~/.bash_logout if this is a login shell */
192 jump_to_top_level (EXITPROG
);
196 /* Send an escape sequence to emacs term mode to tell it the
197 current working directory. */
204 pwd
= get_string_value ("PWD");
206 f
= pwd
= get_working_directory ("eterm");
207 fprintf (stderr
, "\032/%s\n", pwd
);
211 /* Call the YACC-generated parser and return the status of the parse.
212 Input is read from the current input stream (bash_input). yyparse
213 leaves the parsed command in the global variable GLOBAL_COMMAND.
214 This is where PROMPT_COMMAND is executed. */
219 char *command_to_execute
;
222 run_pending_traps ();
224 /* Allow the execution of a random command just before the printing
225 of each primary prompt. If the shell variable PROMPT_COMMAND
226 is set then the value of it is the command to execute. */
227 if (interactive
&& bash_input
.type
!= st_string
)
229 command_to_execute
= get_string_value ("PROMPT_COMMAND");
230 if (command_to_execute
)
231 execute_variable_command (command_to_execute
, "PROMPT_COMMAND");
233 if (running_under_emacs
== 2)
234 send_pwd_to_eterm (); /* Yuck */
237 current_command_line_count
= 0;
241 gather_here_documents ();
246 /* Read and parse a command, returning the status of the parse. The command
247 is left in the globval variable GLOBAL_COMMAND for use by reader_loop.
248 This is where the shell timeout code is executed. */
252 SHELL_VAR
*tmout_var
;
253 int tmout_len
, result
;
254 SigHandler
*old_alrm
;
256 set_current_prompt_level (1);
257 global_command
= (COMMAND
*)NULL
;
259 /* Only do timeouts if interactive. */
260 tmout_var
= (SHELL_VAR
*)NULL
;
262 old_alrm
= (SigHandler
*)NULL
;
266 tmout_var
= find_variable ("TMOUT");
268 if (tmout_var
&& var_isset (tmout_var
))
270 tmout_len
= atoi (value_cell (tmout_var
));
273 old_alrm
= set_signal_handler (SIGALRM
, alrm_catcher
);
281 current_command_line_count
= 0;
282 result
= parse_command ();
284 if (interactive
&& tmout_var
&& (tmout_len
> 0))
287 set_signal_handler (SIGALRM
, old_alrm
);