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