]>
Commit | Line | Data |
---|---|---|
bb70624e | 1 | /* eval.c -- reading and evaluating commands. */ |
ccc6cda3 | 2 | |
ac50fbac | 3 | /* Copyright (C) 1996-2011 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 | ||
a0c0a00f CR |
33 | #include <signal.h> |
34 | ||
b80f6443 JA |
35 | #include "bashintl.h" |
36 | ||
ccc6cda3 | 37 | #include "shell.h" |
d233b485 | 38 | #include "parser.h" |
ccc6cda3 JA |
39 | #include "flags.h" |
40 | #include "trap.h" | |
41 | ||
42 | #include "builtins/common.h" | |
43 | ||
44 | #include "input.h" | |
45 | #include "execute_cmd.h" | |
46 | ||
b72432fd JA |
47 | #if defined (HISTORY) |
48 | # include "bashhist.h" | |
49 | #endif | |
50 | ||
ac50fbac CR |
51 | #if defined (HAVE_POSIX_SIGNALS) |
52 | extern sigset_t top_level_mask; | |
53 | #endif | |
54 | ||
7117c2d2 JA |
55 | static void send_pwd_to_eterm __P((void)); |
56 | static sighandler alrm_catcher __P((int)); | |
57 | ||
ccc6cda3 JA |
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; | |
95732b49 | 64 | COMMAND * volatile current_command; |
ccc6cda3 | 65 | |
f73dda09 JA |
66 | USE_VAR(current_command); |
67 | ||
3185942a JA |
68 | current_command = (COMMAND *)NULL; |
69 | ||
ccc6cda3 JA |
70 | our_indirection_level = ++indirection_level; |
71 | ||
d233b485 CR |
72 | if (just_one_command) |
73 | reset_readahead_token (); | |
74 | ||
ccc6cda3 JA |
75 | while (EOF_Reached == 0) |
76 | { | |
77 | int code; | |
78 | ||
ac50fbac | 79 | code = setjmp_nosigs (top_level); |
ccc6cda3 JA |
80 | |
81 | #if defined (PROCESS_SUBSTITUTION) | |
82 | unlink_fifo_list (); | |
83 | #endif /* PROCESS_SUBSTITUTION */ | |
84 | ||
a0c0a00f CR |
85 | /* XXX - why do we set this every time through the loop? And why do |
86 | it if SIGINT is trapped in an interactive shell? */ | |
d233b485 | 87 | if (interactive_shell && signal_is_ignored (SIGINT) == 0 && signal_is_trapped (SIGINT) == 0) |
ccc6cda3 JA |
88 | set_signal_handler (SIGINT, sigint_sighandler); |
89 | ||
90 | if (code != NOT_JUMPED) | |
91 | { | |
92 | indirection_level = our_indirection_level; | |
93 | ||
94 | switch (code) | |
95 | { | |
ac50fbac | 96 | /* Some kind of throw to top_level has occurred. */ |
ccc6cda3 | 97 | case FORCE_EOF: |
b80f6443 | 98 | case ERREXIT: |
ccc6cda3 JA |
99 | case EXITPROG: |
100 | current_command = (COMMAND *)NULL; | |
bb70624e JA |
101 | if (exit_immediately_on_error) |
102 | variable_context = 0; /* not in a function */ | |
ccc6cda3 JA |
103 | EOF_Reached = EOF; |
104 | goto exec_done; | |
105 | ||
106 | case DISCARD: | |
3185942a JA |
107 | /* Make sure the exit status is reset to a non-zero value, but |
108 | leave existing non-zero values (e.g., > 128 on signal) | |
109 | alone. */ | |
110 | if (last_command_exit_value == 0) | |
111 | last_command_exit_value = EXECUTION_FAILURE; | |
ccc6cda3 JA |
112 | if (subshell_environment) |
113 | { | |
114 | current_command = (COMMAND *)NULL; | |
115 | EOF_Reached = EOF; | |
116 | goto exec_done; | |
117 | } | |
118 | /* Obstack free command elements, etc. */ | |
119 | if (current_command) | |
120 | { | |
121 | dispose_command (current_command); | |
122 | current_command = (COMMAND *)NULL; | |
123 | } | |
ac50fbac CR |
124 | #if defined (HAVE_POSIX_SIGNALS) |
125 | sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL); | |
126 | #endif | |
ccc6cda3 JA |
127 | break; |
128 | ||
129 | default: | |
b72432fd | 130 | command_error ("reader_loop", CMDERR_BADJUMP, code, 0); |
ccc6cda3 JA |
131 | } |
132 | } | |
133 | ||
134 | executing = 0; | |
7117c2d2 JA |
135 | if (temporary_env) |
136 | dispose_used_env_vars (); | |
ccc6cda3 JA |
137 | |
138 | #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA) | |
139 | /* Attempt to reclaim memory allocated with alloca (). */ | |
140 | (void) alloca (0); | |
141 | #endif | |
142 | ||
143 | if (read_command () == 0) | |
144 | { | |
145 | if (interactive_shell == 0 && read_but_dont_execute) | |
146 | { | |
147 | last_command_exit_value = EXECUTION_SUCCESS; | |
148 | dispose_command (global_command); | |
149 | global_command = (COMMAND *)NULL; | |
150 | } | |
151 | else if (current_command = global_command) | |
152 | { | |
153 | global_command = (COMMAND *)NULL; | |
a0c0a00f CR |
154 | |
155 | /* If the shell is interactive, expand and display $PS0 after reading a | |
156 | command (possibly a list or pipeline) and before executing it. */ | |
157 | if (interactive && ps0_prompt) | |
158 | { | |
159 | char *ps0_string; | |
160 | ||
161 | ps0_string = decode_prompt_string (ps0_prompt); | |
162 | if (ps0_string && *ps0_string) | |
163 | { | |
164 | fprintf (stderr, "%s", ps0_string); | |
165 | fflush (stderr); | |
166 | } | |
167 | free (ps0_string); | |
168 | } | |
169 | ||
d233b485 CR |
170 | current_command_number++; |
171 | ||
172 | executing = 1; | |
173 | stdin_redir = 0; | |
174 | ||
ccc6cda3 JA |
175 | execute_command (current_command); |
176 | ||
177 | exec_done: | |
95732b49 JA |
178 | QUIT; |
179 | ||
ccc6cda3 | 180 | if (current_command) |
28ef6c31 | 181 | { |
ccc6cda3 JA |
182 | dispose_command (current_command); |
183 | current_command = (COMMAND *)NULL; | |
28ef6c31 | 184 | } |
ccc6cda3 JA |
185 | } |
186 | } | |
187 | else | |
188 | { | |
189 | /* Parse error, maybe discard rest of stream if not interactive. */ | |
190 | if (interactive == 0) | |
191 | EOF_Reached = EOF; | |
192 | } | |
193 | if (just_one_command) | |
194 | EOF_Reached = EOF; | |
195 | } | |
196 | indirection_level--; | |
197 | return (last_command_exit_value); | |
198 | } | |
199 | ||
d233b485 CR |
200 | /* Pretty print shell scripts */ |
201 | int | |
202 | pretty_print_loop () | |
203 | { | |
204 | COMMAND *current_command; | |
205 | char *command_to_print; | |
206 | int code; | |
207 | int global_posix_mode, last_was_newline; | |
208 | ||
209 | global_posix_mode = posixly_correct; | |
210 | last_was_newline = 0; | |
211 | while (EOF_Reached == 0) | |
212 | { | |
213 | code = setjmp_nosigs (top_level); | |
214 | if (code) | |
215 | return (EXECUTION_FAILURE); | |
216 | if (read_command() == 0) | |
217 | { | |
218 | current_command = global_command; | |
219 | global_command = 0; | |
220 | posixly_correct = 1; /* print posix-conformant */ | |
221 | if (current_command && (command_to_print = make_command_string (current_command))) | |
222 | { | |
223 | printf ("%s\n", command_to_print); /* for now */ | |
224 | last_was_newline = 0; | |
225 | } | |
226 | else if (last_was_newline == 0) | |
227 | { | |
228 | printf ("\n"); | |
229 | last_was_newline = 1; | |
230 | } | |
231 | posixly_correct = global_posix_mode; | |
232 | dispose_command (current_command); | |
233 | } | |
234 | else | |
235 | return (EXECUTION_FAILURE); | |
236 | } | |
237 | ||
238 | return (EXECUTION_SUCCESS); | |
239 | } | |
240 | ||
ccc6cda3 JA |
241 | static sighandler |
242 | alrm_catcher(i) | |
243 | int i; | |
244 | { | |
b80f6443 | 245 | printf (_("\007timed out waiting for input: auto-logout\n")); |
3185942a | 246 | fflush (stdout); |
b80f6443 | 247 | bash_logout (); /* run ~/.bash_logout if this is a login shell */ |
ccc6cda3 JA |
248 | jump_to_top_level (EXITPROG); |
249 | SIGRETURN (0); | |
250 | } | |
251 | ||
252 | /* Send an escape sequence to emacs term mode to tell it the | |
253 | current working directory. */ | |
254 | static void | |
255 | send_pwd_to_eterm () | |
256 | { | |
ac50fbac | 257 | char *pwd, *f; |
ccc6cda3 | 258 | |
ac50fbac | 259 | f = 0; |
ccc6cda3 JA |
260 | pwd = get_string_value ("PWD"); |
261 | if (pwd == 0) | |
ac50fbac | 262 | f = pwd = get_working_directory ("eterm"); |
ccc6cda3 | 263 | fprintf (stderr, "\032/%s\n", pwd); |
ac50fbac | 264 | free (f); |
ccc6cda3 JA |
265 | } |
266 | ||
d233b485 CR |
267 | static void |
268 | execute_prompt_command () | |
269 | { | |
270 | char *command_to_execute; | |
271 | ||
272 | command_to_execute = get_string_value ("PROMPT_COMMAND"); | |
273 | if (command_to_execute) | |
274 | execute_variable_command (command_to_execute, "PROMPT_COMMAND"); | |
275 | } | |
ccc6cda3 JA |
276 | /* Call the YACC-generated parser and return the status of the parse. |
277 | Input is read from the current input stream (bash_input). yyparse | |
278 | leaves the parsed command in the global variable GLOBAL_COMMAND. | |
279 | This is where PROMPT_COMMAND is executed. */ | |
280 | int | |
281 | parse_command () | |
282 | { | |
283 | int r; | |
ccc6cda3 JA |
284 | |
285 | need_here_doc = 0; | |
286 | run_pending_traps (); | |
287 | ||
288 | /* Allow the execution of a random command just before the printing | |
289 | of each primary prompt. If the shell variable PROMPT_COMMAND | |
290 | is set then the value of it is the command to execute. */ | |
a0c0a00f CR |
291 | /* The tests are a combination of SHOULD_PROMPT() and prompt_again() |
292 | from parse.y, which are the conditions under which the prompt is | |
293 | actually printed. */ | |
294 | if (interactive && bash_input.type != st_string && parser_expanding_alias() == 0) | |
ccc6cda3 | 295 | { |
d233b485 | 296 | execute_prompt_command (); |
ccc6cda3 JA |
297 | |
298 | if (running_under_emacs == 2) | |
299 | send_pwd_to_eterm (); /* Yuck */ | |
300 | } | |
301 | ||
302 | current_command_line_count = 0; | |
303 | r = yyparse (); | |
304 | ||
305 | if (need_here_doc) | |
306 | gather_here_documents (); | |
307 | ||
308 | return (r); | |
309 | } | |
310 | ||
311 | /* Read and parse a command, returning the status of the parse. The command | |
312 | is left in the globval variable GLOBAL_COMMAND for use by reader_loop. | |
313 | This is where the shell timeout code is executed. */ | |
314 | int | |
315 | read_command () | |
316 | { | |
317 | SHELL_VAR *tmout_var; | |
318 | int tmout_len, result; | |
319 | SigHandler *old_alrm; | |
320 | ||
bb70624e | 321 | set_current_prompt_level (1); |
ccc6cda3 JA |
322 | global_command = (COMMAND *)NULL; |
323 | ||
324 | /* Only do timeouts if interactive. */ | |
325 | tmout_var = (SHELL_VAR *)NULL; | |
326 | tmout_len = 0; | |
f73dda09 | 327 | old_alrm = (SigHandler *)NULL; |
ccc6cda3 JA |
328 | |
329 | if (interactive) | |
330 | { | |
331 | tmout_var = find_variable ("TMOUT"); | |
ccc6cda3 | 332 | |
7117c2d2 | 333 | if (tmout_var && var_isset (tmout_var)) |
ccc6cda3 | 334 | { |
7117c2d2 | 335 | tmout_len = atoi (value_cell (tmout_var)); |
ccc6cda3 JA |
336 | if (tmout_len > 0) |
337 | { | |
338 | old_alrm = set_signal_handler (SIGALRM, alrm_catcher); | |
339 | alarm (tmout_len); | |
340 | } | |
341 | } | |
342 | } | |
343 | ||
344 | QUIT; | |
345 | ||
346 | current_command_line_count = 0; | |
347 | result = parse_command (); | |
348 | ||
349 | if (interactive && tmout_var && (tmout_len > 0)) | |
350 | { | |
351 | alarm(0); | |
352 | set_signal_handler (SIGALRM, old_alrm); | |
353 | } | |
354 | ||
355 | return (result); | |
356 | } |