blob: 1f65aacacba9488be784f446b6a3daad092c783d [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
Jari Aaltob80f6442004-07-27 13:29:18 +000033#include "bashintl.h"
34
Jari Aaltoccc6cda1996-12-23 17:02:34 +000035#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
Jari Aaltob72432f1999-02-19 17:11:39 +000044#if defined (HISTORY)
45# include "bashhist.h"
46#endif
47
Jari Aaltoccc6cda1996-12-23 17:02:34 +000048extern int EOF_reached;
Jari Aaltof73dda02001-11-13 17:56:06 +000049extern int indirection_level;
Jari Aaltobb706242000-03-17 21:46:59 +000050extern int posixly_correct;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000051extern int subshell_environment, running_under_emacs;
Jari Aaltod166f041997-06-05 14:59:13 +000052extern int last_command_exit_value, stdin_redir;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000053extern int need_here_doc;
54extern int current_command_number, current_command_line_count, line_number;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000055extern int expand_aliases;
56
Chet Rameyac50fba2014-02-26 09:36:43 -050057#if defined (HAVE_POSIX_SIGNALS)
58extern sigset_t top_level_mask;
59#endif
60
Jari Aalto7117c2d2002-07-17 14:10:11 +000061static void send_pwd_to_eterm __P((void));
62static sighandler alrm_catcher __P((int));
63
Jari Aaltoccc6cda1996-12-23 17:02:34 +000064/* Read and execute commands until EOF is reached. This assumes that
65 the input source has already been initialized. */
66int
67reader_loop ()
68{
69 int our_indirection_level;
Jari Aalto95732b42005-12-07 14:08:12 +000070 COMMAND * volatile current_command;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000071
Jari Aaltof73dda02001-11-13 17:56:06 +000072 USE_VAR(current_command);
73
Jari Aalto31859422009-01-12 13:36:28 +000074 current_command = (COMMAND *)NULL;
75
Jari Aaltoccc6cda1996-12-23 17:02:34 +000076 our_indirection_level = ++indirection_level;
77
78 while (EOF_Reached == 0)
79 {
80 int code;
81
Chet Rameyac50fba2014-02-26 09:36:43 -050082 code = setjmp_nosigs (top_level);
Jari Aaltoccc6cda1996-12-23 17:02:34 +000083
84#if defined (PROCESS_SUBSTITUTION)
85 unlink_fifo_list ();
86#endif /* PROCESS_SUBSTITUTION */
87
Chet Rameyac50fba2014-02-26 09:36:43 -050088 /* XXX - why do we set this every time through the loop? */
Jari Aaltoccc6cda1996-12-23 17:02:34 +000089 if (interactive_shell && signal_is_ignored (SIGINT) == 0)
90 set_signal_handler (SIGINT, sigint_sighandler);
91
92 if (code != NOT_JUMPED)
93 {
94 indirection_level = our_indirection_level;
95
96 switch (code)
97 {
Chet Rameyac50fba2014-02-26 09:36:43 -050098 /* Some kind of throw to top_level has occurred. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +000099 case FORCE_EOF:
Jari Aaltob80f6442004-07-27 13:29:18 +0000100 case ERREXIT:
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000101 case EXITPROG:
102 current_command = (COMMAND *)NULL;
Jari Aaltobb706242000-03-17 21:46:59 +0000103 if (exit_immediately_on_error)
104 variable_context = 0; /* not in a function */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000105 EOF_Reached = EOF;
106 goto exec_done;
107
108 case DISCARD:
Jari Aalto31859422009-01-12 13:36:28 +0000109 /* Make sure the exit status is reset to a non-zero value, but
110 leave existing non-zero values (e.g., > 128 on signal)
111 alone. */
112 if (last_command_exit_value == 0)
113 last_command_exit_value = EXECUTION_FAILURE;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000114 if (subshell_environment)
115 {
116 current_command = (COMMAND *)NULL;
117 EOF_Reached = EOF;
118 goto exec_done;
119 }
120 /* Obstack free command elements, etc. */
121 if (current_command)
122 {
123 dispose_command (current_command);
124 current_command = (COMMAND *)NULL;
125 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500126#if defined (HAVE_POSIX_SIGNALS)
127 sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
128#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000129 break;
130
131 default:
Jari Aaltob72432f1999-02-19 17:11:39 +0000132 command_error ("reader_loop", CMDERR_BADJUMP, code, 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000133 }
134 }
135
136 executing = 0;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000137 if (temporary_env)
138 dispose_used_env_vars ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000139
140#if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
141 /* Attempt to reclaim memory allocated with alloca (). */
142 (void) alloca (0);
143#endif
144
145 if (read_command () == 0)
146 {
147 if (interactive_shell == 0 && read_but_dont_execute)
148 {
149 last_command_exit_value = EXECUTION_SUCCESS;
150 dispose_command (global_command);
151 global_command = (COMMAND *)NULL;
152 }
153 else if (current_command = global_command)
154 {
155 global_command = (COMMAND *)NULL;
156 current_command_number++;
157
158 executing = 1;
Jari Aaltod166f041997-06-05 14:59:13 +0000159 stdin_redir = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000160 execute_command (current_command);
161
162 exec_done:
Jari Aalto95732b42005-12-07 14:08:12 +0000163 QUIT;
164
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000165 if (current_command)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000166 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000167 dispose_command (current_command);
168 current_command = (COMMAND *)NULL;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000169 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000170 }
171 }
172 else
173 {
174 /* Parse error, maybe discard rest of stream if not interactive. */
175 if (interactive == 0)
176 EOF_Reached = EOF;
177 }
178 if (just_one_command)
179 EOF_Reached = EOF;
180 }
181 indirection_level--;
182 return (last_command_exit_value);
183}
184
185static sighandler
186alrm_catcher(i)
187 int i;
188{
Jari Aaltob80f6442004-07-27 13:29:18 +0000189 printf (_("\007timed out waiting for input: auto-logout\n"));
Jari Aalto31859422009-01-12 13:36:28 +0000190 fflush (stdout);
Jari Aaltob80f6442004-07-27 13:29:18 +0000191 bash_logout (); /* run ~/.bash_logout if this is a login shell */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000192 jump_to_top_level (EXITPROG);
193 SIGRETURN (0);
194}
195
196/* Send an escape sequence to emacs term mode to tell it the
197 current working directory. */
198static void
199send_pwd_to_eterm ()
200{
Chet Rameyac50fba2014-02-26 09:36:43 -0500201 char *pwd, *f;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000202
Chet Rameyac50fba2014-02-26 09:36:43 -0500203 f = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000204 pwd = get_string_value ("PWD");
205 if (pwd == 0)
Chet Rameyac50fba2014-02-26 09:36:43 -0500206 f = pwd = get_working_directory ("eterm");
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000207 fprintf (stderr, "\032/%s\n", pwd);
Chet Rameyac50fba2014-02-26 09:36:43 -0500208 free (f);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000209}
210
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. */
215int
216parse_command ()
217{
218 int r;
219 char *command_to_execute;
220
221 need_here_doc = 0;
222 run_pending_traps ();
223
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)
228 {
229 command_to_execute = get_string_value ("PROMPT_COMMAND");
230 if (command_to_execute)
Jari Aalto06285672006-10-10 14:15:34 +0000231 execute_variable_command (command_to_execute, "PROMPT_COMMAND");
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000232
233 if (running_under_emacs == 2)
234 send_pwd_to_eterm (); /* Yuck */
235 }
236
237 current_command_line_count = 0;
238 r = yyparse ();
239
240 if (need_here_doc)
241 gather_here_documents ();
242
243 return (r);
244}
245
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. */
249int
250read_command ()
251{
252 SHELL_VAR *tmout_var;
253 int tmout_len, result;
254 SigHandler *old_alrm;
255
Jari Aaltobb706242000-03-17 21:46:59 +0000256 set_current_prompt_level (1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000257 global_command = (COMMAND *)NULL;
258
259 /* Only do timeouts if interactive. */
260 tmout_var = (SHELL_VAR *)NULL;
261 tmout_len = 0;
Jari Aaltof73dda02001-11-13 17:56:06 +0000262 old_alrm = (SigHandler *)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000263
264 if (interactive)
265 {
266 tmout_var = find_variable ("TMOUT");
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000267
Jari Aalto7117c2d2002-07-17 14:10:11 +0000268 if (tmout_var && var_isset (tmout_var))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000269 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000270 tmout_len = atoi (value_cell (tmout_var));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000271 if (tmout_len > 0)
272 {
273 old_alrm = set_signal_handler (SIGALRM, alrm_catcher);
274 alarm (tmout_len);
275 }
276 }
277 }
278
279 QUIT;
280
281 current_command_line_count = 0;
282 result = parse_command ();
283
284 if (interactive && tmout_var && (tmout_len > 0))
285 {
286 alarm(0);
287 set_signal_handler (SIGALRM, old_alrm);
288 }
289
290 return (result);
291}