i-bash/builtins/trap.def

269 lines
7 KiB
Modula-2
Raw Normal View History

1996-08-26 18:22:31 +00:00
This file is trap.def, from which is created trap.c.
It implements the builtin "trap" in Bash.
2006-10-10 14:15:34 +00:00
Copyright (C) 1987-2006 Free Software Foundation, Inc.
1996-08-26 18:22:31 +00:00
This file is part of GNU Bash, the Bourne Again SHell.
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
2000-03-17 21:46:59 +00:00
Software Foundation; either version 2, or (at your option) any later
1996-08-26 18:22:31 +00:00
version.
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.
You should have received a copy of the GNU General Public License along
with Bash; see the file COPYING. If not, write to the Free Software
2000-03-17 21:46:59 +00:00
Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
1996-08-26 18:22:31 +00:00
$PRODUCES trap.c
$BUILTIN trap
$FUNCTION trap_builtin
2004-11-09 21:37:25 +00:00
$SHORT_DOC trap [-lp] [arg signal_spec ...]
1996-08-26 18:22:31 +00:00
The command ARG is to be read and executed when the shell receives
2004-07-27 13:29:18 +00:00
signal(s) SIGNAL_SPEC. If ARG is absent (and a single SIGNAL_SPEC
is supplied) or `-', each specified signal is reset to its original
value. If ARG is the null string each SIGNAL_SPEC is ignored by the
shell and by the commands it invokes. If a SIGNAL_SPEC is EXIT (0)
the command ARG is executed on exit from the shell. If a SIGNAL_SPEC
is DEBUG, ARG is executed after every simple command. If the`-p' option
is supplied then the trap commands associated with each SIGNAL_SPEC are
displayed. If no arguments are supplied or if only `-p' is given, trap
prints the list of commands associated with each signal. Each SIGNAL_SPEC
is either a signal name in <signal.h> or a signal number. Signal names
are case insensitive and the SIG prefix is optional. `trap -l' prints
a list of signal names and their corresponding numbers. Note that a
signal can be sent to the shell with "kill -signal $$".
1996-08-26 18:22:31 +00:00
$END
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 "../bashtypes.h"
1996-08-26 18:22:31 +00:00
#include <signal.h>
1996-12-23 17:02:34 +00:00
#include <stdio.h>
#include "../bashansi.h"
1996-08-26 18:22:31 +00:00
#include "../shell.h"
#include "../trap.h"
#include "common.h"
1996-12-23 17:02:34 +00:00
#include "bashgetopt.h"
2001-11-13 17:56:06 +00:00
static void showtrap __P((int));
static int display_traps __P((WORD_LIST *));
1996-08-26 18:22:31 +00:00
/* The trap command:
trap <arg> <signal ...>
trap <signal ...>
trap -l
1996-12-23 17:02:34 +00:00
trap -p [sigspec ...]
1996-08-26 18:22:31 +00:00
trap [--]
Set things up so that ARG is executed when SIGNAL(s) N is recieved.
If ARG is the empty string, then ignore the SIGNAL(s). If there is
no ARG, then set the trap for SIGNAL(s) to its original value. Just
plain "trap" means to print out the list of commands associated with
each signal number. Single arg of "-l" means list the signal names. */
/* Possible operations to perform on the list of signals.*/
#define SET 0 /* Set this signal to first_arg. */
#define REVERT 1 /* Revert to this signals original value. */
#define IGNORE 2 /* Ignore this signal. */
2001-11-13 17:56:06 +00:00
extern int posixly_correct;
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
int
1996-08-26 18:22:31 +00:00
trap_builtin (list)
WORD_LIST *list;
{
2006-10-10 14:15:34 +00:00
int list_signal_names, display, result, opt;
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
list_signal_names = display = 0;
result = EXECUTION_SUCCESS;
reset_internal_getopt ();
while ((opt = internal_getopt (list, "lp")) != -1)
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
switch (opt)
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
case 'l':
1996-08-26 18:22:31 +00:00
list_signal_names++;
break;
1996-12-23 17:02:34 +00:00
case 'p':
display++;
break;
default:
builtin_usage ();
1996-08-26 18:22:31 +00:00
return (EX_USAGE);
}
}
1996-12-23 17:02:34 +00:00
list = loptend;
1996-08-26 18:22:31 +00:00
2004-07-27 13:29:18 +00:00
opt = DSIG_NOCASE|DSIG_SIGPREFIX; /* flags for decode_signal */
1996-08-26 18:22:31 +00:00
if (list_signal_names)
1996-12-23 17:02:34 +00:00
return (display_signal_list ((WORD_LIST *)NULL, 1));
else if (display || list == 0)
return (display_traps (list));
else
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
char *first_arg;
2004-11-09 21:37:25 +00:00
int operation, sig, first_signal;
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
operation = SET;
first_arg = list->word->word;
2004-11-09 21:37:25 +00:00
first_signal = first_arg && *first_arg && all_digits (first_arg) && signal_object_p (first_arg, opt);
2006-10-10 14:15:34 +00:00
/* Backwards compatibility. XXX - question about whether or not we
should throw an error if an all-digit argument doesn't correspond
to a valid signal number (e.g., if it's `50' on a system with only
32 signals). */
2004-11-09 21:37:25 +00:00
if (first_signal)
operation = REVERT;
2004-07-27 13:29:18 +00:00
/* When in posix mode, the historical behavior of looking for a
missing first argument is disabled. To revert to the original
signal handling disposition, use `-' as the first argument. */
2004-11-09 21:37:25 +00:00
else if (posixly_correct == 0 && first_arg && *first_arg &&
2004-07-27 13:29:18 +00:00
(*first_arg != '-' || first_arg[1]) &&
signal_object_p (first_arg, opt) && list->next == 0)
1996-08-26 18:22:31 +00:00
operation = REVERT;
else
{
list = list->next;
2004-07-27 13:29:18 +00:00
if (list == 0)
{
builtin_usage ();
return (EX_USAGE);
}
else if (*first_arg == '\0')
1996-08-26 18:22:31 +00:00
operation = IGNORE;
else if (first_arg[0] == '-' && !first_arg[1])
operation = REVERT;
}
while (list)
{
2004-07-27 13:29:18 +00:00
sig = decode_signal (list->word->word, opt);
1996-08-26 18:22:31 +00:00
if (sig == NO_SIG)
{
2002-07-17 14:10:11 +00:00
sh_invalidsig (list->word->word);
1996-12-23 17:02:34 +00:00
result = EXECUTION_FAILURE;
1996-08-26 18:22:31 +00:00
}
else
{
switch (operation)
{
case SET:
set_signal (sig, first_arg);
break;
case REVERT:
restore_default_signal (sig);
/* Signals that the shell treats specially need special
handling. */
switch (sig)
{
case SIGINT:
if (interactive)
set_signal_handler (SIGINT, sigint_sighandler);
else
2006-10-10 14:15:34 +00:00
set_signal_handler (SIGINT, termsig_sighandler);
1996-08-26 18:22:31 +00:00
break;
case SIGQUIT:
/* Always ignore SIGQUIT. */
set_signal_handler (SIGQUIT, SIG_IGN);
break;
case SIGTERM:
#if defined (JOB_CONTROL)
case SIGTTIN:
case SIGTTOU:
case SIGTSTP:
#endif /* JOB_CONTROL */
if (interactive)
set_signal_handler (sig, SIG_IGN);
break;
}
break;
case IGNORE:
ignore_signal (sig);
break;
}
}
list = list->next;
}
}
1996-12-23 17:02:34 +00:00
return (result);
}
static void
showtrap (i)
int i;
{
1997-09-22 20:22:27 +00:00
char *t, *p, *sn;
1996-12-23 17:02:34 +00:00
p = trap_list[i];
if (p == (char *)DEFAULT_SIG)
return;
2001-04-06 19:14:31 +00:00
t = (p == (char *)IGNORE_SIG) ? (char *)NULL : sh_single_quote (p);
1997-09-22 20:22:27 +00:00
sn = signal_name (i);
/* Make sure that signals whose names are unknown (for whatever reason)
are printed as signal numbers. */
if (STREQN (sn, "SIGJUNK", 7) || STREQN (sn, "unknown", 7))
printf ("trap -- %s %d\n", t ? t : "''", i);
2001-04-06 19:14:31 +00:00
else if (posixly_correct)
{
if (STREQN (sn, "SIG", 3))
printf ("trap -- %s %s\n", t ? t : "''", sn+3);
else
printf ("trap -- %s %s\n", t ? t : "''", sn);
}
1997-09-22 20:22:27 +00:00
else
printf ("trap -- %s %s\n", t ? t : "''", sn);
FREE (t);
1996-12-23 17:02:34 +00:00
}
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
static int
display_traps (list)
WORD_LIST *list;
{
int result, i;
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
if (list == 0)
{
2001-11-13 17:56:06 +00:00
for (i = 0; i < BASH_NSIG; i++)
1996-12-23 17:02:34 +00:00
showtrap (i);
return (EXECUTION_SUCCESS);
}
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
for (result = EXECUTION_SUCCESS; list; list = list->next)
{
2004-07-27 13:29:18 +00:00
i = decode_signal (list->word->word, DSIG_NOCASE|DSIG_SIGPREFIX);
1996-12-23 17:02:34 +00:00
if (i == NO_SIG)
{
2002-07-17 14:10:11 +00:00
sh_invalidsig (list->word->word);
1996-12-23 17:02:34 +00:00
result = EXECUTION_FAILURE;
}
else
showtrap (i);
1996-08-26 18:22:31 +00:00
}
1996-12-23 17:02:34 +00:00
return (result);
1996-08-26 18:22:31 +00:00
}