i-bash/eval.c

412 lines
11 KiB
C
Raw Normal View History

2000-03-17 21:46:59 +00:00
/* eval.c -- reading and evaluating commands. */
1996-12-23 17:02:34 +00:00
/* Copyright (C) 1996-2011 Free Software Foundation, Inc.
1996-12-23 17:02:34 +00:00
2000-03-17 21:46:59 +00:00
This file is part of GNU Bash, the Bourne Again SHell.
1996-12-23 17:02:34 +00:00
2009-01-12 13:36:28 +00:00
Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
1996-12-23 17:02:34 +00:00
2009-01-12 13:36:28 +00:00
Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
1996-12-23 17:02:34 +00:00
2000-03-17 21:46:59 +00:00
You should have received a copy of the GNU General Public License
2009-01-12 13:36:28 +00:00
along with Bash. If not, see <http://www.gnu.org/licenses/>.
*/
1996-12-23 17:02:34 +00:00
#include "config.h"
#if defined (HAVE_UNISTD_H)
1998-04-17 19:52:44 +00:00
# ifdef _MINIX
# include <sys/types.h>
# endif
1996-12-23 17:02:34 +00:00
# include <unistd.h>
#endif
#include "bashansi.h"
#include <stdio.h>
#include <signal.h>
2004-07-27 13:29:18 +00:00
#include "bashintl.h"
1996-12-23 17:02:34 +00:00
#include "shell.h"
#include "flags.h"
#include "trap.h"
#include "builtins/common.h"
#include "input.h"
#include "execute_cmd.h"
1999-02-19 17:11:39 +00:00
#if defined (HISTORY)
# include "bashhist.h"
#endif
#include <libguile.h>
1996-12-23 17:02:34 +00:00
extern int EOF_reached;
2001-11-13 17:56:06 +00:00
extern int indirection_level;
2000-03-17 21:46:59 +00:00
extern int posixly_correct;
1996-12-23 17:02:34 +00:00
extern int subshell_environment, running_under_emacs;
1997-06-05 14:59:13 +00:00
extern int last_command_exit_value, stdin_redir;
1996-12-23 17:02:34 +00:00
extern int need_here_doc;
extern int current_command_number, current_command_line_count, line_number;
extern int expand_aliases;
extern char *ps0_prompt;
1996-12-23 17:02:34 +00:00
#if defined (HAVE_POSIX_SIGNALS)
extern sigset_t top_level_mask;
#endif
2002-07-17 14:10:11 +00:00
static void send_pwd_to_eterm __P((void));
static sighandler alrm_catcher __P((int));
void execute_command_ (COMMAND *c)
{
/* WORD_LIST *w; */
/* for (w = c->value.Simple->words; w; w = w->next) */
/* { */
/* printf (" %s :-> %x\n", w->word->word, w->word->flags); */
/* } */
execute_command (c);
}
1996-12-23 17:02:34 +00:00
/* Read and execute commands until EOF is reached. This assumes that
the input source has already been initialized. */
int
reader_loop ()
{
int our_indirection_level;
2005-12-07 14:08:12 +00:00
COMMAND * volatile current_command;
1996-12-23 17:02:34 +00:00
2001-11-13 17:56:06 +00:00
USE_VAR(current_command);
2009-01-12 13:36:28 +00:00
current_command = (COMMAND *)NULL;
1996-12-23 17:02:34 +00:00
our_indirection_level = ++indirection_level;
while (EOF_Reached == 0)
{
int code;
code = setjmp_nosigs (top_level);
1996-12-23 17:02:34 +00:00
#if defined (PROCESS_SUBSTITUTION)
unlink_fifo_list ();
#endif /* PROCESS_SUBSTITUTION */
/* XXX - why do we set this every time through the loop? And why do
it if SIGINT is trapped in an interactive shell? */
1996-12-23 17:02:34 +00:00
if (interactive_shell && signal_is_ignored (SIGINT) == 0)
set_signal_handler (SIGINT, sigint_sighandler);
if (code != NOT_JUMPED)
{
indirection_level = our_indirection_level;
switch (code)
{
/* Some kind of throw to top_level has occurred. */
1996-12-23 17:02:34 +00:00
case FORCE_EOF:
2004-07-27 13:29:18 +00:00
case ERREXIT:
1996-12-23 17:02:34 +00:00
case EXITPROG:
current_command = (COMMAND *)NULL;
2000-03-17 21:46:59 +00:00
if (exit_immediately_on_error)
variable_context = 0; /* not in a function */
1996-12-23 17:02:34 +00:00
EOF_Reached = EOF;
goto exec_done;
case DISCARD:
2009-01-12 13:36:28 +00:00
/* Make sure the exit status is reset to a non-zero value, but
leave existing non-zero values (e.g., > 128 on signal)
alone. */
if (last_command_exit_value == 0)
last_command_exit_value = EXECUTION_FAILURE;
1996-12-23 17:02:34 +00:00
if (subshell_environment)
{
current_command = (COMMAND *)NULL;
EOF_Reached = EOF;
goto exec_done;
}
/* Obstack free command elements, etc. */
if (current_command)
{
dispose_command (current_command);
current_command = (COMMAND *)NULL;
}
#if defined (HAVE_POSIX_SIGNALS)
sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
#endif
1996-12-23 17:02:34 +00:00
break;
default:
1999-02-19 17:11:39 +00:00
command_error ("reader_loop", CMDERR_BADJUMP, code, 0);
1996-12-23 17:02:34 +00:00
}
}
executing = 0;
2002-07-17 14:10:11 +00:00
if (temporary_env)
dispose_used_env_vars ();
1996-12-23 17:02:34 +00:00
#if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
/* Attempt to reclaim memory allocated with alloca (). */
(void) alloca (0);
#endif
if (read_command () == 0)
{
if (interactive_shell == 0 && read_but_dont_execute)
{
last_command_exit_value = EXECUTION_SUCCESS;
dispose_command (global_command);
global_command = (COMMAND *)NULL;
}
else if (current_command = global_command)
{
global_command = (COMMAND *)NULL;
current_command_number++;
executing = 1;
1997-06-05 14:59:13 +00:00
stdin_redir = 0;
/* If the shell is interactive, expand and display $PS0 after reading a
command (possibly a list or pipeline) and before executing it. */
if (interactive && ps0_prompt)
{
char *ps0_string;
ps0_string = decode_prompt_string (ps0_prompt);
if (ps0_string && *ps0_string)
{
fprintf (stderr, "%s", ps0_string);
fflush (stderr);
}
free (ps0_string);
}
if (current_command->type != cm_simple)
execute_command (current_command);
else
{
/* Need to make a SCM array of strings. */
WORD_LIST *words;
SCM a = SCM_EOL;
for (words = current_command->value.Simple->words;
words;
words = words->next)
a = scm_cons (scm_from_locale_string (words->word->word),
a);
a = scm_reverse (a);
/* !!! Need to check return from scm_c_lookup. */
SCM x = scm_call_1
(scm_variable_ref
(scm_c_lookup ("BASH:process-command-line")),
a);
if (! scm_is_true (x))
execute_command_ (current_command);
else
{
for (;;)
{
SCM r = scm_call_0
(scm_variable_ref
(scm_c_lookup ("BASH:next-command")));
if (scm_is_false (r))
break;
/* Now we have to compose a new list of words in
* current_command. */
for (words = current_command->value.Simple->words;
words;)
{
WORD_LIST *w = words->next;
/* !!! I think there is some sub-structure
* to this which also needs free'ing. */
free (words);
words = w;
}
WORD_LIST **words;
for (words = &current_command->value.Simple->words;
;
)
{
if (scm_is_null (r))
break;
*words = (WORD_LIST*) malloc (sizeof (WORD_LIST));
(*words)->next = NULL;
(*words)->word = (WORD_DESC*) malloc (sizeof (WORD_DESC));
(*words)->word->flags = 0;
(*words)->word->word = scm_to_locale_string (scm_car (r));
r = scm_cdr (r);
words = &((*words)->next);
}
execute_command_ (current_command);
}
}
}
1996-12-23 17:02:34 +00:00
exec_done:
2005-12-07 14:08:12 +00:00
QUIT;
1996-12-23 17:02:34 +00:00
if (current_command)
2001-04-06 19:14:31 +00:00
{
1996-12-23 17:02:34 +00:00
dispose_command (current_command);
current_command = (COMMAND *)NULL;
2001-04-06 19:14:31 +00:00
}
1996-12-23 17:02:34 +00:00
}
}
else
{
/* Parse error, maybe discard rest of stream if not interactive. */
if (interactive == 0)
EOF_Reached = EOF;
}
if (just_one_command)
EOF_Reached = EOF;
}
indirection_level--;
return (last_command_exit_value);
}
static sighandler
alrm_catcher(i)
int i;
{
2004-07-27 13:29:18 +00:00
printf (_("\007timed out waiting for input: auto-logout\n"));
2009-01-12 13:36:28 +00:00
fflush (stdout);
2004-07-27 13:29:18 +00:00
bash_logout (); /* run ~/.bash_logout if this is a login shell */
1996-12-23 17:02:34 +00:00
jump_to_top_level (EXITPROG);
SIGRETURN (0);
}
/* Send an escape sequence to emacs term mode to tell it the
current working directory. */
static void
send_pwd_to_eterm ()
{
char *pwd, *f;
1996-12-23 17:02:34 +00:00
f = 0;
1996-12-23 17:02:34 +00:00
pwd = get_string_value ("PWD");
if (pwd == 0)
f = pwd = get_working_directory ("eterm");
1996-12-23 17:02:34 +00:00
fprintf (stderr, "\032/%s\n", pwd);
free (f);
1996-12-23 17:02:34 +00:00
}
/* Call the YACC-generated parser and return the status of the parse.
Input is read from the current input stream (bash_input). yyparse
leaves the parsed command in the global variable GLOBAL_COMMAND.
This is where PROMPT_COMMAND is executed. */
int
parse_command ()
{
int r;
char *command_to_execute;
need_here_doc = 0;
run_pending_traps ();
/* Allow the execution of a random command just before the printing
of each primary prompt. If the shell variable PROMPT_COMMAND
is set then the value of it is the command to execute. */
/* The tests are a combination of SHOULD_PROMPT() and prompt_again()
from parse.y, which are the conditions under which the prompt is
actually printed. */
if (interactive && bash_input.type != st_string && parser_expanding_alias() == 0)
1996-12-23 17:02:34 +00:00
{
command_to_execute = get_string_value ("PROMPT_COMMAND");
if (command_to_execute)
2006-10-10 14:15:34 +00:00
execute_variable_command (command_to_execute, "PROMPT_COMMAND");
1996-12-23 17:02:34 +00:00
if (running_under_emacs == 2)
send_pwd_to_eterm (); /* Yuck */
}
current_command_line_count = 0;
r = yyparse ();
if (need_here_doc)
gather_here_documents ();
return (r);
}
/* Read and parse a command, returning the status of the parse. The command
is left in the globval variable GLOBAL_COMMAND for use by reader_loop.
This is where the shell timeout code is executed. */
int
read_command ()
{
SHELL_VAR *tmout_var;
int tmout_len, result;
SigHandler *old_alrm;
2000-03-17 21:46:59 +00:00
set_current_prompt_level (1);
1996-12-23 17:02:34 +00:00
global_command = (COMMAND *)NULL;
/* Only do timeouts if interactive. */
tmout_var = (SHELL_VAR *)NULL;
tmout_len = 0;
2001-11-13 17:56:06 +00:00
old_alrm = (SigHandler *)NULL;
1996-12-23 17:02:34 +00:00
if (interactive)
{
tmout_var = find_variable ("TMOUT");
2002-07-17 14:10:11 +00:00
if (tmout_var && var_isset (tmout_var))
1996-12-23 17:02:34 +00:00
{
2002-07-17 14:10:11 +00:00
tmout_len = atoi (value_cell (tmout_var));
1996-12-23 17:02:34 +00:00
if (tmout_len > 0)
{
old_alrm = set_signal_handler (SIGALRM, alrm_catcher);
alarm (tmout_len);
}
}
}
QUIT;
current_command_line_count = 0;
result = parse_command ();
if (interactive && tmout_var && (tmout_len > 0))
{
alarm(0);
set_signal_handler (SIGALRM, old_alrm);
}
return (result);
}