Patch-ID: bash40-035
[bash.git] / eval.c
blob9011e0bf469c336e950625adb7efab3901b8e4fa
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/>.
21 #include "config.h"
23 #if defined (HAVE_UNISTD_H)
24 # ifdef _MINIX
25 # include <sys/types.h>
26 # endif
27 # include <unistd.h>
28 #endif
30 #include "bashansi.h"
31 #include <stdio.h>
33 #include "bashintl.h"
35 #include "shell.h"
36 #include "flags.h"
37 #include "trap.h"
39 #include "builtins/common.h"
41 #include "input.h"
42 #include "execute_cmd.h"
44 #if defined (HISTORY)
45 # include "bashhist.h"
46 #endif
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. */
62 int
63 reader_loop ()
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)
76 int code;
78 code = setjmp (top_level);
80 #if defined (PROCESS_SUBSTITUTION)
81 unlink_fifo_list ();
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;
91 switch (code)
93 /* Some kind of throw to top_level has occured. */
94 case FORCE_EOF:
95 case ERREXIT:
96 case EXITPROG:
97 current_command = (COMMAND *)NULL;
98 if (exit_immediately_on_error)
99 variable_context = 0; /* not in a function */
100 EOF_Reached = EOF;
101 goto exec_done;
103 case DISCARD:
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)
106 alone. */
107 if (last_command_exit_value == 0)
108 last_command_exit_value = EXECUTION_FAILURE;
109 if (subshell_environment)
111 current_command = (COMMAND *)NULL;
112 EOF_Reached = EOF;
113 goto exec_done;
115 /* Obstack free command elements, etc. */
116 if (current_command)
118 dispose_command (current_command);
119 current_command = (COMMAND *)NULL;
121 break;
123 default:
124 command_error ("reader_loop", CMDERR_BADJUMP, code, 0);
128 executing = 0;
129 if (temporary_env)
130 dispose_used_env_vars ();
132 #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
133 /* Attempt to reclaim memory allocated with alloca (). */
134 (void) alloca (0);
135 #endif
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++;
150 executing = 1;
151 stdin_redir = 0;
152 execute_command (current_command);
154 exec_done:
155 QUIT;
157 if (current_command)
159 dispose_command (current_command);
160 current_command = (COMMAND *)NULL;
164 else
166 /* Parse error, maybe discard rest of stream if not interactive. */
167 if (interactive == 0)
168 EOF_Reached = EOF;
170 if (just_one_command)
171 EOF_Reached = EOF;
173 indirection_level--;
174 return (last_command_exit_value);
177 static sighandler
178 alrm_catcher(i)
179 int i;
181 printf (_("\007timed out waiting for input: auto-logout\n"));
182 fflush (stdout);
183 bash_logout (); /* run ~/.bash_logout if this is a login shell */
184 jump_to_top_level (EXITPROG);
185 SIGRETURN (0);
188 /* Send an escape sequence to emacs term mode to tell it the
189 current working directory. */
190 static void
191 send_pwd_to_eterm ()
193 char *pwd;
195 pwd = get_string_value ("PWD");
196 if (pwd == 0)
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. */
206 parse_command ()
208 int r;
209 char *command_to_execute;
211 need_here_doc = 0;
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;
228 r = yyparse ();
230 if (need_here_doc)
231 gather_here_documents ();
233 return (r);
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. */
240 read_command ()
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;
251 tmout_len = 0;
252 old_alrm = (SigHandler *)NULL;
254 if (interactive)
256 tmout_var = find_variable ("TMOUT");
258 if (tmout_var && var_isset (tmout_var))
260 tmout_len = atoi (value_cell (tmout_var));
261 if (tmout_len > 0)
263 old_alrm = set_signal_handler (SIGALRM, alrm_catcher);
264 alarm (tmout_len);
269 QUIT;
271 current_command_line_count = 0;
272 result = parse_command ();
274 if (interactive && tmout_var && (tmout_len > 0))
276 alarm(0);
277 set_signal_handler (SIGALRM, old_alrm);
280 return (result);