blob: f02d6e40e8812b7430b2b79179f20d31af8e5369 [file] [log] [blame]
Jari Aaltobb706242000-03-17 21:46:59 +00001/* eval.c -- reading and evaluating commands. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002
Chet Rameyac50fba2014-02-26 09:36:43 -05003/* Copyright (C) 1996-2011 Free Software Foundation, Inc.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004
Jari Aaltobb706242000-03-17 21:46:59 +00005 This file is part of GNU Bash, the Bourne Again SHell.
6
Jari Aalto31859422009-01-12 13:36:28 +00007 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.
Jari Aaltoccc6cda1996-12-23 17:02:34 +000011
Jari Aalto31859422009-01-12 13:36:28 +000012 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.
Jari Aaltoccc6cda1996-12-23 17:02:34 +000016
Jari Aaltobb706242000-03-17 21:46:59 +000017 You should have received a copy of the GNU General Public License
Jari Aalto31859422009-01-12 13:36:28 +000018 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
Jari Aaltoccc6cda1996-12-23 17:02:34 +000020
21#include "config.h"
22
23#if defined (HAVE_UNISTD_H)
Jari Aaltocce855b1998-04-17 19:52:44 +000024# ifdef _MINIX
25# include <sys/types.h>
26# endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +000027# include <unistd.h>
28#endif
29
30#include "bashansi.h"
31#include <stdio.h>
32
Chet Rameya0c0a002016-09-15 16:59:08 -040033#include <signal.h>
34
Jari Aaltob80f6442004-07-27 13:29:18 +000035#include "bashintl.h"
36
Jari Aaltoccc6cda1996-12-23 17:02:34 +000037#include "shell.h"
Chet Rameyd233b482019-01-07 09:27:52 -050038#include "parser.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000039#include "flags.h"
40#include "trap.h"
41
42#include "builtins/common.h"
43
44#include "input.h"
45#include "execute_cmd.h"
46
Jari Aaltob72432f1999-02-19 17:11:39 +000047#if defined (HISTORY)
48# include "bashhist.h"
49#endif
50
Chet Rameyac50fba2014-02-26 09:36:43 -050051#if defined (HAVE_POSIX_SIGNALS)
52extern sigset_t top_level_mask;
53#endif
54
Jari Aalto7117c2d2002-07-17 14:10:11 +000055static void send_pwd_to_eterm __P((void));
56static sighandler alrm_catcher __P((int));
57
Jari Aaltoccc6cda1996-12-23 17:02:34 +000058/* Read and execute commands until EOF is reached. This assumes that
59 the input source has already been initialized. */
60int
61reader_loop ()
62{
63 int our_indirection_level;
Jari Aalto95732b42005-12-07 14:08:12 +000064 COMMAND * volatile current_command;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000065
Jari Aaltof73dda02001-11-13 17:56:06 +000066 USE_VAR(current_command);
67
Jari Aalto31859422009-01-12 13:36:28 +000068 current_command = (COMMAND *)NULL;
69
Jari Aaltoccc6cda1996-12-23 17:02:34 +000070 our_indirection_level = ++indirection_level;
71
Chet Rameyd233b482019-01-07 09:27:52 -050072 if (just_one_command)
73 reset_readahead_token ();
74
Jari Aaltoccc6cda1996-12-23 17:02:34 +000075 while (EOF_Reached == 0)
76 {
77 int code;
78
Chet Rameyac50fba2014-02-26 09:36:43 -050079 code = setjmp_nosigs (top_level);
Jari Aaltoccc6cda1996-12-23 17:02:34 +000080
81#if defined (PROCESS_SUBSTITUTION)
82 unlink_fifo_list ();
83#endif /* PROCESS_SUBSTITUTION */
84
Chet Rameya0c0a002016-09-15 16:59:08 -040085 /* XXX - why do we set this every time through the loop? And why do
86 it if SIGINT is trapped in an interactive shell? */
Chet Rameyd233b482019-01-07 09:27:52 -050087 if (interactive_shell && signal_is_ignored (SIGINT) == 0 && signal_is_trapped (SIGINT) == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +000088 set_signal_handler (SIGINT, sigint_sighandler);
89
90 if (code != NOT_JUMPED)
91 {
92 indirection_level = our_indirection_level;
93
94 switch (code)
95 {
Chet Rameyac50fba2014-02-26 09:36:43 -050096 /* Some kind of throw to top_level has occurred. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +000097 case FORCE_EOF:
Jari Aaltob80f6442004-07-27 13:29:18 +000098 case ERREXIT:
Jari Aaltoccc6cda1996-12-23 17:02:34 +000099 case EXITPROG:
100 current_command = (COMMAND *)NULL;
Jari Aaltobb706242000-03-17 21:46:59 +0000101 if (exit_immediately_on_error)
102 variable_context = 0; /* not in a function */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000103 EOF_Reached = EOF;
104 goto exec_done;
105
106 case DISCARD:
Jari Aalto31859422009-01-12 13:36:28 +0000107 /* 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;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000112 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 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500124#if defined (HAVE_POSIX_SIGNALS)
125 sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
126#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000127 break;
128
129 default:
Jari Aaltob72432f1999-02-19 17:11:39 +0000130 command_error ("reader_loop", CMDERR_BADJUMP, code, 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000131 }
132 }
133
134 executing = 0;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000135 if (temporary_env)
136 dispose_used_env_vars ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000137
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;
Chet Rameya0c0a002016-09-15 16:59:08 -0400154
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
Chet Rameyd233b482019-01-07 09:27:52 -0500170 current_command_number++;
171
172 executing = 1;
173 stdin_redir = 0;
174
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000175 execute_command (current_command);
176
177 exec_done:
Jari Aalto95732b42005-12-07 14:08:12 +0000178 QUIT;
179
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000180 if (current_command)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000181 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000182 dispose_command (current_command);
183 current_command = (COMMAND *)NULL;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000184 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000185 }
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
Chet Rameyd233b482019-01-07 09:27:52 -0500200/* Pretty print shell scripts */
201int
202pretty_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
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000241static sighandler
242alrm_catcher(i)
243 int i;
244{
Jari Aaltob80f6442004-07-27 13:29:18 +0000245 printf (_("\007timed out waiting for input: auto-logout\n"));
Jari Aalto31859422009-01-12 13:36:28 +0000246 fflush (stdout);
Jari Aaltob80f6442004-07-27 13:29:18 +0000247 bash_logout (); /* run ~/.bash_logout if this is a login shell */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000248 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. */
254static void
255send_pwd_to_eterm ()
256{
Chet Rameyac50fba2014-02-26 09:36:43 -0500257 char *pwd, *f;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000258
Chet Rameyac50fba2014-02-26 09:36:43 -0500259 f = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000260 pwd = get_string_value ("PWD");
261 if (pwd == 0)
Chet Rameyac50fba2014-02-26 09:36:43 -0500262 f = pwd = get_working_directory ("eterm");
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000263 fprintf (stderr, "\032/%s\n", pwd);
Chet Rameyac50fba2014-02-26 09:36:43 -0500264 free (f);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000265}
266
Chet Rameyd233b482019-01-07 09:27:52 -0500267static void
268execute_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}
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000276/* 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. */
280int
281parse_command ()
282{
283 int r;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000284
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. */
Chet Rameya0c0a002016-09-15 16:59:08 -0400291 /* 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)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000295 {
Chet Rameyd233b482019-01-07 09:27:52 -0500296 execute_prompt_command ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000297
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. */
314int
315read_command ()
316{
317 SHELL_VAR *tmout_var;
318 int tmout_len, result;
319 SigHandler *old_alrm;
320
Jari Aaltobb706242000-03-17 21:46:59 +0000321 set_current_prompt_level (1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000322 global_command = (COMMAND *)NULL;
323
324 /* Only do timeouts if interactive. */
325 tmout_var = (SHELL_VAR *)NULL;
326 tmout_len = 0;
Jari Aaltof73dda02001-11-13 17:56:06 +0000327 old_alrm = (SigHandler *)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000328
329 if (interactive)
330 {
331 tmout_var = find_variable ("TMOUT");
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000332
Jari Aalto7117c2d2002-07-17 14:10:11 +0000333 if (tmout_var && var_isset (tmout_var))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000334 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000335 tmout_len = atoi (value_cell (tmout_var));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000336 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}