No specific user configuration
[platform/upstream/bash.git] / eval.c
1 /* eval.c -- reading and evaluating commands. */
2
3 /* Copyright (C) 1996-2011 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
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11
12    Bash is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Bash.  If not, see <http://www.gnu.org/licenses/>.
19 */
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 #if defined (HAVE_POSIX_SIGNALS)
58 extern sigset_t top_level_mask;
59 #endif
60
61 static void send_pwd_to_eterm __P((void));
62 static sighandler alrm_catcher __P((int));
63
64 /* Read and execute commands until EOF is reached.  This assumes that
65    the input source has already been initialized. */
66 int
67 reader_loop ()
68 {
69   int our_indirection_level;
70   COMMAND * volatile current_command;
71
72   USE_VAR(current_command);
73
74   current_command = (COMMAND *)NULL;
75
76   our_indirection_level = ++indirection_level;
77
78   while (EOF_Reached == 0)
79     {
80       int code;
81
82       code = setjmp_nosigs (top_level);
83
84 #if defined (PROCESS_SUBSTITUTION)
85       unlink_fifo_list ();
86 #endif /* PROCESS_SUBSTITUTION */
87
88       /* XXX - why do we set this every time through the loop? */
89       if (interactive_shell && signal_is_ignored (SIGINT) == 0)
90         set_signal_handler (SIGINT, sigint_sighandler);
91
92       if (code != NOT_JUMPED)
93         {
94           indirection_level = our_indirection_level;
95
96           switch (code)
97             {
98               /* Some kind of throw to top_level has occurred. */
99             case FORCE_EOF:
100             case ERREXIT:
101             case EXITPROG:
102               current_command = (COMMAND *)NULL;
103               if (exit_immediately_on_error)
104                 variable_context = 0;   /* not in a function */
105               EOF_Reached = EOF;
106               goto exec_done;
107
108             case DISCARD:
109               /* Make sure the exit status is reset to a non-zero value, but
110                  leave existing non-zero values (e.g., > 128 on signal)
111                  alone. */
112               if (last_command_exit_value == 0)
113                 last_command_exit_value = EXECUTION_FAILURE;
114               if (subshell_environment)
115                 {
116                   current_command = (COMMAND *)NULL;
117                   EOF_Reached = EOF;
118                   goto exec_done;
119                 }
120               /* Obstack free command elements, etc. */
121               if (current_command)
122                 {
123                   dispose_command (current_command);
124                   current_command = (COMMAND *)NULL;
125                 }
126 #if defined (HAVE_POSIX_SIGNALS)
127               sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
128 #endif
129               break;
130
131             default:
132               command_error ("reader_loop", CMDERR_BADJUMP, code, 0);
133             }
134         }
135
136       executing = 0;
137       if (temporary_env)
138         dispose_used_env_vars ();
139
140 #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
141       /* Attempt to reclaim memory allocated with alloca (). */
142       (void) alloca (0);
143 #endif
144
145       if (read_command () == 0)
146         {
147           if (interactive_shell == 0 && read_but_dont_execute)
148             {
149               last_command_exit_value = EXECUTION_SUCCESS;
150               dispose_command (global_command);
151               global_command = (COMMAND *)NULL;
152             }
153           else if (current_command = global_command)
154             {
155               global_command = (COMMAND *)NULL;
156               current_command_number++;
157
158               executing = 1;
159               stdin_redir = 0;
160               execute_command (current_command);
161
162             exec_done:
163               QUIT;
164
165               if (current_command)
166                 {
167                   dispose_command (current_command);
168                   current_command = (COMMAND *)NULL;
169                 }
170             }
171         }
172       else
173         {
174           /* Parse error, maybe discard rest of stream if not interactive. */
175           if (interactive == 0)
176             EOF_Reached = EOF;
177         }
178       if (just_one_command)
179         EOF_Reached = EOF;
180     }
181   indirection_level--;
182   return (last_command_exit_value);
183 }
184
185 static sighandler
186 alrm_catcher(i)
187      int i;
188 {
189   printf (_("\007timed out waiting for input: auto-logout\n"));
190   fflush (stdout);
191   bash_logout ();       /* run ~/.bash_logout if this is a login shell */
192   jump_to_top_level (EXITPROG);
193   SIGRETURN (0);
194 }
195
196 /* Send an escape sequence to emacs term mode to tell it the
197    current working directory. */
198 static void
199 send_pwd_to_eterm ()
200 {
201   char *pwd, *f;
202
203   f = 0;
204   pwd = get_string_value ("PWD");
205   if (pwd == 0)
206     f = pwd = get_working_directory ("eterm");
207   fprintf (stderr, "\032/%s\n", pwd);
208   free (f);
209 }
210
211 /* Call the YACC-generated parser and return the status of the parse.
212    Input is read from the current input stream (bash_input).  yyparse
213    leaves the parsed command in the global variable GLOBAL_COMMAND.
214    This is where PROMPT_COMMAND is executed. */
215 int
216 parse_command ()
217 {
218   int r;
219   char *command_to_execute;
220
221   need_here_doc = 0;
222   run_pending_traps ();
223
224   /* Allow the execution of a random command just before the printing
225      of each primary prompt.  If the shell variable PROMPT_COMMAND
226      is set then the value of it is the command to execute. */
227   if (interactive && bash_input.type != st_string)
228     {
229       command_to_execute = get_string_value ("PROMPT_COMMAND");
230       if (command_to_execute)
231         execute_variable_command (command_to_execute, "PROMPT_COMMAND");
232
233       if (running_under_emacs == 2)
234         send_pwd_to_eterm ();   /* Yuck */
235     }
236
237   current_command_line_count = 0;
238   r = yyparse ();
239
240   if (need_here_doc)
241     gather_here_documents ();
242
243   return (r);
244 }
245
246 /* Read and parse a command, returning the status of the parse.  The command
247    is left in the globval variable GLOBAL_COMMAND for use by reader_loop.
248    This is where the shell timeout code is executed. */
249 int
250 read_command ()
251 {
252   SHELL_VAR *tmout_var;
253   int tmout_len, result;
254   SigHandler *old_alrm;
255
256   set_current_prompt_level (1);
257   global_command = (COMMAND *)NULL;
258
259   /* Only do timeouts if interactive. */
260   tmout_var = (SHELL_VAR *)NULL;
261   tmout_len = 0;
262   old_alrm = (SigHandler *)NULL;
263
264   if (interactive)
265     {
266       tmout_var = find_variable ("TMOUT");
267
268       if (tmout_var && var_isset (tmout_var))
269         {
270           tmout_len = atoi (value_cell (tmout_var));
271           if (tmout_len > 0)
272             {
273               old_alrm = set_signal_handler (SIGALRM, alrm_catcher);
274               alarm (tmout_len);
275             }
276         }
277     }
278
279   QUIT;
280
281   current_command_line_count = 0;
282   result = parse_command ();
283
284   if (interactive && tmout_var && (tmout_len > 0))
285     {
286       alarm(0);
287       set_signal_handler (SIGALRM, old_alrm);
288     }
289
290   return (result);
291 }