]> git.ipfire.org Git - thirdparty/bash.git/blob - eval.c
3ca5141f857129730ed8e4954428fdf5004f7d78
[thirdparty/bash.git] / eval.c
1 /* eval.c -- reading and evaluating commands.
2
3 Copyright (C) 1996 Free Software Foundation, Inc.
4
5 This file is part of GNU Bash.
6
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
12 details.
13
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.
19
20 Among other things, the copyright notice and this notice must be
21 preserved on all copies. */
22
23 #include "config.h"
24
25 #if defined (HAVE_UNISTD_H)
26 # ifdef _MINIX
27 # include <sys/types.h>
28 # endif
29 # include <unistd.h>
30 #endif
31
32 #include "bashansi.h"
33 #include <stdio.h>
34
35 #include "shell.h"
36 #include "flags.h"
37 #include "trap.h"
38
39 #include "builtins/common.h"
40
41 #include "input.h"
42 #include "execute_cmd.h"
43
44 #if defined (HISTORY)
45 # include "bashhist.h"
46 #endif
47
48 extern int yyparse ();
49
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;
58
59 /* Read and execute commands until EOF is reached. This assumes that
60 the input source has already been initialized. */
61 int
62 reader_loop ()
63 {
64 int our_indirection_level;
65 COMMAND *current_command = (COMMAND *)NULL;
66
67 our_indirection_level = ++indirection_level;
68
69 while (EOF_Reached == 0)
70 {
71 int code;
72
73 code = setjmp (top_level);
74
75 #if defined (PROCESS_SUBSTITUTION)
76 unlink_fifo_list ();
77 #endif /* PROCESS_SUBSTITUTION */
78
79 if (interactive_shell && signal_is_ignored (SIGINT) == 0)
80 set_signal_handler (SIGINT, sigint_sighandler);
81
82 if (code != NOT_JUMPED)
83 {
84 indirection_level = our_indirection_level;
85
86 switch (code)
87 {
88 /* Some kind of throw to top_level has occured. */
89 case FORCE_EOF:
90 case EXITPROG:
91 current_command = (COMMAND *)NULL;
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:
112 command_error ("reader_loop", CMDERR_BADJUMP, code, 0);
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;
138 stdin_redir = 0;
139 execute_command (current_command);
140
141 exec_done:
142 if (current_command)
143 {
144 dispose_command (current_command);
145 current_command = (COMMAND *)NULL;
146 }
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
164 static sighandler
165 alrm_catcher(i)
166 int i;
167 {
168 printf ("\007timed out waiting for input: auto-logout\n");
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. */
175 static void
176 send_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. */
190 int
191 parse_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. */
224 int
225 read_command ()
226 {
227 SHELL_VAR *tmout_var;
228 int tmout_len, result;
229 SigHandler *old_alrm;
230
231 prompt_string_pointer = &ps1_prompt;
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 }
267
268 /* Take a string and run it through the shell parser, returning the
269 resultant word list. Used by compound array assignment. */
270 WORD_LIST *
271 parse_string_to_word_list (s, whom)
272 char *s, *whom;
273 {
274 WORD_LIST *wl;
275 COMMAND *saved_global;
276 #if defined (HISTORY)
277 int old_remember_on_history, old_history_expansion_inhibited;
278 #endif
279
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;
284 # endif
285 bash_history_disable ();
286 #endif
287
288 push_stream (1);
289
290 saved_global = global_command;
291 global_command = (COMMAND *)0;
292
293 with_input_from_string (s, whom);
294 if (parse_command () != 0 || global_command == 0 || global_command->type != cm_simple)
295 {
296 if (global_command)
297 dispose_command (global_command);
298 wl = (WORD_LIST *)NULL;
299 }
300 else
301 {
302 wl = global_command->value.Simple->words;
303 free (global_command->value.Simple);
304 free (global_command);
305 }
306
307 global_command = saved_global;
308
309 pop_stream ();
310
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 */
316 #endif /* HISTORY */
317
318 return (wl);
319 }