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)
36 #include "builtins/common.h"
39 #include "execute_cmd.h"
41 extern int yyparse ();
43 extern int EOF_reached;
44 extern int indirection_level, interactive, interactive_shell;
45 extern int subshell_environment, running_under_emacs;
46 extern int last_command_exit_value, stdin_redir;
47 extern int need_here_doc;
48 extern int current_command_number, current_command_line_count, line_number;
49 extern char *ps1_prompt, **prompt_string_pointer;
50 extern int expand_aliases;
52 /* Read and execute commands until EOF is reached. This assumes that
53 the input source has already been initialized. */
57 int our_indirection_level;
58 COMMAND *current_command = (COMMAND *)NULL;
60 our_indirection_level = ++indirection_level;
62 while (EOF_Reached == 0)
66 code = setjmp (top_level);
68 #if defined (PROCESS_SUBSTITUTION)
70 #endif /* PROCESS_SUBSTITUTION */
72 if (interactive_shell && signal_is_ignored (SIGINT) == 0)
73 set_signal_handler (SIGINT, sigint_sighandler);
75 if (code != NOT_JUMPED)
77 indirection_level = our_indirection_level;
81 /* Some kind of throw to top_level has occured. */
84 current_command = (COMMAND *)NULL;
89 last_command_exit_value = 1;
90 if (subshell_environment)
92 current_command = (COMMAND *)NULL;
96 /* Obstack free command elements, etc. */
99 dispose_command (current_command);
100 current_command = (COMMAND *)NULL;
105 programming_error ("reader_loop: bad jump: code %d", code);
110 dispose_used_env_vars ();
112 #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
113 /* Attempt to reclaim memory allocated with alloca (). */
117 if (read_command () == 0)
119 if (interactive_shell == 0 && read_but_dont_execute)
121 last_command_exit_value = EXECUTION_SUCCESS;
122 dispose_command (global_command);
123 global_command = (COMMAND *)NULL;
125 else if (current_command = global_command)
127 global_command = (COMMAND *)NULL;
128 current_command_number++;
132 execute_command (current_command);
137 dispose_command (current_command);
138 current_command = (COMMAND *)NULL;
146 /* Parse error, maybe discard rest of stream if not interactive. */
147 if (interactive == 0)
150 if (just_one_command)
154 return (last_command_exit_value);
161 printf ("%ctimed out waiting for input: auto-logout\n", '\07');
162 jump_to_top_level (EXITPROG);
166 /* Send an escape sequence to emacs term mode to tell it the
167 current working directory. */
173 pwd = get_string_value ("PWD");
175 pwd = get_working_directory ("eterm");
176 fprintf (stderr, "\032/%s\n", pwd);
179 /* Call the YACC-generated parser and return the status of the parse.
180 Input is read from the current input stream (bash_input). yyparse
181 leaves the parsed command in the global variable GLOBAL_COMMAND.
182 This is where PROMPT_COMMAND is executed. */
187 char *command_to_execute;
190 run_pending_traps ();
192 /* Allow the execution of a random command just before the printing
193 of each primary prompt. If the shell variable PROMPT_COMMAND
194 is set then the value of it is the command to execute. */
195 if (interactive && bash_input.type != st_string)
197 command_to_execute = get_string_value ("PROMPT_COMMAND");
198 if (command_to_execute)
199 execute_prompt_command (command_to_execute);
201 if (running_under_emacs == 2)
202 send_pwd_to_eterm (); /* Yuck */
205 current_command_line_count = 0;
209 gather_here_documents ();
214 /* Read and parse a command, returning the status of the parse. The command
215 is left in the globval variable GLOBAL_COMMAND for use by reader_loop.
216 This is where the shell timeout code is executed. */
220 SHELL_VAR *tmout_var;
221 int tmout_len, result;
222 SigHandler *old_alrm;
224 prompt_string_pointer = &ps1_prompt;
225 global_command = (COMMAND *)NULL;
227 /* Only do timeouts if interactive. */
228 tmout_var = (SHELL_VAR *)NULL;
233 tmout_var = find_variable ("TMOUT");
234 old_alrm = (SigHandler *)NULL;
236 if (tmout_var && tmout_var->value)
238 tmout_len = atoi (tmout_var->value);
241 old_alrm = set_signal_handler (SIGALRM, alrm_catcher);
249 current_command_line_count = 0;
250 result = parse_command ();
252 if (interactive && tmout_var && (tmout_len > 0))
255 set_signal_handler (SIGALRM, old_alrm);