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