]>
Commit | Line | Data |
---|---|---|
bb70624e | 1 | /* eval.c -- reading and evaluating commands. */ |
ccc6cda3 | 2 | |
74091dd4 | 3 | /* Copyright (C) 1996-2022 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 | ||
8868edaf CR |
51 | static void send_pwd_to_eterm PARAMS((void)); |
52 | static 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. */ | |
56 | int | |
57 | reader_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. */ |
b80f6443 | 93 | case ERREXIT: |
74091dd4 CR |
94 | if (exit_immediately_on_error) |
95 | reset_local_contexts (); /* not in a function */ | |
96 | case FORCE_EOF: | |
ccc6cda3 | 97 | case EXITPROG: |
74091dd4 | 98 | case EXITBLTIN: |
ccc6cda3 JA |
99 | current_command = (COMMAND *)NULL; |
100 | EOF_Reached = EOF; | |
101 | goto exec_done; | |
102 | ||
103 | case DISCARD: | |
3185942a JA |
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) | |
8868edaf | 108 | set_exit_status (EXECUTION_FAILURE); |
ccc6cda3 JA |
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 | } | |
8868edaf CR |
121 | |
122 | restore_sigmask (); | |
ccc6cda3 JA |
123 | break; |
124 | ||
125 | default: | |
b72432fd | 126 | command_error ("reader_loop", CMDERR_BADJUMP, code, 0); |
ccc6cda3 JA |
127 | } |
128 | } | |
129 | ||
130 | executing = 0; | |
7117c2d2 JA |
131 | if (temporary_env) |
132 | dispose_used_env_vars (); | |
ccc6cda3 JA |
133 | |
134 | #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA) | |
135 | /* Attempt to reclaim memory allocated with alloca (). */ | |
136 | (void) alloca (0); | |
137 | #endif | |
138 | ||
139 | if (read_command () == 0) | |
140 | { | |
141 | if (interactive_shell == 0 && read_but_dont_execute) | |
142 | { | |
74091dd4 | 143 | set_exit_status (last_command_exit_value); |
ccc6cda3 JA |
144 | dispose_command (global_command); |
145 | global_command = (COMMAND *)NULL; | |
146 | } | |
147 | else if (current_command = global_command) | |
148 | { | |
149 | global_command = (COMMAND *)NULL; | |
a0c0a00f CR |
150 | |
151 | /* If the shell is interactive, expand and display $PS0 after reading a | |
152 | command (possibly a list or pipeline) and before executing it. */ | |
153 | if (interactive && ps0_prompt) | |
154 | { | |
155 | char *ps0_string; | |
156 | ||
157 | ps0_string = decode_prompt_string (ps0_prompt); | |
158 | if (ps0_string && *ps0_string) | |
159 | { | |
160 | fprintf (stderr, "%s", ps0_string); | |
161 | fflush (stderr); | |
162 | } | |
163 | free (ps0_string); | |
164 | } | |
165 | ||
d233b485 CR |
166 | current_command_number++; |
167 | ||
168 | executing = 1; | |
169 | stdin_redir = 0; | |
170 | ||
ccc6cda3 JA |
171 | execute_command (current_command); |
172 | ||
173 | exec_done: | |
95732b49 JA |
174 | QUIT; |
175 | ||
ccc6cda3 | 176 | if (current_command) |
28ef6c31 | 177 | { |
ccc6cda3 JA |
178 | dispose_command (current_command); |
179 | current_command = (COMMAND *)NULL; | |
28ef6c31 | 180 | } |
ccc6cda3 JA |
181 | } |
182 | } | |
183 | else | |
184 | { | |
185 | /* Parse error, maybe discard rest of stream if not interactive. */ | |
186 | if (interactive == 0) | |
187 | EOF_Reached = EOF; | |
188 | } | |
189 | if (just_one_command) | |
190 | EOF_Reached = EOF; | |
191 | } | |
192 | indirection_level--; | |
193 | return (last_command_exit_value); | |
194 | } | |
195 | ||
d233b485 CR |
196 | /* Pretty print shell scripts */ |
197 | int | |
198 | pretty_print_loop () | |
199 | { | |
200 | COMMAND *current_command; | |
201 | char *command_to_print; | |
202 | int code; | |
203 | int global_posix_mode, last_was_newline; | |
204 | ||
205 | global_posix_mode = posixly_correct; | |
206 | last_was_newline = 0; | |
207 | while (EOF_Reached == 0) | |
208 | { | |
209 | code = setjmp_nosigs (top_level); | |
210 | if (code) | |
211 | return (EXECUTION_FAILURE); | |
212 | if (read_command() == 0) | |
213 | { | |
214 | current_command = global_command; | |
215 | global_command = 0; | |
216 | posixly_correct = 1; /* print posix-conformant */ | |
217 | if (current_command && (command_to_print = make_command_string (current_command))) | |
218 | { | |
219 | printf ("%s\n", command_to_print); /* for now */ | |
220 | last_was_newline = 0; | |
221 | } | |
222 | else if (last_was_newline == 0) | |
223 | { | |
224 | printf ("\n"); | |
225 | last_was_newline = 1; | |
226 | } | |
227 | posixly_correct = global_posix_mode; | |
228 | dispose_command (current_command); | |
229 | } | |
230 | else | |
231 | return (EXECUTION_FAILURE); | |
232 | } | |
233 | ||
234 | return (EXECUTION_SUCCESS); | |
235 | } | |
236 | ||
ccc6cda3 JA |
237 | static sighandler |
238 | alrm_catcher(i) | |
239 | int i; | |
240 | { | |
8868edaf CR |
241 | char *msg; |
242 | ||
243 | msg = _("\007timed out waiting for input: auto-logout\n"); | |
244 | write (1, msg, strlen (msg)); | |
245 | ||
b80f6443 | 246 | bash_logout (); /* run ~/.bash_logout if this is a login shell */ |
ccc6cda3 JA |
247 | jump_to_top_level (EXITPROG); |
248 | SIGRETURN (0); | |
249 | } | |
250 | ||
251 | /* Send an escape sequence to emacs term mode to tell it the | |
252 | current working directory. */ | |
253 | static void | |
254 | send_pwd_to_eterm () | |
255 | { | |
ac50fbac | 256 | char *pwd, *f; |
ccc6cda3 | 257 | |
ac50fbac | 258 | f = 0; |
ccc6cda3 JA |
259 | pwd = get_string_value ("PWD"); |
260 | if (pwd == 0) | |
ac50fbac | 261 | f = pwd = get_working_directory ("eterm"); |
ccc6cda3 | 262 | fprintf (stderr, "\032/%s\n", pwd); |
ac50fbac | 263 | free (f); |
ccc6cda3 JA |
264 | } |
265 | ||
8868edaf CR |
266 | #if defined (ARRAY_VARS) |
267 | /* Caller ensures that A has a non-zero number of elements */ | |
268 | int | |
269 | execute_array_command (a, v) | |
270 | ARRAY *a; | |
271 | void *v; | |
272 | { | |
273 | char *tag; | |
274 | char **argv; | |
275 | int argc, i; | |
276 | ||
277 | tag = (char *)v; | |
278 | argc = 0; | |
279 | argv = array_to_argv (a, &argc); | |
280 | for (i = 0; i < argc; i++) | |
281 | { | |
282 | if (argv[i] && argv[i][0]) | |
283 | execute_variable_command (argv[i], tag); | |
284 | } | |
285 | strvec_dispose (argv); | |
286 | return 0; | |
287 | } | |
288 | #endif | |
289 | ||
d233b485 CR |
290 | static void |
291 | execute_prompt_command () | |
292 | { | |
293 | char *command_to_execute; | |
8868edaf CR |
294 | SHELL_VAR *pcv; |
295 | #if defined (ARRAY_VARS) | |
296 | ARRAY *pcmds; | |
297 | #endif | |
d233b485 | 298 | |
8868edaf CR |
299 | pcv = find_variable ("PROMPT_COMMAND"); |
300 | if (pcv == 0 || var_isset (pcv) == 0 || invisible_p (pcv)) | |
301 | return; | |
302 | #if defined (ARRAY_VARS) | |
303 | if (array_p (pcv)) | |
304 | { | |
305 | if ((pcmds = array_cell (pcv)) && array_num_elements (pcmds) > 0) | |
306 | execute_array_command (pcmds, "PROMPT_COMMAND"); | |
307 | return; | |
308 | } | |
309 | else if (assoc_p (pcv)) | |
310 | return; /* currently don't allow associative arrays here */ | |
311 | #endif | |
312 | ||
313 | command_to_execute = value_cell (pcv); | |
314 | if (command_to_execute && *command_to_execute) | |
d233b485 CR |
315 | execute_variable_command (command_to_execute, "PROMPT_COMMAND"); |
316 | } | |
8868edaf | 317 | |
ccc6cda3 JA |
318 | /* Call the YACC-generated parser and return the status of the parse. |
319 | Input is read from the current input stream (bash_input). yyparse | |
320 | leaves the parsed command in the global variable GLOBAL_COMMAND. | |
321 | This is where PROMPT_COMMAND is executed. */ | |
322 | int | |
323 | parse_command () | |
324 | { | |
325 | int r; | |
ccc6cda3 JA |
326 | |
327 | need_here_doc = 0; | |
328 | run_pending_traps (); | |
329 | ||
330 | /* Allow the execution of a random command just before the printing | |
331 | of each primary prompt. If the shell variable PROMPT_COMMAND | |
8868edaf | 332 | is set then its value (array or string) is the command(s) to execute. */ |
a0c0a00f CR |
333 | /* The tests are a combination of SHOULD_PROMPT() and prompt_again() |
334 | from parse.y, which are the conditions under which the prompt is | |
335 | actually printed. */ | |
336 | if (interactive && bash_input.type != st_string && parser_expanding_alias() == 0) | |
ccc6cda3 | 337 | { |
8868edaf CR |
338 | #if defined (READLINE) |
339 | if (no_line_editing || (bash_input.type == st_stdin && parser_will_prompt ())) | |
340 | #endif | |
341 | execute_prompt_command (); | |
ccc6cda3 JA |
342 | |
343 | if (running_under_emacs == 2) | |
344 | send_pwd_to_eterm (); /* Yuck */ | |
345 | } | |
346 | ||
347 | current_command_line_count = 0; | |
348 | r = yyparse (); | |
349 | ||
350 | if (need_here_doc) | |
351 | gather_here_documents (); | |
352 | ||
353 | return (r); | |
354 | } | |
355 | ||
356 | /* Read and parse a command, returning the status of the parse. The command | |
357 | is left in the globval variable GLOBAL_COMMAND for use by reader_loop. | |
358 | This is where the shell timeout code is executed. */ | |
359 | int | |
360 | read_command () | |
361 | { | |
362 | SHELL_VAR *tmout_var; | |
363 | int tmout_len, result; | |
364 | SigHandler *old_alrm; | |
365 | ||
bb70624e | 366 | set_current_prompt_level (1); |
ccc6cda3 JA |
367 | global_command = (COMMAND *)NULL; |
368 | ||
369 | /* Only do timeouts if interactive. */ | |
370 | tmout_var = (SHELL_VAR *)NULL; | |
371 | tmout_len = 0; | |
f73dda09 | 372 | old_alrm = (SigHandler *)NULL; |
ccc6cda3 JA |
373 | |
374 | if (interactive) | |
375 | { | |
376 | tmout_var = find_variable ("TMOUT"); | |
ccc6cda3 | 377 | |
7117c2d2 | 378 | if (tmout_var && var_isset (tmout_var)) |
ccc6cda3 | 379 | { |
7117c2d2 | 380 | tmout_len = atoi (value_cell (tmout_var)); |
ccc6cda3 JA |
381 | if (tmout_len > 0) |
382 | { | |
383 | old_alrm = set_signal_handler (SIGALRM, alrm_catcher); | |
384 | alarm (tmout_len); | |
385 | } | |
386 | } | |
387 | } | |
388 | ||
389 | QUIT; | |
390 | ||
391 | current_command_line_count = 0; | |
392 | result = parse_command (); | |
393 | ||
394 | if (interactive && tmout_var && (tmout_len > 0)) | |
395 | { | |
396 | alarm(0); | |
397 | set_signal_handler (SIGALRM, old_alrm); | |
398 | } | |
399 | ||
400 | return (result); | |
401 | } |