improve of cmpl.
[bush.git] / src / runner / eval.c
blob26338ca148053527d410f4e6213578de6a2a3e64
1 /* eval.c -- reading and evaluating commands. */
3 /* Copyright (C) 1996-2020 Free Software Foundation, Inc.
5 This file is part of GNU Bush, the Bourne Again SHell.
7 Bush 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.
12 Bush 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.
17 You should have received a copy of the GNU General Public License
18 along with Bush. If not, see <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #if defined (HAVE_UNISTD_H)
24 # ifdef _MINIX
25 # include <sys/types.h>
26 # endif
27 # include <unistd.h>
28 #endif
30 #include "bushansi.h"
31 #include <stdio.h>
33 #include <signal.h>
35 #include "bushintl.h"
37 #include "shell.h"
38 #include "lxrgmr/parser.h"
39 #include "flags.h"
40 #include "trap.h"
42 #include "builtins/common.h"
44 #include "input/input.h"
45 #include "runner/execute_cmd.h"
47 #if defined (HISTORY)
48 # include "bushhist.h"
49 #endif
51 static void send_pwd_to_eterm PARAMS((void));
52 static sighandler alrm_catcher PARAMS((int));
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 ()
59 int our_indirection_level;
60 COMMAND * volatile current_command;
62 USE_VAR(current_command);
64 current_command = (COMMAND *)NULL;
66 our_indirection_level = ++indirection_level;
68 if (just_one_command)
69 reset_readahead_token ();
71 while (EOF_Reached == 0)
73 int code;
75 code = setjmp_nosigs (top_level);
77 #if defined (PROCESS_SUBSTITUTION)
78 unlink_fifo_list ();
79 #endif /* PROCESS_SUBSTITUTION */
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? */
83 if (interactive_shell && signal_is_ignored (SIGINT) == 0 && signal_is_trapped (SIGINT) == 0)
84 set_signal_handler (SIGINT, sigint_sighandler);
86 if (code != NOT_JUMPED)
88 indirection_level = our_indirection_level;
90 switch (code)
92 /* Some kind of throw to top_level has occurred. */
93 case FORCE_EOF:
94 case ERREXIT:
95 case EXITPROG:
96 current_command = (COMMAND *)NULL;
97 if (exit_immediately_on_error)
98 variable_context = 0; /* not in a function */
99 EOF_Reached = EOF;
100 goto exec_done;
102 case DISCARD:
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)
107 set_exit_status (EXECUTION_FAILURE);
108 if (subshell_environment)
110 current_command = (COMMAND *)NULL;
111 EOF_Reached = EOF;
112 goto exec_done;
114 /* Obstack free command elements, etc. */
115 if (current_command)
117 dispose_command (current_command);
118 current_command = (COMMAND *)NULL;
121 restore_sigmask ();
122 break;
124 default:
125 command_error ("reader_loop", CMDERR_BADJUMP, code, 0);
129 executing = 0;
130 if (temporary_env)
131 dispose_used_env_vars ();
133 #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
134 /* Attempt to reclaim memory allocated with alloca (). */
135 (void) alloca (0);
136 #endif
138 if (read_command () == 0)
140 if (interactive_shell == 0 && read_but_dont_execute)
142 set_exit_status (EXECUTION_SUCCESS);
143 dispose_command (global_command);
144 global_command = (COMMAND *)NULL;
146 else if (current_command = global_command)
148 global_command = (COMMAND *)NULL;
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)
154 char *ps0_string;
156 ps0_string = decode_prompt_string (ps0_prompt);
157 if (ps0_string && *ps0_string)
159 fprintf (stderr, "%s", ps0_string);
160 fflush (stderr);
162 free (ps0_string);
165 current_command_number++;
167 executing = 1;
168 stdin_redir = 0;
170 execute_command (current_command);
172 exec_done:
173 QUIT;
175 if (current_command)
177 dispose_command (current_command);
178 current_command = (COMMAND *)NULL;
182 else
184 /* Parse error, maybe discard rest of stream if not interactive. */
185 if (interactive == 0)
186 EOF_Reached = EOF;
188 if (just_one_command)
189 EOF_Reached = EOF;
191 indirection_level--;
192 return (last_command_exit_value);
195 /* Pretty print shell scripts */
197 pretty_print_loop ()
199 COMMAND *current_command;
200 char *command_to_print;
201 int code;
202 int global_posix_mode, last_was_newline;
204 global_posix_mode = posixly_correct;
205 last_was_newline = 0;
206 while (EOF_Reached == 0)
208 code = setjmp_nosigs (top_level);
209 if (code)
210 return (EXECUTION_FAILURE);
211 if (read_command() == 0)
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)))
218 printf ("%s\n", command_to_print); /* for now */
219 last_was_newline = 0;
221 else if (last_was_newline == 0)
223 printf ("\n");
224 last_was_newline = 1;
226 posixly_correct = global_posix_mode;
227 dispose_command (current_command);
229 else
230 return (EXECUTION_FAILURE);
233 return (EXECUTION_SUCCESS);
236 static sighandler
237 alrm_catcher(i)
238 int i;
240 char *msg;
242 msg = _("\007timed out waiting for input: auto-logout\n");
243 if (write (1, msg, strlen (msg)))
244 printf("err on writing.\n");
246 bush_logout (); /* run ~/.bush_logout if this is a login shell */
247 jump_to_top_level (EXITPROG);
248 SIGRETURN (0);
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 ()
256 char *pwd, *f;
258 f = 0;
259 pwd = get_string_value ("PWD");
260 if (pwd == 0)
261 f = pwd = get_working_directory ("eterm");
262 fprintf (stderr, "\032/%s\n", pwd);
263 free (f);
266 #if defined (ARRAY_VARS)
267 /* Caller ensures that A has a non-zero number of elements */
269 execute_array_command (a, v)
270 ARRAY *a;
271 void *v;
273 char *tag;
274 char **argv;
275 int argc, i;
277 tag = (char *)v;
278 argc = 0;
279 argv = array_to_argv (a, &argc);
280 for (i = 0; i < argc; i++)
282 if (argv[i] && argv[i][0])
283 execute_variable_command (argv[i], tag);
285 strvec_dispose (argv);
286 return 0;
288 #endif
290 static void
291 execute_prompt_command ()
293 char *command_to_execute;
294 SHELL_VAR *pcv;
295 #if defined (ARRAY_VARS)
296 ARRAY *pcmds;
297 #endif
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))
305 if ((pcmds = array_cell (pcv)) && array_num_elements (pcmds) > 0)
306 execute_array_command (pcmds, "PROMPT_COMMAND");
307 return;
309 else if (assoc_p (pcv))
310 return; /* currently don't allow associative arrays here */
311 #endif
313 command_to_execute = value_cell (pcv);
314 if (command_to_execute && *command_to_execute)
315 execute_variable_command (command_to_execute, "PROMPT_COMMAND");
318 /* Call the YACC-generated parser and return the status of the parse.
319 Input is read from the current input stream (bush_input). yyparse
320 leaves the parsed command in the global variable GLOBAL_COMMAND.
321 This is where PROMPT_COMMAND is executed. */
323 parse_command ()
325 int r;
327 need_here_doc = 0;
328 run_pending_traps ();
330 /* Allow the execution of a random command just before the printing
331 of each primary prompt. If the shell variable PROMPT_COMMAND
332 is set then its value (array or string) is the command(s) to execute. */
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 && bush_input.type != st_string && parser_expanding_alias() == 0)
338 #if defined (READLINE)
339 if (no_line_editing || (bush_input.type == st_stdin && parser_will_prompt ()))
340 #endif
341 execute_prompt_command ();
343 if (running_under_emacs == 2)
344 send_pwd_to_eterm (); /* Yuck */
347 current_command_line_count = 0;
348 r = yyparse ();
350 if (need_here_doc)
351 gather_here_documents ();
353 return (r);
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. */
360 read_command ()
362 SHELL_VAR *tmout_var;
363 int tmout_len, result;
364 SigHandler *old_alrm;
366 set_current_prompt_level (1);
367 global_command = (COMMAND *)NULL;
369 /* Only do timeouts if interactive. */
370 tmout_var = (SHELL_VAR *)NULL;
371 tmout_len = 0;
372 old_alrm = (SigHandler *)NULL;
374 if (interactive)
376 tmout_var = find_variable ("TMOUT");
378 if (tmout_var && var_isset (tmout_var))
380 tmout_len = atoi (value_cell (tmout_var));
381 if (tmout_len > 0)
383 old_alrm = set_signal_handler (SIGALRM, alrm_catcher);
384 alarm (tmout_len);
389 QUIT;
391 current_command_line_count = 0;
392 result = parse_command ();
394 if (interactive && tmout_var && (tmout_len > 0))
396 alarm(0);
397 set_signal_handler (SIGALRM, old_alrm);
400 return (result);