| 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 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3 | /* Copyright (C) 1996 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 | |
| 7 | Bash is free software; you can redistribute it and/or modify it |
| 8 | under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2, or (at your option) |
| 10 | any later version. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 11 | |
| 12 | Bash is distributed in the hope that it will be useful, but WITHOUT |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 13 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 15 | 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 |
| 18 | along with Bash; see the file COPYING. If not, write to the Free |
| 19 | Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ |
| 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 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 33 | #include "bashintl.h" |
| 34 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 44 | #if defined (HISTORY) |
| 45 | # include "bashhist.h" |
| 46 | #endif |
| 47 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 48 | extern int EOF_reached; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 49 | extern int indirection_level; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 50 | extern int posixly_correct; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 51 | extern int subshell_environment, running_under_emacs; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 52 | extern int last_command_exit_value, stdin_redir; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 53 | extern int need_here_doc; |
| 54 | extern int current_command_number, current_command_line_count, line_number; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 55 | extern int expand_aliases; |
| 56 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 57 | static void send_pwd_to_eterm __P((void)); |
| 58 | static sighandler alrm_catcher __P((int)); |
| 59 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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; |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 66 | COMMAND * volatile current_command; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 67 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 68 | current_command = (COMMAND *)NULL; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 69 | USE_VAR(current_command); |
| 70 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 71 | our_indirection_level = ++indirection_level; |
| 72 | |
| 73 | while (EOF_Reached == 0) |
| 74 | { |
| 75 | int code; |
| 76 | |
| 77 | code = setjmp (top_level); |
| 78 | |
| 79 | #if defined (PROCESS_SUBSTITUTION) |
| 80 | unlink_fifo_list (); |
| 81 | #endif /* PROCESS_SUBSTITUTION */ |
| 82 | |
| 83 | if (interactive_shell && signal_is_ignored (SIGINT) == 0) |
| 84 | set_signal_handler (SIGINT, sigint_sighandler); |
| 85 | |
| 86 | if (code != NOT_JUMPED) |
| 87 | { |
| 88 | indirection_level = our_indirection_level; |
| 89 | |
| 90 | switch (code) |
| 91 | { |
| 92 | /* Some kind of throw to top_level has occured. */ |
| 93 | case FORCE_EOF: |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 94 | case ERREXIT: |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 95 | case EXITPROG: |
| 96 | current_command = (COMMAND *)NULL; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 97 | if (exit_immediately_on_error) |
| 98 | variable_context = 0; /* not in a function */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 99 | EOF_Reached = EOF; |
| 100 | goto exec_done; |
| 101 | |
| 102 | case DISCARD: |
| 103 | last_command_exit_value = 1; |
| 104 | if (subshell_environment) |
| 105 | { |
| 106 | current_command = (COMMAND *)NULL; |
| 107 | EOF_Reached = EOF; |
| 108 | goto exec_done; |
| 109 | } |
| 110 | /* Obstack free command elements, etc. */ |
| 111 | if (current_command) |
| 112 | { |
| 113 | dispose_command (current_command); |
| 114 | current_command = (COMMAND *)NULL; |
| 115 | } |
| 116 | break; |
| 117 | |
| 118 | default: |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 119 | command_error ("reader_loop", CMDERR_BADJUMP, code, 0); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | executing = 0; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 124 | if (temporary_env) |
| 125 | dispose_used_env_vars (); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 126 | |
| 127 | #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA) |
| 128 | /* Attempt to reclaim memory allocated with alloca (). */ |
| 129 | (void) alloca (0); |
| 130 | #endif |
| 131 | |
| 132 | if (read_command () == 0) |
| 133 | { |
| 134 | if (interactive_shell == 0 && read_but_dont_execute) |
| 135 | { |
| 136 | last_command_exit_value = EXECUTION_SUCCESS; |
| 137 | dispose_command (global_command); |
| 138 | global_command = (COMMAND *)NULL; |
| 139 | } |
| 140 | else if (current_command = global_command) |
| 141 | { |
| 142 | global_command = (COMMAND *)NULL; |
| 143 | current_command_number++; |
| 144 | |
| 145 | executing = 1; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 146 | stdin_redir = 0; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 147 | execute_command (current_command); |
| 148 | |
| 149 | exec_done: |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 150 | QUIT; |
| 151 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 152 | if (current_command) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 153 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 154 | dispose_command (current_command); |
| 155 | current_command = (COMMAND *)NULL; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 156 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | /* Parse error, maybe discard rest of stream if not interactive. */ |
| 162 | if (interactive == 0) |
| 163 | EOF_Reached = EOF; |
| 164 | } |
| 165 | if (just_one_command) |
| 166 | EOF_Reached = EOF; |
| 167 | } |
| 168 | indirection_level--; |
| 169 | return (last_command_exit_value); |
| 170 | } |
| 171 | |
| 172 | static sighandler |
| 173 | alrm_catcher(i) |
| 174 | int i; |
| 175 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 176 | printf (_("\007timed out waiting for input: auto-logout\n")); |
| 177 | bash_logout (); /* run ~/.bash_logout if this is a login shell */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 178 | jump_to_top_level (EXITPROG); |
| 179 | SIGRETURN (0); |
| 180 | } |
| 181 | |
| 182 | /* Send an escape sequence to emacs term mode to tell it the |
| 183 | current working directory. */ |
| 184 | static void |
| 185 | send_pwd_to_eterm () |
| 186 | { |
| 187 | char *pwd; |
| 188 | |
| 189 | pwd = get_string_value ("PWD"); |
| 190 | if (pwd == 0) |
| 191 | pwd = get_working_directory ("eterm"); |
| 192 | fprintf (stderr, "\032/%s\n", pwd); |
| 193 | } |
| 194 | |
| 195 | /* Call the YACC-generated parser and return the status of the parse. |
| 196 | Input is read from the current input stream (bash_input). yyparse |
| 197 | leaves the parsed command in the global variable GLOBAL_COMMAND. |
| 198 | This is where PROMPT_COMMAND is executed. */ |
| 199 | int |
| 200 | parse_command () |
| 201 | { |
| 202 | int r; |
| 203 | char *command_to_execute; |
| 204 | |
| 205 | need_here_doc = 0; |
| 206 | run_pending_traps (); |
| 207 | |
| 208 | /* Allow the execution of a random command just before the printing |
| 209 | of each primary prompt. If the shell variable PROMPT_COMMAND |
| 210 | is set then the value of it is the command to execute. */ |
| 211 | if (interactive && bash_input.type != st_string) |
| 212 | { |
| 213 | command_to_execute = get_string_value ("PROMPT_COMMAND"); |
| 214 | if (command_to_execute) |
| 215 | execute_prompt_command (command_to_execute); |
| 216 | |
| 217 | if (running_under_emacs == 2) |
| 218 | send_pwd_to_eterm (); /* Yuck */ |
| 219 | } |
| 220 | |
| 221 | current_command_line_count = 0; |
| 222 | r = yyparse (); |
| 223 | |
| 224 | if (need_here_doc) |
| 225 | gather_here_documents (); |
| 226 | |
| 227 | return (r); |
| 228 | } |
| 229 | |
| 230 | /* Read and parse a command, returning the status of the parse. The command |
| 231 | is left in the globval variable GLOBAL_COMMAND for use by reader_loop. |
| 232 | This is where the shell timeout code is executed. */ |
| 233 | int |
| 234 | read_command () |
| 235 | { |
| 236 | SHELL_VAR *tmout_var; |
| 237 | int tmout_len, result; |
| 238 | SigHandler *old_alrm; |
| 239 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 240 | set_current_prompt_level (1); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 241 | global_command = (COMMAND *)NULL; |
| 242 | |
| 243 | /* Only do timeouts if interactive. */ |
| 244 | tmout_var = (SHELL_VAR *)NULL; |
| 245 | tmout_len = 0; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 246 | old_alrm = (SigHandler *)NULL; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 247 | |
| 248 | if (interactive) |
| 249 | { |
| 250 | tmout_var = find_variable ("TMOUT"); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 251 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 252 | if (tmout_var && var_isset (tmout_var)) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 253 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 254 | tmout_len = atoi (value_cell (tmout_var)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 255 | if (tmout_len > 0) |
| 256 | { |
| 257 | old_alrm = set_signal_handler (SIGALRM, alrm_catcher); |
| 258 | alarm (tmout_len); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | QUIT; |
| 264 | |
| 265 | current_command_line_count = 0; |
| 266 | result = parse_command (); |
| 267 | |
| 268 | if (interactive && tmout_var && (tmout_len > 0)) |
| 269 | { |
| 270 | alarm(0); |
| 271 | set_signal_handler (SIGALRM, old_alrm); |
| 272 | } |
| 273 | |
| 274 | return (result); |
| 275 | } |