]> git.ipfire.org Git - thirdparty/bash.git/blob - eval.c
Bash-4.2 patch 17
[thirdparty/bash.git] / eval.c
1 /* eval.c -- reading and evaluating commands. */
2
3 /* Copyright (C) 1996-2009 Free Software Foundation, Inc.
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
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.
11
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.
16
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/>.
19 */
20
21 #include "config.h"
22
23 #if defined (HAVE_UNISTD_H)
24 # ifdef _MINIX
25 # include <sys/types.h>
26 # endif
27 # include <unistd.h>
28 #endif
29
30 #include "bashansi.h"
31 #include <stdio.h>
32
33 #include "bashintl.h"
34
35 #include "shell.h"
36 #include "flags.h"
37 #include "trap.h"
38
39 #include "builtins/common.h"
40
41 #include "input.h"
42 #include "execute_cmd.h"
43
44 #if defined (HISTORY)
45 # include "bashhist.h"
46 #endif
47
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;
56
57 static void send_pwd_to_eterm __P((void));
58 static sighandler alrm_catcher __P((int));
59
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 ()
64 {
65 int our_indirection_level;
66 COMMAND * volatile current_command;
67
68 USE_VAR(current_command);
69
70 current_command = (COMMAND *)NULL;
71
72 our_indirection_level = ++indirection_level;
73
74 while (EOF_Reached == 0)
75 {
76 int code;
77
78 code = setjmp (top_level);
79
80 #if defined (PROCESS_SUBSTITUTION)
81 unlink_fifo_list ();
82 #endif /* PROCESS_SUBSTITUTION */
83
84 if (interactive_shell && signal_is_ignored (SIGINT) == 0)
85 set_signal_handler (SIGINT, sigint_sighandler);
86
87 if (code != NOT_JUMPED)
88 {
89 indirection_level = our_indirection_level;
90
91 switch (code)
92 {
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;
102
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)
110 {
111 current_command = (COMMAND *)NULL;
112 EOF_Reached = EOF;
113 goto exec_done;
114 }
115 /* Obstack free command elements, etc. */
116 if (current_command)
117 {
118 dispose_command (current_command);
119 current_command = (COMMAND *)NULL;
120 }
121 break;
122
123 default:
124 command_error ("reader_loop", CMDERR_BADJUMP, code, 0);
125 }
126 }
127
128 executing = 0;
129 if (temporary_env)
130 dispose_used_env_vars ();
131
132 #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
133 /* Attempt to reclaim memory allocated with alloca (). */
134 (void) alloca (0);
135 #endif
136
137 if (read_command () == 0)
138 {
139 if (interactive_shell == 0 && read_but_dont_execute)
140 {
141 last_command_exit_value = EXECUTION_SUCCESS;
142 dispose_command (global_command);
143 global_command = (COMMAND *)NULL;
144 }
145 else if (current_command = global_command)
146 {
147 global_command = (COMMAND *)NULL;
148 current_command_number++;
149
150 executing = 1;
151 stdin_redir = 0;
152 execute_command (current_command);
153
154 exec_done:
155 QUIT;
156
157 if (current_command)
158 {
159 dispose_command (current_command);
160 current_command = (COMMAND *)NULL;
161 }
162 }
163 }
164 else
165 {
166 /* Parse error, maybe discard rest of stream if not interactive. */
167 if (interactive == 0)
168 EOF_Reached = EOF;
169 }
170 if (just_one_command)
171 EOF_Reached = EOF;
172 }
173 indirection_level--;
174 return (last_command_exit_value);
175 }
176
177 static sighandler
178 alrm_catcher(i)
179 int i;
180 {
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);
186 }
187
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 ()
192 {
193 char *pwd;
194
195 pwd = get_string_value ("PWD");
196 if (pwd == 0)
197 pwd = get_working_directory ("eterm");
198 fprintf (stderr, "\032/%s\n", pwd);
199 }
200
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. */
205 int
206 parse_command ()
207 {
208 int r;
209 char *command_to_execute;
210
211 need_here_doc = 0;
212 run_pending_traps ();
213
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)
218 {
219 command_to_execute = get_string_value ("PROMPT_COMMAND");
220 if (command_to_execute)
221 execute_variable_command (command_to_execute, "PROMPT_COMMAND");
222
223 if (running_under_emacs == 2)
224 send_pwd_to_eterm (); /* Yuck */
225 }
226
227 current_command_line_count = 0;
228 r = yyparse ();
229
230 if (need_here_doc)
231 gather_here_documents ();
232
233 return (r);
234 }
235
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. */
239 int
240 read_command ()
241 {
242 SHELL_VAR *tmout_var;
243 int tmout_len, result;
244 SigHandler *old_alrm;
245
246 set_current_prompt_level (1);
247 global_command = (COMMAND *)NULL;
248
249 /* Only do timeouts if interactive. */
250 tmout_var = (SHELL_VAR *)NULL;
251 tmout_len = 0;
252 old_alrm = (SigHandler *)NULL;
253
254 if (interactive)
255 {
256 tmout_var = find_variable ("TMOUT");
257
258 if (tmout_var && var_isset (tmout_var))
259 {
260 tmout_len = atoi (value_cell (tmout_var));
261 if (tmout_len > 0)
262 {
263 old_alrm = set_signal_handler (SIGALRM, alrm_catcher);
264 alarm (tmout_len);
265 }
266 }
267 }
268
269 QUIT;
270
271 current_command_line_count = 0;
272 result = parse_command ();
273
274 if (interactive && tmout_var && (tmout_len > 0))
275 {
276 alarm(0);
277 set_signal_handler (SIGALRM, old_alrm);
278 }
279
280 return (result);
281 }