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;
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++;
131 execute_command (current_command);
136 dispose_command (current_command);
137 current_command = (COMMAND *)NULL;
145 /* Parse error, maybe discard rest of stream if not interactive. */
146 if (interactive == 0)
149 if (just_one_command)
153 return (last_command_exit_value);
160 printf ("%ctimed out waiting for input: auto-logout\n", '\07');
161 jump_to_top_level (EXITPROG);
165 /* Send an escape sequence to emacs term mode to tell it the
166 current working directory. */
172 pwd = get_string_value ("PWD");
174 pwd = get_working_directory ("eterm");
175 fprintf (stderr, "\032/%s\n", pwd);
178 /* Call the YACC-generated parser and return the status of the parse.
179 Input is read from the current input stream (bash_input). yyparse
180 leaves the parsed command in the global variable GLOBAL_COMMAND.
181 This is where PROMPT_COMMAND is executed. */
186 char *command_to_execute;
189 run_pending_traps ();
191 /* Allow the execution of a random command just before the printing
192 of each primary prompt. If the shell variable PROMPT_COMMAND
193 is set then the value of it is the command to execute. */
194 if (interactive && bash_input.type != st_string)
196 command_to_execute = get_string_value ("PROMPT_COMMAND");
197 if (command_to_execute)
198 execute_prompt_command (command_to_execute);
200 if (running_under_emacs == 2)
201 send_pwd_to_eterm (); /* Yuck */
204 current_command_line_count = 0;
208 gather_here_documents ();
213 /* Read and parse a command, returning the status of the parse. The command
214 is left in the globval variable GLOBAL_COMMAND for use by reader_loop.
215 This is where the shell timeout code is executed. */
219 SHELL_VAR *tmout_var;
220 int tmout_len, result;
221 SigHandler *old_alrm;
223 prompt_string_pointer = &ps1_prompt;
224 global_command = (COMMAND *)NULL;
226 /* Only do timeouts if interactive. */
227 tmout_var = (SHELL_VAR *)NULL;
232 tmout_var = find_variable ("TMOUT");
233 old_alrm = (SigHandler *)NULL;
235 if (tmout_var && tmout_var->value)
237 tmout_len = atoi (tmout_var->value);
240 old_alrm = set_signal_handler (SIGALRM, alrm_catcher);
248 current_command_line_count = 0;
249 result = parse_command ();
251 if (interactive && tmout_var && (tmout_len > 0))
254 set_signal_handler (SIGALRM, old_alrm);