]> git.ipfire.org Git - thirdparty/bash.git/blame - eval.c
bash-5.1 beta release
[thirdparty/bash.git] / eval.c
CommitLineData
bb70624e 1/* eval.c -- reading and evaluating commands. */
ccc6cda3 2
712f80b0 3/* Copyright (C) 1996-2020 Free Software Foundation, Inc.
ccc6cda3 4
bb70624e 5 This file is part of GNU Bash, the Bourne Again SHell.
ccc6cda3 6
3185942a
JA
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.
ccc6cda3 11
3185942a
JA
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.
ccc6cda3 16
bb70624e 17 You should have received a copy of the GNU General Public License
3185942a
JA
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
ccc6cda3
JA
20
21#include "config.h"
22
23#if defined (HAVE_UNISTD_H)
cce855bc
JA
24# ifdef _MINIX
25# include <sys/types.h>
26# endif
ccc6cda3
JA
27# include <unistd.h>
28#endif
29
30#include "bashansi.h"
31#include <stdio.h>
32
a0c0a00f
CR
33#include <signal.h>
34
b80f6443
JA
35#include "bashintl.h"
36
ccc6cda3 37#include "shell.h"
d233b485 38#include "parser.h"
ccc6cda3
JA
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
b72432fd
JA
47#if defined (HISTORY)
48# include "bashhist.h"
49#endif
50
712f80b0
CR
51static void send_pwd_to_eterm PARAMS((void));
52static sighandler alrm_catcher PARAMS((int));
7117c2d2 53
ccc6cda3
JA
54/* Read and execute commands until EOF is reached. This assumes that
55 the input source has already been initialized. */
56int
57reader_loop ()
58{
59 int our_indirection_level;
95732b49 60 COMMAND * volatile current_command;
ccc6cda3 61
f73dda09
JA
62 USE_VAR(current_command);
63
3185942a
JA
64 current_command = (COMMAND *)NULL;
65
ccc6cda3
JA
66 our_indirection_level = ++indirection_level;
67
d233b485
CR
68 if (just_one_command)
69 reset_readahead_token ();
70
ccc6cda3
JA
71 while (EOF_Reached == 0)
72 {
73 int code;
74
ac50fbac 75 code = setjmp_nosigs (top_level);
ccc6cda3
JA
76
77#if defined (PROCESS_SUBSTITUTION)
78 unlink_fifo_list ();
79#endif /* PROCESS_SUBSTITUTION */
80
a0c0a00f
CR
81 /* XXX - why do we set this every time through the loop? And why do
82 it if SIGINT is trapped in an interactive shell? */
d233b485 83 if (interactive_shell && signal_is_ignored (SIGINT) == 0 && signal_is_trapped (SIGINT) == 0)
ccc6cda3
JA
84 set_signal_handler (SIGINT, sigint_sighandler);
85
86 if (code != NOT_JUMPED)
87 {
88 indirection_level = our_indirection_level;
89
90 switch (code)
91 {
ac50fbac 92 /* Some kind of throw to top_level has occurred. */
ccc6cda3 93 case FORCE_EOF:
b80f6443 94 case ERREXIT:
ccc6cda3
JA
95 case EXITPROG:
96 current_command = (COMMAND *)NULL;
bb70624e
JA
97 if (exit_immediately_on_error)
98 variable_context = 0; /* not in a function */
ccc6cda3
JA
99 EOF_Reached = EOF;
100 goto exec_done;
101
102 case DISCARD:
3185942a
JA
103 /* Make sure the exit status is reset to a non-zero value, but
104 leave existing non-zero values (e.g., > 128 on signal)
105 alone. */
106 if (last_command_exit_value == 0)
712f80b0 107 set_exit_status (EXECUTION_FAILURE);
ccc6cda3
JA
108 if (subshell_environment)
109 {
110 current_command = (COMMAND *)NULL;
111 EOF_Reached = EOF;
112 goto exec_done;
113 }
114 /* Obstack free command elements, etc. */
115 if (current_command)
116 {
117 dispose_command (current_command);
118 current_command = (COMMAND *)NULL;
119 }
712f80b0
CR
120
121 restore_sigmask ();
ccc6cda3
JA
122 break;
123
124 default:
b72432fd 125 command_error ("reader_loop", CMDERR_BADJUMP, code, 0);
ccc6cda3
JA
126 }
127 }
128
129 executing = 0;
7117c2d2
JA
130 if (temporary_env)
131 dispose_used_env_vars ();
ccc6cda3
JA
132
133#if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
134 /* Attempt to reclaim memory allocated with alloca (). */
135 (void) alloca (0);
136#endif
137
138 if (read_command () == 0)
139 {
140 if (interactive_shell == 0 && read_but_dont_execute)
141 {
712f80b0 142 set_exit_status (EXECUTION_SUCCESS);
ccc6cda3
JA
143 dispose_command (global_command);
144 global_command = (COMMAND *)NULL;
145 }
146 else if (current_command = global_command)
147 {
148 global_command = (COMMAND *)NULL;
a0c0a00f
CR
149
150 /* If the shell is interactive, expand and display $PS0 after reading a
151 command (possibly a list or pipeline) and before executing it. */
152 if (interactive && ps0_prompt)
153 {
154 char *ps0_string;
155
156 ps0_string = decode_prompt_string (ps0_prompt);
157 if (ps0_string && *ps0_string)
158 {
159 fprintf (stderr, "%s", ps0_string);
160 fflush (stderr);
161 }
162 free (ps0_string);
163 }
164
d233b485
CR
165 current_command_number++;
166
167 executing = 1;
168 stdin_redir = 0;
169
ccc6cda3
JA
170 execute_command (current_command);
171
172 exec_done:
95732b49
JA
173 QUIT;
174
ccc6cda3 175 if (current_command)
28ef6c31 176 {
ccc6cda3
JA
177 dispose_command (current_command);
178 current_command = (COMMAND *)NULL;
28ef6c31 179 }
ccc6cda3
JA
180 }
181 }
182 else
183 {
184 /* Parse error, maybe discard rest of stream if not interactive. */
185 if (interactive == 0)
186 EOF_Reached = EOF;
187 }
188 if (just_one_command)
189 EOF_Reached = EOF;
190 }
191 indirection_level--;
192 return (last_command_exit_value);
193}
194
d233b485
CR
195/* Pretty print shell scripts */
196int
197pretty_print_loop ()
198{
199 COMMAND *current_command;
200 char *command_to_print;
201 int code;
202 int global_posix_mode, last_was_newline;
203
204 global_posix_mode = posixly_correct;
205 last_was_newline = 0;
206 while (EOF_Reached == 0)
207 {
208 code = setjmp_nosigs (top_level);
209 if (code)
210 return (EXECUTION_FAILURE);
211 if (read_command() == 0)
212 {
213 current_command = global_command;
214 global_command = 0;
215 posixly_correct = 1; /* print posix-conformant */
216 if (current_command && (command_to_print = make_command_string (current_command)))
217 {
218 printf ("%s\n", command_to_print); /* for now */
219 last_was_newline = 0;
220 }
221 else if (last_was_newline == 0)
222 {
223 printf ("\n");
224 last_was_newline = 1;
225 }
226 posixly_correct = global_posix_mode;
227 dispose_command (current_command);
228 }
229 else
230 return (EXECUTION_FAILURE);
231 }
232
233 return (EXECUTION_SUCCESS);
234}
235
ccc6cda3
JA
236static sighandler
237alrm_catcher(i)
238 int i;
239{
3eb0018e
CR
240 char *msg;
241
242 msg = _("\007timed out waiting for input: auto-logout\n");
243 write (1, msg, strlen (msg));
244
b80f6443 245 bash_logout (); /* run ~/.bash_logout if this is a login shell */
ccc6cda3
JA
246 jump_to_top_level (EXITPROG);
247 SIGRETURN (0);
248}
249
250/* Send an escape sequence to emacs term mode to tell it the
251 current working directory. */
252static void
253send_pwd_to_eterm ()
254{
ac50fbac 255 char *pwd, *f;
ccc6cda3 256
ac50fbac 257 f = 0;
ccc6cda3
JA
258 pwd = get_string_value ("PWD");
259 if (pwd == 0)
ac50fbac 260 f = pwd = get_working_directory ("eterm");
ccc6cda3 261 fprintf (stderr, "\032/%s\n", pwd);
ac50fbac 262 free (f);
ccc6cda3
JA
263}
264
712f80b0 265#if defined (ARRAY_VARS)
3eb0018e 266/* Caller ensures that A has a non-zero number of elements */
712f80b0 267int
3eb0018e
CR
268execute_array_command (a, v)
269 ARRAY *a;
712f80b0
CR
270 void *v;
271{
3eb0018e
CR
272 char *tag;
273 char **argv;
274 int argc, i;
712f80b0
CR
275
276 tag = (char *)v;
3eb0018e
CR
277 argc = 0;
278 argv = array_to_argv (a, &argc);
279 for (i = 0; i < argc; i++)
280 {
281 if (argv[i] && argv[i][0])
282 execute_variable_command (argv[i], tag);
283 }
284 strvec_dispose (argv);
712f80b0
CR
285 return 0;
286}
287#endif
288
d233b485
CR
289static void
290execute_prompt_command ()
291{
292 char *command_to_execute;
712f80b0 293 SHELL_VAR *pcv;
3eb0018e 294#if defined (ARRAY_VARS)
712f80b0 295 ARRAY *pcmds;
3eb0018e 296#endif
712f80b0 297
3eb0018e
CR
298 pcv = find_variable ("PROMPT_COMMAND");
299 if (pcv == 0 || var_isset (pcv) == 0 || invisible_p (pcv))
300 return;
301#if defined (ARRAY_VARS)
302 if (array_p (pcv))
712f80b0 303 {
3eb0018e
CR
304 if ((pcmds = array_cell (pcv)) && array_num_elements (pcmds) > 0)
305 execute_array_command (pcmds, "PROMPT_COMMAND");
712f80b0
CR
306 return;
307 }
3eb0018e
CR
308 else if (assoc_p (pcv))
309 return; /* currently don't allow associative arrays here */
712f80b0 310#endif
d233b485 311
3eb0018e 312 command_to_execute = value_cell (pcv);
712f80b0 313 if (command_to_execute && *command_to_execute)
d233b485
CR
314 execute_variable_command (command_to_execute, "PROMPT_COMMAND");
315}
712f80b0 316
ccc6cda3
JA
317/* Call the YACC-generated parser and return the status of the parse.
318 Input is read from the current input stream (bash_input). yyparse
319 leaves the parsed command in the global variable GLOBAL_COMMAND.
320 This is where PROMPT_COMMAND is executed. */
321int
322parse_command ()
323{
324 int r;
ccc6cda3
JA
325
326 need_here_doc = 0;
327 run_pending_traps ();
328
329 /* Allow the execution of a random command just before the printing
330 of each primary prompt. If the shell variable PROMPT_COMMAND
3eb0018e 331 is set then its value (array or string) is the command(s) to execute. */
a0c0a00f
CR
332 /* The tests are a combination of SHOULD_PROMPT() and prompt_again()
333 from parse.y, which are the conditions under which the prompt is
334 actually printed. */
335 if (interactive && bash_input.type != st_string && parser_expanding_alias() == 0)
ccc6cda3 336 {
712f80b0
CR
337#if defined (READLINE)
338 if (no_line_editing || (bash_input.type == st_stdin && parser_will_prompt ()))
339#endif
340 execute_prompt_command ();
ccc6cda3
JA
341
342 if (running_under_emacs == 2)
343 send_pwd_to_eterm (); /* Yuck */
344 }
345
346 current_command_line_count = 0;
347 r = yyparse ();
348
349 if (need_here_doc)
350 gather_here_documents ();
351
352 return (r);
353}
354
355/* Read and parse a command, returning the status of the parse. The command
356 is left in the globval variable GLOBAL_COMMAND for use by reader_loop.
357 This is where the shell timeout code is executed. */
358int
359read_command ()
360{
361 SHELL_VAR *tmout_var;
362 int tmout_len, result;
363 SigHandler *old_alrm;
364
bb70624e 365 set_current_prompt_level (1);
ccc6cda3
JA
366 global_command = (COMMAND *)NULL;
367
368 /* Only do timeouts if interactive. */
369 tmout_var = (SHELL_VAR *)NULL;
370 tmout_len = 0;
f73dda09 371 old_alrm = (SigHandler *)NULL;
ccc6cda3
JA
372
373 if (interactive)
374 {
375 tmout_var = find_variable ("TMOUT");
ccc6cda3 376
7117c2d2 377 if (tmout_var && var_isset (tmout_var))
ccc6cda3 378 {
7117c2d2 379 tmout_len = atoi (value_cell (tmout_var));
ccc6cda3
JA
380 if (tmout_len > 0)
381 {
382 old_alrm = set_signal_handler (SIGALRM, alrm_catcher);
383 alarm (tmout_len);
384 }
385 }
386 }
387
388 QUIT;
389
390 current_command_line_count = 0;
391 result = parse_command ();
392
393 if (interactive && tmout_var && (tmout_len > 0))
394 {
395 alarm(0);
396 set_signal_handler (SIGALRM, old_alrm);
397 }
398
399 return (result);
400}