]> git.ipfire.org Git - thirdparty/bash.git/blob - eval.c
Imported from ../bash-2.05b.tar.gz.
[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, the Bourne Again SHell.
6
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.
11
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.
16
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. */
20
21 #include "config.h"
22
23 #if defined (HAVE_UNISTD_H)
24 # ifdef _MINIX
25 # include <sys/types.h>
26 # endif
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
42 #if defined (HISTORY)
43 # include "bashhist.h"
44 #endif
45
46 extern int EOF_reached;
47 extern int indirection_level;
48 extern int posixly_correct;
49 extern int subshell_environment, running_under_emacs;
50 extern int last_command_exit_value, stdin_redir;
51 extern int need_here_doc;
52 extern int current_command_number, current_command_line_count, line_number;
53 extern int expand_aliases;
54
55 static void send_pwd_to_eterm __P((void));
56 static sighandler alrm_catcher __P((int));
57
58 /* Read and execute commands until EOF is reached. This assumes that
59 the input source has already been initialized. */
60 int
61 reader_loop ()
62 {
63 int our_indirection_level;
64 COMMAND *current_command = (COMMAND *)NULL;
65
66 USE_VAR(current_command);
67
68 our_indirection_level = ++indirection_level;
69
70 while (EOF_Reached == 0)
71 {
72 int code;
73
74 code = setjmp (top_level);
75
76 #if defined (PROCESS_SUBSTITUTION)
77 unlink_fifo_list ();
78 #endif /* PROCESS_SUBSTITUTION */
79
80 if (interactive_shell && signal_is_ignored (SIGINT) == 0)
81 set_signal_handler (SIGINT, sigint_sighandler);
82
83 if (code != NOT_JUMPED)
84 {
85 indirection_level = our_indirection_level;
86
87 switch (code)
88 {
89 /* Some kind of throw to top_level has occured. */
90 case FORCE_EOF:
91 case EXITPROG:
92 current_command = (COMMAND *)NULL;
93 if (exit_immediately_on_error)
94 variable_context = 0; /* not in a function */
95 EOF_Reached = EOF;
96 goto exec_done;
97
98 case DISCARD:
99 last_command_exit_value = 1;
100 if (subshell_environment)
101 {
102 current_command = (COMMAND *)NULL;
103 EOF_Reached = EOF;
104 goto exec_done;
105 }
106 /* Obstack free command elements, etc. */
107 if (current_command)
108 {
109 dispose_command (current_command);
110 current_command = (COMMAND *)NULL;
111 }
112 break;
113
114 default:
115 command_error ("reader_loop", CMDERR_BADJUMP, code, 0);
116 }
117 }
118
119 executing = 0;
120 if (temporary_env)
121 dispose_used_env_vars ();
122
123 #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
124 /* Attempt to reclaim memory allocated with alloca (). */
125 (void) alloca (0);
126 #endif
127
128 if (read_command () == 0)
129 {
130 if (interactive_shell == 0 && read_but_dont_execute)
131 {
132 last_command_exit_value = EXECUTION_SUCCESS;
133 dispose_command (global_command);
134 global_command = (COMMAND *)NULL;
135 }
136 else if (current_command = global_command)
137 {
138 global_command = (COMMAND *)NULL;
139 current_command_number++;
140
141 executing = 1;
142 stdin_redir = 0;
143 execute_command (current_command);
144
145 exec_done:
146 if (current_command)
147 {
148 dispose_command (current_command);
149 current_command = (COMMAND *)NULL;
150 }
151
152 QUIT;
153 }
154 }
155 else
156 {
157 /* Parse error, maybe discard rest of stream if not interactive. */
158 if (interactive == 0)
159 EOF_Reached = EOF;
160 }
161 if (just_one_command)
162 EOF_Reached = EOF;
163 }
164 indirection_level--;
165 return (last_command_exit_value);
166 }
167
168 static sighandler
169 alrm_catcher(i)
170 int i;
171 {
172 printf ("\007timed out waiting for input: auto-logout\n");
173 jump_to_top_level (EXITPROG);
174 SIGRETURN (0);
175 }
176
177 /* Send an escape sequence to emacs term mode to tell it the
178 current working directory. */
179 static void
180 send_pwd_to_eterm ()
181 {
182 char *pwd;
183
184 pwd = get_string_value ("PWD");
185 if (pwd == 0)
186 pwd = get_working_directory ("eterm");
187 fprintf (stderr, "\032/%s\n", pwd);
188 }
189
190 /* Call the YACC-generated parser and return the status of the parse.
191 Input is read from the current input stream (bash_input). yyparse
192 leaves the parsed command in the global variable GLOBAL_COMMAND.
193 This is where PROMPT_COMMAND is executed. */
194 int
195 parse_command ()
196 {
197 int r;
198 char *command_to_execute;
199
200 need_here_doc = 0;
201 run_pending_traps ();
202
203 /* Allow the execution of a random command just before the printing
204 of each primary prompt. If the shell variable PROMPT_COMMAND
205 is set then the value of it is the command to execute. */
206 if (interactive && bash_input.type != st_string)
207 {
208 command_to_execute = get_string_value ("PROMPT_COMMAND");
209 if (command_to_execute)
210 execute_prompt_command (command_to_execute);
211
212 if (running_under_emacs == 2)
213 send_pwd_to_eterm (); /* Yuck */
214 }
215
216 current_command_line_count = 0;
217 r = yyparse ();
218
219 if (need_here_doc)
220 gather_here_documents ();
221
222 return (r);
223 }
224
225 /* Read and parse a command, returning the status of the parse. The command
226 is left in the globval variable GLOBAL_COMMAND for use by reader_loop.
227 This is where the shell timeout code is executed. */
228 int
229 read_command ()
230 {
231 SHELL_VAR *tmout_var;
232 int tmout_len, result;
233 SigHandler *old_alrm;
234
235 set_current_prompt_level (1);
236 global_command = (COMMAND *)NULL;
237
238 /* Only do timeouts if interactive. */
239 tmout_var = (SHELL_VAR *)NULL;
240 tmout_len = 0;
241 old_alrm = (SigHandler *)NULL;
242
243 if (interactive)
244 {
245 tmout_var = find_variable ("TMOUT");
246
247 if (tmout_var && var_isset (tmout_var))
248 {
249 tmout_len = atoi (value_cell (tmout_var));
250 if (tmout_len > 0)
251 {
252 old_alrm = set_signal_handler (SIGALRM, alrm_catcher);
253 alarm (tmout_len);
254 }
255 }
256 }
257
258 QUIT;
259
260 current_command_line_count = 0;
261 result = parse_command ();
262
263 if (interactive && tmout_var && (tmout_len > 0))
264 {
265 alarm(0);
266 set_signal_handler (SIGALRM, old_alrm);
267 }
268
269 return (result);
270 }