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"
44 extern int yyparse ();
46 extern int EOF_reached;
47 extern int indirection_level, interactive, interactive_shell;
48 extern int subshell_environment, running_under_emacs;
49 extern int last_command_exit_value, stdin_redir;
50 extern int need_here_doc;
51 extern int current_command_number, current_command_line_count, line_number;
52 extern char *ps1_prompt, **prompt_string_pointer;
53 extern int expand_aliases;
55 /* Read and execute commands until EOF is reached. This assumes that
56 the input source has already been initialized. */
60 int our_indirection_level;
61 COMMAND *current_command = (COMMAND *)NULL;
63 our_indirection_level = ++indirection_level;
65 while (EOF_Reached == 0)
69 code = setjmp (top_level);
71 #if defined (PROCESS_SUBSTITUTION)
73 #endif /* PROCESS_SUBSTITUTION */
75 if (interactive_shell && signal_is_ignored (SIGINT) == 0)
76 set_signal_handler (SIGINT, sigint_sighandler);
78 if (code != NOT_JUMPED)
80 indirection_level = our_indirection_level;
84 /* Some kind of throw to top_level has occured. */
87 current_command = (COMMAND *)NULL;
92 last_command_exit_value = 1;
93 if (subshell_environment)
95 current_command = (COMMAND *)NULL;
99 /* Obstack free command elements, etc. */
102 dispose_command (current_command);
103 current_command = (COMMAND *)NULL;
108 programming_error ("reader_loop: bad jump: code %d", code);
113 dispose_used_env_vars ();
115 #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
116 /* Attempt to reclaim memory allocated with alloca (). */
120 if (read_command () == 0)
122 if (interactive_shell == 0 && read_but_dont_execute)
124 last_command_exit_value = EXECUTION_SUCCESS;
125 dispose_command (global_command);
126 global_command = (COMMAND *)NULL;
128 else if (current_command = global_command)
130 global_command = (COMMAND *)NULL;
131 current_command_number++;
135 execute_command (current_command);
140 dispose_command (current_command);
141 current_command = (COMMAND *)NULL;
149 /* Parse error, maybe discard rest of stream if not interactive. */
150 if (interactive == 0)
153 if (just_one_command)
157 return (last_command_exit_value);
164 printf ("%ctimed out waiting for input: auto-logout\n", '\07');
165 jump_to_top_level (EXITPROG);
169 /* Send an escape sequence to emacs term mode to tell it the
170 current working directory. */
176 pwd = get_string_value ("PWD");
178 pwd = get_working_directory ("eterm");
179 fprintf (stderr, "\032/%s\n", pwd);
182 /* Call the YACC-generated parser and return the status of the parse.
183 Input is read from the current input stream (bash_input). yyparse
184 leaves the parsed command in the global variable GLOBAL_COMMAND.
185 This is where PROMPT_COMMAND is executed. */
190 char *command_to_execute;
193 run_pending_traps ();
195 /* Allow the execution of a random command just before the printing
196 of each primary prompt. If the shell variable PROMPT_COMMAND
197 is set then the value of it is the command to execute. */
198 if (interactive && bash_input.type != st_string)
200 command_to_execute = get_string_value ("PROMPT_COMMAND");
201 if (command_to_execute)
202 execute_prompt_command (command_to_execute);
204 if (running_under_emacs == 2)
205 send_pwd_to_eterm (); /* Yuck */
208 current_command_line_count = 0;
212 gather_here_documents ();
217 /* Read and parse a command, returning the status of the parse. The command
218 is left in the globval variable GLOBAL_COMMAND for use by reader_loop.
219 This is where the shell timeout code is executed. */
223 SHELL_VAR *tmout_var;
224 int tmout_len, result;
225 SigHandler *old_alrm;
227 prompt_string_pointer = &ps1_prompt;
228 global_command = (COMMAND *)NULL;
230 /* Only do timeouts if interactive. */
231 tmout_var = (SHELL_VAR *)NULL;
236 tmout_var = find_variable ("TMOUT");
237 old_alrm = (SigHandler *)NULL;
239 if (tmout_var && tmout_var->value)
241 tmout_len = atoi (tmout_var->value);
244 old_alrm = set_signal_handler (SIGALRM, alrm_catcher);
252 current_command_line_count = 0;
253 result = parse_command ();
255 if (interactive && tmout_var && (tmout_len > 0))
258 set_signal_handler (SIGALRM, old_alrm);
264 /* Take a string and run it through the shell parser, returning the
265 resultant word list. Used by compound array assignment. */
267 parse_string_to_word_list (s, whom)
271 COMMAND *saved_global;
275 saved_global = global_command;
276 global_command = (COMMAND *)0;
278 with_input_from_string (s, whom);
279 if (parse_command () != 0 || global_command == 0 || global_command->type != cm_simple)
282 dispose_command (global_command);
283 wl = (WORD_LIST *)NULL;
287 wl = global_command->value.Simple->words;
288 free (global_command->value.Simple);
289 free (global_command);
292 global_command = saved_global;