| 1 | /* eval.c -- reading and evaluating commands. */
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 1996 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 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.
|
|---|
| 11 |
|
|---|
| 12 | Bash is distributed in the hope that it will be useful, but WITHOUT
|
|---|
| 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.
|
|---|
| 16 |
|
|---|
| 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. */
|
|---|
| 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 | static void send_pwd_to_eterm __P((void));
|
|---|
| 58 | static sighandler alrm_catcher __P((int));
|
|---|
| 59 |
|
|---|
| 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;
|
|---|
| 66 | COMMAND * volatile current_command;
|
|---|
| 67 |
|
|---|
| 68 | current_command = (COMMAND *)NULL;
|
|---|
| 69 | USE_VAR(current_command);
|
|---|
| 70 |
|
|---|
| 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:
|
|---|
| 94 | case ERREXIT:
|
|---|
| 95 | case EXITPROG:
|
|---|
| 96 | current_command = (COMMAND *)NULL;
|
|---|
| 97 | if (exit_immediately_on_error)
|
|---|
| 98 | variable_context = 0; /* not in a function */
|
|---|
| 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:
|
|---|
| 119 | command_error ("reader_loop", CMDERR_BADJUMP, code, 0);
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | executing = 0;
|
|---|
| 124 | if (temporary_env)
|
|---|
| 125 | dispose_used_env_vars ();
|
|---|
| 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;
|
|---|
| 146 | stdin_redir = 0;
|
|---|
| 147 | execute_command (current_command);
|
|---|
| 148 |
|
|---|
| 149 | exec_done:
|
|---|
| 150 | QUIT;
|
|---|
| 151 |
|
|---|
| 152 | if (current_command)
|
|---|
| 153 | {
|
|---|
| 154 | dispose_command (current_command);
|
|---|
| 155 | current_command = (COMMAND *)NULL;
|
|---|
| 156 | }
|
|---|
| 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);
|
|---|
|
|---|