| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1 | /* eval.c -- reading and evaluating commands. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3 | /* Copyright (C) 1996-2011 Free Software Foundation, Inc. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5 | This file is part of GNU Bash, the Bourne Again SHell. |
| 6 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 11 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 16 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 17 | You should have received a copy of the GNU General Public License |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 20 | |
| 21 | #include "config.h" |
| 22 | |
| 23 | #if defined (HAVE_UNISTD_H) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 24 | # ifdef _MINIX |
| 25 | # include <sys/types.h> |
| 26 | # endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 27 | # include <unistd.h> |
| 28 | #endif |
| 29 | |
| 30 | #include "bashansi.h" |
| 31 | #include <stdio.h> |
| 32 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 33 | #include <signal.h> |
| 34 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 35 | #include "bashintl.h" |
| 36 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 37 | #include "shell.h" |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 38 | #include "parser.h" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 47 | #if defined (HISTORY) |
| 48 | # include "bashhist.h" |
| 49 | #endif |
| 50 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 51 | #if defined (HAVE_POSIX_SIGNALS) |
| 52 | extern sigset_t top_level_mask; |
| 53 | #endif |
| 54 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 55 | static void send_pwd_to_eterm __P((void)); |
| 56 | static sighandler alrm_catcher __P((int)); |
| 57 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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; |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 64 | COMMAND * volatile current_command; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 65 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 66 | USE_VAR(current_command); |
| 67 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 68 | current_command = (COMMAND *)NULL; |
| 69 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 70 | our_indirection_level = ++indirection_level; |
| 71 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 72 | if (just_one_command) |
| 73 | reset_readahead_token (); |
| 74 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 75 | while (EOF_Reached == 0) |
| 76 | { |
| 77 | int code; |
| 78 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 79 | code = setjmp_nosigs (top_level); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 80 | |
| 81 | #if defined (PROCESS_SUBSTITUTION) |
| 82 | unlink_fifo_list (); |
| 83 | #endif /* PROCESS_SUBSTITUTION */ |
| 84 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 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? */ |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 87 | if (interactive_shell && signal_is_ignored (SIGINT) == 0 && signal_is_trapped (SIGINT) == 0) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | { |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 96 | /* Some kind of throw to top_level has occurred. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 97 | case FORCE_EOF: |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 98 | case ERREXIT: |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 99 | case EXITPROG: |
| 100 | current_command = (COMMAND *)NULL; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 101 | if (exit_immediately_on_error) |
| 102 | variable_context = 0; /* not in a function */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 103 | EOF_Reached = EOF; |
| 104 | goto exec_done; |
| 105 | |
| 106 | case DISCARD: |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | } |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 124 | #if defined (HAVE_POSIX_SIGNALS) |
| 125 | sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL); |
| 126 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 127 | break; |
| 128 | |
| 129 | default: |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 130 | command_error ("reader_loop", CMDERR_BADJUMP, code, 0); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | executing = 0; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 135 | if (temporary_env) |
| 136 | dispose_used_env_vars (); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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; |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 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 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 170 | current_command_number++; |
| 171 | |
| 172 | executing = 1; |
| 173 | stdin_redir = 0; |
| 174 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 175 | execute_command (current_command); |
| 176 | |
| 177 | exec_done: |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 178 | QUIT; |
| 179 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 180 | if (current_command) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 181 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 182 | dispose_command (current_command); |
| 183 | current_command = (COMMAND *)NULL; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 184 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 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 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 241 | static sighandler |
| 242 | alrm_catcher(i) |
| 243 | int i; |
| 244 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 245 | printf (_("\007timed out waiting for input: auto-logout\n")); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 246 | fflush (stdout); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 247 | bash_logout (); /* run ~/.bash_logout if this is a login shell */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | { |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 257 | char *pwd, *f; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 258 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 259 | f = 0; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 260 | pwd = get_string_value ("PWD"); |
| 261 | if (pwd == 0) |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 262 | f = pwd = get_working_directory ("eterm"); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 263 | fprintf (stderr, "\032/%s\n", pwd); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 264 | free (f); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 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 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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. */ |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 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) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 295 | { |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 296 | execute_prompt_command (); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 321 | set_current_prompt_level (1); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 322 | global_command = (COMMAND *)NULL; |
| 323 | |
| 324 | /* Only do timeouts if interactive. */ |
| 325 | tmout_var = (SHELL_VAR *)NULL; |
| 326 | tmout_len = 0; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 327 | old_alrm = (SigHandler *)NULL; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 328 | |
| 329 | if (interactive) |
| 330 | { |
| 331 | tmout_var = find_variable ("TMOUT"); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 332 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 333 | if (tmout_var && var_isset (tmout_var)) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 334 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 335 | tmout_len = atoi (value_cell (tmout_var)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | } |