]> git.ipfire.org Git - thirdparty/bash.git/blob - eval.c
Bash-5.0 patch 4: the wait builtin without arguments only waits for known children...
[thirdparty/bash.git] / eval.c
1 /* eval.c -- reading and evaluating commands. */
2
3 /* Copyright (C) 1996-2011 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 <signal.h>
34
35 #include "bashintl.h"
36
37 #include "shell.h"
38 #include "parser.h"
39 #include "flags.h"
40 #include "trap.h"
41
42 #include "builtins/common.h"
43
44 #include "input.h"
45 #include "execute_cmd.h"
46
47 #if defined (HISTORY)
48 # include "bashhist.h"
49 #endif
50
51 #if defined (HAVE_POSIX_SIGNALS)
52 extern sigset_t top_level_mask;
53 #endif
54
55 static void send_pwd_to_eterm __P((void));
56 static sighandler alrm_catcher __P((int));
57
58 /* Read and execute commands until EOF is reached. This assumes that
59 the input source has already been initialized. */
60 int
61 reader_loop ()
62 {
63 int our_indirection_level;
64 COMMAND * volatile current_command;
65
66 USE_VAR(current_command);
67
68 current_command = (COMMAND *)NULL;
69
70 our_indirection_level = ++indirection_level;
71
72 if (just_one_command)
73 reset_readahead_token ();
74
75 while (EOF_Reached == 0)
76 {
77 int code;
78
79 code = setjmp_nosigs (top_level);
80
81 #if defined (PROCESS_SUBSTITUTION)
82 unlink_fifo_list ();
83 #endif /* PROCESS_SUBSTITUTION */
84
85 /* XXX - why do we set this every time through the loop? And why do
86 it if SIGINT is trapped in an interactive shell? */
87 if (interactive_shell && signal_is_ignored (SIGINT) == 0 && signal_is_trapped (SIGINT) == 0)
88 set_signal_handler (SIGINT, sigint_sighandler);
89
90 if (code != NOT_JUMPED)
91 {
92 indirection_level = our_indirection_level;
93
94 switch (code)
95 {
96 /* Some kind of throw to top_level has occurred. */
97 case FORCE_EOF:
98 case ERREXIT:
99 case EXITPROG:
100 current_command = (COMMAND *)NULL;
101 if (exit_immediately_on_error)
102 variable_context = 0; /* not in a function */
103 EOF_Reached = EOF;
104 goto exec_done;
105
106 case DISCARD:
107 /* Make sure the exit status is reset to a non-zero value, but
108 leave existing non-zero values (e.g., > 128 on signal)
109 alone. */
110 if (last_command_exit_value == 0)
111 last_command_exit_value = EXECUTION_FAILURE;
112 if (subshell_environment)
113 {
114 current_command = (COMMAND *)NULL;
115 EOF_Reached = EOF;
116 goto exec_done;
117 }
118 /* Obstack free command elements, etc. */
119 if (current_command)
120 {
121 dispose_command (current_command);
122 current_command = (COMMAND *)NULL;
123 }
124 #if defined (HAVE_POSIX_SIGNALS)
125 sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
126 #endif
127 break;
128
129 default:
130 command_error ("reader_loop", CMDERR_BADJUMP, code, 0);
131 }
132 }
133
134 executing = 0;
135 if (temporary_env)
136 dispose_used_env_vars ();
137
138 #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
139 /* Attempt to reclaim memory allocated with alloca (). */
140 (void) alloca (0);
141 #endif
142
143 if (read_command () == 0)
144 {
145 if (interactive_shell == 0 && read_but_dont_execute)
146 {
147 last_command_exit_value = EXECUTION_SUCCESS;
148 dispose_command (global_command);
149 global_command = (COMMAND *)NULL;
150 }
151 else if (current_command = global_command)
152 {
153 global_command = (COMMAND *)NULL;
154
155 /* If the shell is interactive, expand and display $PS0 after reading a
156 command (possibly a list or pipeline) and before executing it. */
157 if (interactive && ps0_prompt)
158 {
159 char *ps0_string;
160
161 ps0_string = decode_prompt_string (ps0_prompt);
162 if (ps0_string && *ps0_string)
163 {
164 fprintf (stderr, "%s", ps0_string);
165 fflush (stderr);
166 }
167 free (ps0_string);
168 }
169
170 current_command_number++;
171
172 executing = 1;
173 stdin_redir = 0;
174
175 execute_command (current_command);
176
177 exec_done:
178 QUIT;
179
180 if (current_command)
181 {
182 dispose_command (current_command);
183 current_command = (COMMAND *)NULL;
184 }
185 }
186 }
187 else
188 {
189 /* Parse error, maybe discard rest of stream if not interactive. */
190 if (interactive == 0)
191 EOF_Reached = EOF;
192 }
193 if (just_one_command)
194 EOF_Reached = EOF;
195 }
196 indirection_level--;
197 return (last_command_exit_value);
198 }
199
200 /* Pretty print shell scripts */
201 int
202 pretty_print_loop ()
203 {
204 COMMAND *current_command;
205 char *command_to_print;
206 int code;
207 int global_posix_mode, last_was_newline;
208
209 global_posix_mode = posixly_correct;
210 last_was_newline = 0;
211 while (EOF_Reached == 0)
212 {
213 code = setjmp_nosigs (top_level);
214 if (code)
215 return (EXECUTION_FAILURE);
216 if (read_command() == 0)
217 {
218 current_command = global_command;
219 global_command = 0;
220 posixly_correct = 1; /* print posix-conformant */
221 if (current_command && (command_to_print = make_command_string (current_command)))
222 {
223 printf ("%s\n", command_to_print); /* for now */
224 last_was_newline = 0;
225 }
226 else if (last_was_newline == 0)
227 {
228 printf ("\n");
229 last_was_newline = 1;
230 }
231 posixly_correct = global_posix_mode;
232 dispose_command (current_command);
233 }
234 else
235 return (EXECUTION_FAILURE);
236 }
237
238 return (EXECUTION_SUCCESS);
239 }
240
241 static sighandler
242 alrm_catcher(i)
243 int i;
244 {
245 printf (_("\007timed out waiting for input: auto-logout\n"));
246 fflush (stdout);
247 bash_logout (); /* run ~/.bash_logout if this is a login shell */
248 jump_to_top_level (EXITPROG);
249 SIGRETURN (0);
250 }
251
252 /* Send an escape sequence to emacs term mode to tell it the
253 current working directory. */
254 static void
255 send_pwd_to_eterm ()
256 {
257 char *pwd, *f;
258
259 f = 0;
260 pwd = get_string_value ("PWD");
261 if (pwd == 0)
262 f = pwd = get_working_directory ("eterm");
263 fprintf (stderr, "\032/%s\n", pwd);
264 free (f);
265 }
266
267 static void
268 execute_prompt_command ()
269 {
270 char *command_to_execute;
271
272 command_to_execute = get_string_value ("PROMPT_COMMAND");
273 if (command_to_execute)
274 execute_variable_command (command_to_execute, "PROMPT_COMMAND");
275 }
276 /* Call the YACC-generated parser and return the status of the parse.
277 Input is read from the current input stream (bash_input). yyparse
278 leaves the parsed command in the global variable GLOBAL_COMMAND.
279 This is where PROMPT_COMMAND is executed. */
280 int
281 parse_command ()
282 {
283 int r;
284
285 need_here_doc = 0;
286 run_pending_traps ();
287
288 /* Allow the execution of a random command just before the printing
289 of each primary prompt. If the shell variable PROMPT_COMMAND
290 is set then the value of it is the command to execute. */
291 /* The tests are a combination of SHOULD_PROMPT() and prompt_again()
292 from parse.y, which are the conditions under which the prompt is
293 actually printed. */
294 if (interactive && bash_input.type != st_string && parser_expanding_alias() == 0)
295 {
296 execute_prompt_command ();
297
298 if (running_under_emacs == 2)
299 send_pwd_to_eterm (); /* Yuck */
300 }
301
302 current_command_line_count = 0;
303 r = yyparse ();
304
305 if (need_here_doc)
306 gather_here_documents ();
307
308 return (r);
309 }
310
311 /* Read and parse a command, returning the status of the parse. The command
312 is left in the globval variable GLOBAL_COMMAND for use by reader_loop.
313 This is where the shell timeout code is executed. */
314 int
315 read_command ()
316 {
317 SHELL_VAR *tmout_var;
318 int tmout_len, result;
319 SigHandler *old_alrm;
320
321 set_current_prompt_level (1);
322 global_command = (COMMAND *)NULL;
323
324 /* Only do timeouts if interactive. */
325 tmout_var = (SHELL_VAR *)NULL;
326 tmout_len = 0;
327 old_alrm = (SigHandler *)NULL;
328
329 if (interactive)
330 {
331 tmout_var = find_variable ("TMOUT");
332
333 if (tmout_var && var_isset (tmout_var))
334 {
335 tmout_len = atoi (value_cell (tmout_var));
336 if (tmout_len > 0)
337 {
338 old_alrm = set_signal_handler (SIGALRM, alrm_catcher);
339 alarm (tmout_len);
340 }
341 }
342 }
343
344 QUIT;
345
346 current_command_line_count = 0;
347 result = parse_command ();
348
349 if (interactive && tmout_var && (tmout_len > 0))
350 {
351 alarm(0);
352 set_signal_handler (SIGALRM, old_alrm);
353 }
354
355 return (result);
356 }