]>
Commit | Line | Data |
---|---|---|
bb70624e | 1 | /* eval.c -- reading and evaluating commands. */ |
ccc6cda3 | 2 | |
3185942a | 3 | /* Copyright (C) 1996-2009 Free Software Foundation, Inc. |
ccc6cda3 | 4 | |
bb70624e | 5 | This file is part of GNU Bash, the Bourne Again SHell. |
ccc6cda3 | 6 | |
3185942a JA |
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. | |
ccc6cda3 | 11 | |
3185942a JA |
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. | |
ccc6cda3 | 16 | |
bb70624e | 17 | You should have received a copy of the GNU General Public License |
3185942a JA |
18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
19 | */ | |
ccc6cda3 JA |
20 | |
21 | #include "config.h" | |
22 | ||
23 | #if defined (HAVE_UNISTD_H) | |
cce855bc JA |
24 | # ifdef _MINIX |
25 | # include <sys/types.h> | |
26 | # endif | |
ccc6cda3 JA |
27 | # include <unistd.h> |
28 | #endif | |
29 | ||
30 | #include "bashansi.h" | |
31 | #include <stdio.h> | |
32 | ||
b80f6443 JA |
33 | #include "bashintl.h" |
34 | ||
ccc6cda3 JA |
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 | ||
b72432fd JA |
44 | #if defined (HISTORY) |
45 | # include "bashhist.h" | |
46 | #endif | |
47 | ||
ccc6cda3 | 48 | extern int EOF_reached; |
f73dda09 | 49 | extern int indirection_level; |
bb70624e | 50 | extern int posixly_correct; |
ccc6cda3 | 51 | extern int subshell_environment, running_under_emacs; |
d166f048 | 52 | extern int last_command_exit_value, stdin_redir; |
ccc6cda3 JA |
53 | extern int need_here_doc; |
54 | extern int current_command_number, current_command_line_count, line_number; | |
ccc6cda3 JA |
55 | extern int expand_aliases; |
56 | ||
7117c2d2 JA |
57 | static void send_pwd_to_eterm __P((void)); |
58 | static sighandler alrm_catcher __P((int)); | |
59 | ||
ccc6cda3 JA |
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; | |
95732b49 | 66 | COMMAND * volatile current_command; |
ccc6cda3 | 67 | |
f73dda09 JA |
68 | USE_VAR(current_command); |
69 | ||
3185942a JA |
70 | current_command = (COMMAND *)NULL; |
71 | ||
ccc6cda3 JA |
72 | our_indirection_level = ++indirection_level; |
73 | ||
74 | while (EOF_Reached == 0) | |
75 | { | |
76 | int code; | |
77 | ||
78 | code = setjmp (top_level); | |
79 | ||
80 | #if defined (PROCESS_SUBSTITUTION) | |
81 | unlink_fifo_list (); | |
82 | #endif /* PROCESS_SUBSTITUTION */ | |
83 | ||
84 | if (interactive_shell && signal_is_ignored (SIGINT) == 0) | |
85 | set_signal_handler (SIGINT, sigint_sighandler); | |
86 | ||
87 | if (code != NOT_JUMPED) | |
88 | { | |
89 | indirection_level = our_indirection_level; | |
90 | ||
91 | switch (code) | |
92 | { | |
93 | /* Some kind of throw to top_level has occured. */ | |
94 | case FORCE_EOF: | |
b80f6443 | 95 | case ERREXIT: |
ccc6cda3 JA |
96 | case EXITPROG: |
97 | current_command = (COMMAND *)NULL; | |
bb70624e JA |
98 | if (exit_immediately_on_error) |
99 | variable_context = 0; /* not in a function */ | |
ccc6cda3 JA |
100 | EOF_Reached = EOF; |
101 | goto exec_done; | |
102 | ||
103 | case DISCARD: | |
3185942a JA |
104 | /* Make sure the exit status is reset to a non-zero value, but |
105 | leave existing non-zero values (e.g., > 128 on signal) | |
106 | alone. */ | |
107 | if (last_command_exit_value == 0) | |
108 | last_command_exit_value = EXECUTION_FAILURE; | |
ccc6cda3 JA |
109 | if (subshell_environment) |
110 | { | |
111 | current_command = (COMMAND *)NULL; | |
112 | EOF_Reached = EOF; | |
113 | goto exec_done; | |
114 | } | |
115 | /* Obstack free command elements, etc. */ | |
116 | if (current_command) | |
117 | { | |
118 | dispose_command (current_command); | |
119 | current_command = (COMMAND *)NULL; | |
120 | } | |
121 | break; | |
122 | ||
123 | default: | |
b72432fd | 124 | command_error ("reader_loop", CMDERR_BADJUMP, code, 0); |
ccc6cda3 JA |
125 | } |
126 | } | |
127 | ||
128 | executing = 0; | |
7117c2d2 JA |
129 | if (temporary_env) |
130 | dispose_used_env_vars (); | |
ccc6cda3 JA |
131 | |
132 | #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA) | |
133 | /* Attempt to reclaim memory allocated with alloca (). */ | |
134 | (void) alloca (0); | |
135 | #endif | |
136 | ||
137 | if (read_command () == 0) | |
138 | { | |
139 | if (interactive_shell == 0 && read_but_dont_execute) | |
140 | { | |
141 | last_command_exit_value = EXECUTION_SUCCESS; | |
142 | dispose_command (global_command); | |
143 | global_command = (COMMAND *)NULL; | |
144 | } | |
145 | else if (current_command = global_command) | |
146 | { | |
147 | global_command = (COMMAND *)NULL; | |
148 | current_command_number++; | |
149 | ||
150 | executing = 1; | |
d166f048 | 151 | stdin_redir = 0; |
ccc6cda3 JA |
152 | execute_command (current_command); |
153 | ||
154 | exec_done: | |
95732b49 JA |
155 | QUIT; |
156 | ||
ccc6cda3 | 157 | if (current_command) |
28ef6c31 | 158 | { |
ccc6cda3 JA |
159 | dispose_command (current_command); |
160 | current_command = (COMMAND *)NULL; | |
28ef6c31 | 161 | } |
ccc6cda3 JA |
162 | } |
163 | } | |
164 | else | |
165 | { | |
166 | /* Parse error, maybe discard rest of stream if not interactive. */ | |
167 | if (interactive == 0) | |
168 | EOF_Reached = EOF; | |
169 | } | |
170 | if (just_one_command) | |
171 | EOF_Reached = EOF; | |
172 | } | |
173 | indirection_level--; | |
174 | return (last_command_exit_value); | |
175 | } | |
176 | ||
177 | static sighandler | |
178 | alrm_catcher(i) | |
179 | int i; | |
180 | { | |
b80f6443 | 181 | printf (_("\007timed out waiting for input: auto-logout\n")); |
3185942a | 182 | fflush (stdout); |
b80f6443 | 183 | bash_logout (); /* run ~/.bash_logout if this is a login shell */ |
ccc6cda3 JA |
184 | jump_to_top_level (EXITPROG); |
185 | SIGRETURN (0); | |
186 | } | |
187 | ||
188 | /* Send an escape sequence to emacs term mode to tell it the | |
189 | current working directory. */ | |
190 | static void | |
191 | send_pwd_to_eterm () | |
192 | { | |
193 | char *pwd; | |
194 | ||
195 | pwd = get_string_value ("PWD"); | |
196 | if (pwd == 0) | |
197 | pwd = get_working_directory ("eterm"); | |
198 | fprintf (stderr, "\032/%s\n", pwd); | |
199 | } | |
200 | ||
201 | /* Call the YACC-generated parser and return the status of the parse. | |
202 | Input is read from the current input stream (bash_input). yyparse | |
203 | leaves the parsed command in the global variable GLOBAL_COMMAND. | |
204 | This is where PROMPT_COMMAND is executed. */ | |
205 | int | |
206 | parse_command () | |
207 | { | |
208 | int r; | |
209 | char *command_to_execute; | |
210 | ||
211 | need_here_doc = 0; | |
212 | run_pending_traps (); | |
213 | ||
214 | /* Allow the execution of a random command just before the printing | |
215 | of each primary prompt. If the shell variable PROMPT_COMMAND | |
216 | is set then the value of it is the command to execute. */ | |
217 | if (interactive && bash_input.type != st_string) | |
218 | { | |
219 | command_to_execute = get_string_value ("PROMPT_COMMAND"); | |
220 | if (command_to_execute) | |
0628567a | 221 | execute_variable_command (command_to_execute, "PROMPT_COMMAND"); |
ccc6cda3 JA |
222 | |
223 | if (running_under_emacs == 2) | |
224 | send_pwd_to_eterm (); /* Yuck */ | |
225 | } | |
226 | ||
227 | current_command_line_count = 0; | |
228 | r = yyparse (); | |
229 | ||
230 | if (need_here_doc) | |
231 | gather_here_documents (); | |
232 | ||
233 | return (r); | |
234 | } | |
235 | ||
236 | /* Read and parse a command, returning the status of the parse. The command | |
237 | is left in the globval variable GLOBAL_COMMAND for use by reader_loop. | |
238 | This is where the shell timeout code is executed. */ | |
239 | int | |
240 | read_command () | |
241 | { | |
242 | SHELL_VAR *tmout_var; | |
243 | int tmout_len, result; | |
244 | SigHandler *old_alrm; | |
245 | ||
bb70624e | 246 | set_current_prompt_level (1); |
ccc6cda3 JA |
247 | global_command = (COMMAND *)NULL; |
248 | ||
249 | /* Only do timeouts if interactive. */ | |
250 | tmout_var = (SHELL_VAR *)NULL; | |
251 | tmout_len = 0; | |
f73dda09 | 252 | old_alrm = (SigHandler *)NULL; |
ccc6cda3 JA |
253 | |
254 | if (interactive) | |
255 | { | |
256 | tmout_var = find_variable ("TMOUT"); | |
ccc6cda3 | 257 | |
7117c2d2 | 258 | if (tmout_var && var_isset (tmout_var)) |
ccc6cda3 | 259 | { |
7117c2d2 | 260 | tmout_len = atoi (value_cell (tmout_var)); |
ccc6cda3 JA |
261 | if (tmout_len > 0) |
262 | { | |
263 | old_alrm = set_signal_handler (SIGALRM, alrm_catcher); | |
264 | alarm (tmout_len); | |
265 | } | |
266 | } | |
267 | } | |
268 | ||
269 | QUIT; | |
270 | ||
271 | current_command_line_count = 0; | |
272 | result = parse_command (); | |
273 | ||
274 | if (interactive && tmout_var && (tmout_len > 0)) | |
275 | { | |
276 | alarm(0); | |
277 | set_signal_handler (SIGALRM, old_alrm); | |
278 | } | |
279 | ||
280 | return (result); | |
281 | } |