/* trap.c -- Not the trap command, but useful functions for manipulating
those objects. The trap command is in builtins/trap.def. */
-/* Copyright (C) 1987-2009 Free Software Foundation, Inc.
+/* Copyright (C) 1987-2010 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
static int _run_trap_internal __P((int, char *));
+static void free_trap_string __P((int));
static void reset_signal __P((int));
static void restore_signal __P((int));
static void reset_or_restore_signal_handlers __P((sh_resetsig_func_t *));
extern procenv_t wait_intr_buf;
extern int return_catch_flag, return_catch_value;
extern int subshell_level;
+extern WORD_LIST *subst_assign_varlist;
/* The list of things to do originally, before we started trapping. */
SigHandler *original_signals[NSIG];
/* The (trapped) signal received while executing in the `wait' builtin */
int wait_signal_received;
-/* A value which can never be the target of a trap handler. */
-#define IMPOSSIBLE_TRAP_HANDLER (SigHandler *)initialize_traps
-
#define GETORIGSIG(sig) \
do { \
original_signals[sig] = (SigHandler *)set_signal_handler (sig, SIG_DFL); \
sigmodes[sig] |= SIG_HARD_IGNORE; \
} while (0)
+#define SETORIGSIG(sig,handler) \
+ do { \
+ original_signals[sig] = handler; \
+ if (original_signals[sig] == SIG_IGN) \
+ sigmodes[sig] |= SIG_HARD_IGNORE; \
+ } while (0)
+
#define GET_ORIGINAL_SIGNAL(sig) \
if (sig && sig < NSIG && original_signals[sig] == IMPOSSIBLE_TRAP_HANDLER) \
GETORIGSIG(sig)
{
pending_traps[i] = 0;
trap_list[i] = (char *)DEFAULT_SIG;
- sigmodes[i] = SIG_INHERITED;
+ sigmodes[i] = SIG_INHERITED; /* XXX - only set, not used */
original_signals[i] = IMPOSSIBLE_TRAP_HANDLER;
}
{
register int sig;
int old_exit_value, *token_state;
+ WORD_LIST *save_subst_varlist;
+#if defined (ARRAY_VARS)
+ ARRAY *ps;
+#endif
if (catch_flag == 0) /* simple optimization */
return;
/* Preserve $? when running trap. */
old_exit_value = last_command_exit_value;
+#if defined (ARRAY_VARS)
+ ps = save_pipestatus_array ();
+#endif
for (sig = 1; sig < NSIG; sig++)
{
else
{
token_state = save_token_state ();
+ save_subst_varlist = subst_assign_varlist;
+ subst_assign_varlist = 0;
+
parse_and_execute (savestring (trap_list[sig]), "trap", SEVAL_NONINT|SEVAL_NOHIST|SEVAL_RESETLINE);
restore_token_state (token_state);
free (token_state);
+
+ subst_assign_varlist = save_subst_varlist;
}
pending_traps[sig] = 0;
}
}
+#if defined (ARRAY_VARS)
+ restore_pipestatus_array (ps);
+#endif
last_command_exit_value = old_exit_value;
}
int sig;
{
/* If we aren't sure the of the original value, then get it. */
- if (original_signals[sig] == (SigHandler *)IMPOSSIBLE_TRAP_HANDLER)
+ if (sig > 0 && sig < NSIG && original_signals[sig] == (SigHandler *)IMPOSSIBLE_TRAP_HANDLER)
GETORIGSIG (sig);
}
+void
+get_all_original_signals ()
+{
+ register int i;
+
+ for (i = 1; i < NSIG; i++)
+ GET_ORIGINAL_SIGNAL (i);
+}
+
+void
+set_original_signal (sig, handler)
+ int sig;
+ SigHandler *handler;
+{
+ if (sig > 0 && sig < NSIG && original_signals[sig] == (SigHandler *)IMPOSSIBLE_TRAP_HANDLER)
+ SETORIGSIG (sig, handler);
+}
+
/* Restore the default action for SIG; i.e., the action the shell
would have taken before you used the trap command. This is called
from trap_builtin (), which takes care to restore the handlers for
{
char *trap_command;
int code, function_code, retval;
+#if defined (ARRAY_VARS)
+ ARRAY *ps;
+#endif
trap_saved_exit_value = last_command_exit_value;
+#if defined (ARRAY_VARS)
+ ps = save_pipestatus_array ();
+#endif
function_code = 0;
/* Run the trap only if signal 0 is trapped and not ignored, and we are not
return retval;
}
+#if defined (ARRAY_VARS)
+ restore_pipestatus_array (ps);
+#endif
return (trap_saved_exit_value);
}
int trap_exit_value, *token_state;
int save_return_catch_flag, function_code, flags;
procenv_t save_return_catch;
+ WORD_LIST *save_subst_varlist;
+#if defined (ARRAY_VARS)
+ ARRAY *ps;
+#endif
trap_exit_value = function_code = 0;
/* Run the trap only if SIG is trapped and not ignored, and we are not
running_trap = sig + 1;
trap_saved_exit_value = last_command_exit_value;
+#if defined (ARRAY_VARS)
+ ps = save_pipestatus_array ();
+#endif
token_state = save_token_state ();
+ save_subst_varlist = subst_assign_varlist;
+ subst_assign_varlist = 0;
/* If we're in a function, make sure return longjmps come here, too. */
save_return_catch_flag = return_catch_flag;
restore_token_state (token_state);
free (token_state);
+ subst_assign_varlist = save_subst_varlist;
+
trap_exit_value = last_command_exit_value;
last_command_exit_value = trap_saved_exit_value;
+#if defined (ARRAY_VARS)
+ restore_pipestatus_array (ps);
+#endif
running_trap = 0;
sigmodes[sig] &= ~SIG_INPROGRESS;
_run_trap_internal (SIGINT, "interrupt trap");
}
-#ifdef INCLUDE_UNUSED
/* Free all the allocated strings in the list of traps and reset the trap
- values to the default. */
+ values to the default. Intended to be called from subshells that want
+ to complete work done by reset_signal_handlers upon execution of a
+ subsequent `trap' command that changes a signal's disposition. */
void
free_trap_strings ()
{
register int i;
for (i = 0; i < BASH_NSIG; i++)
- {
- free_trap_command (i);
- trap_list[i] = (char *)DEFAULT_SIG;
- sigmodes[i] &= ~SIG_TRAPPED;
- }
+ free_trap_string (i);
trap_list[DEBUG_TRAP] = trap_list[EXIT_TRAP] = trap_list[ERROR_TRAP] = trap_list[RETURN_TRAP] = (char *)NULL;
}
-#endif
-/* Reset the handler for SIG to the original value. */
+/* Free a trap command string associated with SIG without changing signal
+ disposition. Intended to be called from free_trap_strings() */
+static void
+free_trap_string (sig)
+ int sig;
+{
+ change_signal (sig, (char *)DEFAULT_SIG);
+ sigmodes[sig] &= ~SIG_TRAPPED;
+}
+
+/* Reset the handler for SIG to the original value but leave the trap string
+ in place. */
static void
reset_signal (sig)
int sig;
}
/* Reset trapped signals to their original values, but don't free the
- trap strings. Called by the command substitution code. */
+ trap strings. Called by the command substitution code and other places
+ that create a "subshell environment". */
void
reset_signal_handlers ()
{
return (sigmodes[sig] & SIG_IGNORED);
}
+int
+signal_is_hard_ignored (sig)
+ int sig;
+{
+ return (sigmodes[sig] & SIG_HARD_IGNORE);
+}
+
void
set_signal_ignored (sig)
int sig;