]> git.ipfire.org Git - thirdparty/bash.git/blame - eval.c
Imported from ../bash-2.05.tar.gz.
[thirdparty/bash.git] / eval.c
CommitLineData
bb70624e 1/* eval.c -- reading and evaluating commands. */
ccc6cda3 2
bb70624e 3/* Copyright (C) 1996 Free Software Foundation, Inc.
ccc6cda3 4
bb70624e 5 This file is part of GNU Bash, the Bourne Again SHell.
ccc6cda3 6
bb70624e
JA
7 Bash is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
ccc6cda3 11
bb70624e
JA
12 Bash is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
ccc6cda3 16
bb70624e
JA
17 You should have received a copy of the GNU General Public License
18 along with Bash; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
ccc6cda3
JA
20
21#include "config.h"
22
23#if defined (HAVE_UNISTD_H)
cce855bc
JA
24# ifdef _MINIX
25# include <sys/types.h>
26# endif
ccc6cda3
JA
27# include <unistd.h>
28#endif
29
30#include "bashansi.h"
31#include <stdio.h>
32
33#include "shell.h"
34#include "flags.h"
35#include "trap.h"
36
37#include "builtins/common.h"
38
39#include "input.h"
40#include "execute_cmd.h"
41
b72432fd
JA
42#if defined (HISTORY)
43# include "bashhist.h"
44#endif
45
ccc6cda3
JA
46extern int yyparse ();
47
48extern int EOF_reached;
49extern int indirection_level, interactive, interactive_shell;
bb70624e 50extern int posixly_correct;
ccc6cda3 51extern int subshell_environment, running_under_emacs;
d166f048 52extern int last_command_exit_value, stdin_redir;
ccc6cda3
JA
53extern int need_here_doc;
54extern int current_command_number, current_command_line_count, line_number;
ccc6cda3
JA
55extern int expand_aliases;
56
57/* Read and execute commands until EOF is reached. This assumes that
58 the input source has already been initialized. */
59int
60reader_loop ()
61{
62 int our_indirection_level;
63 COMMAND *current_command = (COMMAND *)NULL;
64
65 our_indirection_level = ++indirection_level;
66
67 while (EOF_Reached == 0)
68 {
69 int code;
70
71 code = setjmp (top_level);
72
73#if defined (PROCESS_SUBSTITUTION)
74 unlink_fifo_list ();
75#endif /* PROCESS_SUBSTITUTION */
76
77 if (interactive_shell && signal_is_ignored (SIGINT) == 0)
78 set_signal_handler (SIGINT, sigint_sighandler);
79
80 if (code != NOT_JUMPED)
81 {
82 indirection_level = our_indirection_level;
83
84 switch (code)
85 {
86 /* Some kind of throw to top_level has occured. */
87 case FORCE_EOF:
88 case EXITPROG:
89 current_command = (COMMAND *)NULL;
bb70624e
JA
90 if (exit_immediately_on_error)
91 variable_context = 0; /* not in a function */
ccc6cda3
JA
92 EOF_Reached = EOF;
93 goto exec_done;
94
95 case DISCARD:
96 last_command_exit_value = 1;
97 if (subshell_environment)
98 {
99 current_command = (COMMAND *)NULL;
100 EOF_Reached = EOF;
101 goto exec_done;
102 }
103 /* Obstack free command elements, etc. */
104 if (current_command)
105 {
106 dispose_command (current_command);
107 current_command = (COMMAND *)NULL;
108 }
109 break;
110
111 default:
b72432fd 112 command_error ("reader_loop", CMDERR_BADJUMP, code, 0);
ccc6cda3
JA
113 }
114 }
115
116 executing = 0;
117 dispose_used_env_vars ();
118
119#if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
120 /* Attempt to reclaim memory allocated with alloca (). */
121 (void) alloca (0);
122#endif
123
124 if (read_command () == 0)
125 {
126 if (interactive_shell == 0 && read_but_dont_execute)
127 {
128 last_command_exit_value = EXECUTION_SUCCESS;
129 dispose_command (global_command);
130 global_command = (COMMAND *)NULL;
131 }
132 else if (current_command = global_command)
133 {
134 global_command = (COMMAND *)NULL;
135 current_command_number++;
136
137 executing = 1;
d166f048 138 stdin_redir = 0;
ccc6cda3
JA
139 execute_command (current_command);
140
141 exec_done:
142 if (current_command)
28ef6c31 143 {
ccc6cda3
JA
144 dispose_command (current_command);
145 current_command = (COMMAND *)NULL;
28ef6c31 146 }
ccc6cda3
JA
147
148 QUIT;
149 }
150 }
151 else
152 {
153 /* Parse error, maybe discard rest of stream if not interactive. */
154 if (interactive == 0)
155 EOF_Reached = EOF;
156 }
157 if (just_one_command)
158 EOF_Reached = EOF;
159 }
160 indirection_level--;
161 return (last_command_exit_value);
162}
163
164static sighandler
165alrm_catcher(i)
166 int i;
167{
b72432fd 168 printf ("\007timed out waiting for input: auto-logout\n");
ccc6cda3
JA
169 jump_to_top_level (EXITPROG);
170 SIGRETURN (0);
171}
172
173/* Send an escape sequence to emacs term mode to tell it the
174 current working directory. */
175static void
176send_pwd_to_eterm ()
177{
178 char *pwd;
179
180 pwd = get_string_value ("PWD");
181 if (pwd == 0)
182 pwd = get_working_directory ("eterm");
183 fprintf (stderr, "\032/%s\n", pwd);
184}
185
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. */
190int
191parse_command ()
192{
193 int r;
194 char *command_to_execute;
195
196 need_here_doc = 0;
197 run_pending_traps ();
198
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)
203 {
204 command_to_execute = get_string_value ("PROMPT_COMMAND");
205 if (command_to_execute)
206 execute_prompt_command (command_to_execute);
207
208 if (running_under_emacs == 2)
209 send_pwd_to_eterm (); /* Yuck */
210 }
211
212 current_command_line_count = 0;
213 r = yyparse ();
214
215 if (need_here_doc)
216 gather_here_documents ();
217
218 return (r);
219}
220
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. */
224int
225read_command ()
226{
227 SHELL_VAR *tmout_var;
228 int tmout_len, result;
229 SigHandler *old_alrm;
230
bb70624e 231 set_current_prompt_level (1);
ccc6cda3
JA
232 global_command = (COMMAND *)NULL;
233
234 /* Only do timeouts if interactive. */
235 tmout_var = (SHELL_VAR *)NULL;
236 tmout_len = 0;
237
238 if (interactive)
239 {
240 tmout_var = find_variable ("TMOUT");
241 old_alrm = (SigHandler *)NULL;
242
243 if (tmout_var && tmout_var->value)
244 {
245 tmout_len = atoi (tmout_var->value);
246 if (tmout_len > 0)
247 {
248 old_alrm = set_signal_handler (SIGALRM, alrm_catcher);
249 alarm (tmout_len);
250 }
251 }
252 }
253
254 QUIT;
255
256 current_command_line_count = 0;
257 result = parse_command ();
258
259 if (interactive && tmout_var && (tmout_len > 0))
260 {
261 alarm(0);
262 set_signal_handler (SIGALRM, old_alrm);
263 }
264
265 return (result);
266}