1 /* eval.c -- reading and evaluating commands.
3 Copyright (C) 1996 Free Software Foundation, Inc.
5 This file is part of GNU Bash.
7 Bash is distributed in the hope that it will be useful, but WITHOUT
8 ANY WARRANTY. No author or distributor accepts responsibility to
9 anyone for the consequences of using it or for whether it serves
10 any particular purpose or works at all, unless he says so in
11 writing. Refer to the GNU Emacs General Public License for full
14 Everyone is granted permission to copy, modify and redistribute
15 Bash, but only under the conditions described in the GNU General
16 Public License. A copy of this license is supposed to have been
17 given to you along with GNU Emacs so you can know your rights and
18 responsibilities. It should be in a file named COPYING.
20 Among other things, the copyright notice and this notice must be
21 preserved on all copies. */
25 #if defined (HAVE_UNISTD_H)
27 # include <sys/types.h>
39 #include "builtins/common.h"
42 #include "execute_cmd.h"
45 # include "bashhist.h"
48 extern int yyparse ();
50 extern int EOF_reached;
51 extern int indirection_level, interactive, interactive_shell;
52 extern int subshell_environment, running_under_emacs;
53 extern int last_command_exit_value, stdin_redir;
54 extern int need_here_doc;
55 extern int current_command_number, current_command_line_count, line_number;
56 extern char *ps1_prompt, **prompt_string_pointer;
57 extern int expand_aliases;
59 /* Read and execute commands until EOF is reached. This assumes that
60 the input source has already been initialized. */
64 int our_indirection_level;
65 COMMAND *current_command = (COMMAND *)NULL;
67 our_indirection_level = ++indirection_level;
69 while (EOF_Reached == 0)
73 code = setjmp (top_level);
75 #if defined (PROCESS_SUBSTITUTION)
77 #endif /* PROCESS_SUBSTITUTION */
79 if (interactive_shell && signal_is_ignored (SIGINT) == 0)
80 set_signal_handler (SIGINT, sigint_sighandler);
82 if (code != NOT_JUMPED)
84 indirection_level = our_indirection_level;
88 /* Some kind of throw to top_level has occured. */
91 current_command = (COMMAND *)NULL;
96 last_command_exit_value = 1;
97 if (subshell_environment)
99 current_command = (COMMAND *)NULL;
103 /* Obstack free command elements, etc. */
106 dispose_command (current_command);
107 current_command = (COMMAND *)NULL;
112 command_error ("reader_loop", CMDERR_BADJUMP, code, 0);
117 dispose_used_env_vars ();
119 #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
120 /* Attempt to reclaim memory allocated with alloca (). */
124 if (read_command () == 0)
126 if (interactive_shell == 0 && read_but_dont_execute)
128 last_command_exit_value = EXECUTION_SUCCESS;
129 dispose_command (global_command);
130 global_command = (COMMAND *)NULL;
132 else if (current_command = global_command)
134 global_command = (COMMAND *)NULL;
135 current_command_number++;
139 execute_command (current_command);
144 dispose_command (current_command);
145 current_command = (COMMAND *)NULL;
153 /* Parse error, maybe discard rest of stream if not interactive. */
154 if (interactive == 0)
157 if (just_one_command)
161 return (last_command_exit_value);
168 printf ("\007timed out waiting for input: auto-logout\n");
169 jump_to_top_level (EXITPROG);
173 /* Send an escape sequence to emacs term mode to tell it the
174 current working directory. */
180 pwd = get_string_value ("PWD");
182 pwd = get_working_directory ("eterm");
183 fprintf (stderr, "\032/%s\n", pwd);
186 /* Call the YACC-generated parser and return the status of the parse.
187 Input is read from the current input stream (bash_input). yyparse
188 leaves the parsed command in the global variable GLOBAL_COMMAND.
189 This is where PROMPT_COMMAND is executed. */
194 char *command_to_execute;
197 run_pending_traps ();
199 /* Allow the execution of a random command just before the printing
200 of each primary prompt. If the shell variable PROMPT_COMMAND
201 is set then the value of it is the command to execute. */
202 if (interactive && bash_input.type != st_string)
204 command_to_execute = get_string_value ("PROMPT_COMMAND");
205 if (command_to_execute)
206 execute_prompt_command (command_to_execute);
208 if (running_under_emacs == 2)
209 send_pwd_to_eterm (); /* Yuck */
212 current_command_line_count = 0;
216 gather_here_documents ();
221 /* Read and parse a command, returning the status of the parse. The command
222 is left in the globval variable GLOBAL_COMMAND for use by reader_loop.
223 This is where the shell timeout code is executed. */
227 SHELL_VAR *tmout_var;
228 int tmout_len, result;
229 SigHandler *old_alrm;
231 prompt_string_pointer = &ps1_prompt;
232 global_command = (COMMAND *)NULL;
234 /* Only do timeouts if interactive. */
235 tmout_var = (SHELL_VAR *)NULL;
240 tmout_var = find_variable ("TMOUT");
241 old_alrm = (SigHandler *)NULL;
243 if (tmout_var && tmout_var->value)
245 tmout_len = atoi (tmout_var->value);
248 old_alrm = set_signal_handler (SIGALRM, alrm_catcher);
256 current_command_line_count = 0;
257 result = parse_command ();
259 if (interactive && tmout_var && (tmout_len > 0))
262 set_signal_handler (SIGALRM, old_alrm);
268 /* Take a string and run it through the shell parser, returning the
269 resultant word list. Used by compound array assignment. */
271 parse_string_to_word_list (s, whom)
275 COMMAND *saved_global;
276 #if defined (HISTORY)
277 int old_remember_on_history, old_history_expansion_inhibited;
280 #if defined (HISTORY)
281 old_remember_on_history = remember_on_history;
282 # if defined (BANG_HISTORY)
283 old_history_expansion_inhibited = history_expansion_inhibited;
285 bash_history_disable ();
290 saved_global = global_command;
291 global_command = (COMMAND *)0;
293 with_input_from_string (s, whom);
294 if (parse_command () != 0 || global_command == 0 || global_command->type != cm_simple)
297 dispose_command (global_command);
298 wl = (WORD_LIST *)NULL;
302 wl = global_command->value.Simple->words;
303 free (global_command->value.Simple);
304 free (global_command);
307 global_command = saved_global;
311 #if defined (HISTORY)
312 remember_on_history = old_remember_on_history;
313 # if defined (BANG_HISTORY)
314 history_expansion_inhibited = old_history_expansion_inhibited;
315 # endif /* BANG_HISTORY */