commit bash-20070726 snapshot

This commit is contained in:
Chet Ramey
2011-12-07 09:10:50 -05:00
parent aea33a6b25
commit 7a131ef23d
13 changed files with 5263 additions and 5 deletions
+29
View File
@@ -14784,3 +14784,32 @@ variables.c
lib/sh/getcwd.c
- #undef HAVE_LSTAT on qnx, so it uses stat instead. Patch from
Sean Boudreau <seanb@qnx.com>
7/21
----
builtins/common.c
- change sh_invalidnum to be a little smarter about octal and hex
numbers and change the message appropriately. Bug originally
reported on coreutils list by Jürgen Niinre<Jyrgen.Niinre@emt.ee>
7/26
----
test.c
- make sure the string passed to test_unop has only a single character
following the `-'. Fixes bug reported by Michael A. Smith
<michael@smith-li.com>
parse.y
- better input validation: make sure a word looks like a conditional
unary operator (-X) before calling test_unop
7/28
----
trap.c
- in trap_handler, if it's called directly from the signal handler
(e.g., SIGINT sighandler, set by set_sigint_handler), but the
trap disposition has been reset to the default between the
assignment and receipt of the signal, check that the signal is
trapped and issue a warning if the shell was compiled with
debugging enabled. Fixes bug reported by Fergus Henderson
<fergus@google.com>
+22
View File
@@ -14780,3 +14780,25 @@ lib/readline/display.c
variables.c
- use native __QNX__ and __QNXNTO__ cpp defines instead of qnx and
qnx6, respectively. Patch from Sean Boudreau <seanb@qnx.com>
lib/sh/getcwd.c
- #undef HAVE_LSTAT on qnx, so it uses stat instead. Patch from
Sean Boudreau <seanb@qnx.com>
7/21
----
builtins/common.c
- change sh_invalidnum to be a little smarter about octal and hex
numbers and change the message appropriately. Bug originally
reported on coreutils list by Jürgen Niinre<Jyrgen.Niinre@emt.ee>
7/26
----
test.c
- make sure the string passed to test_unop has only a single character
following the `-'. Fixes bug reported by Michael A. Smith
<michael@smith-li.com>
parse.y
- better input validation: make sure a word looks like a conditional
unary operator (-X) before calling test_unop
+2
View File
@@ -468,6 +468,8 @@ po/en@quot.po f
po/en@boldquot.po f
po/en@quot.gmo f
po/en@boldquot.gmo f
po/bg.po f
po/bg.gmo f
po/ru.po f
po/ru.gmo f
po/sv.po f
+9 -1
View File
@@ -199,7 +199,15 @@ void
sh_invalidnum (s)
char *s;
{
builtin_error (_("%s: invalid number"), s);
char *msg;
if (*s == '0' && isdigit (s[1]))
msg = _("invalid octal number");
else if (*s == '0' && s[1] == 'x')
msg = _("invalid hex number");
else
msg = _("invalid number");
builtin_error ("%s: %s", s, msg);
}
void
+1
View File
@@ -1,4 +1,5 @@
/* true and false builtins */
#include <config.h>
#include "bashtypes.h"
#include "shell.h"
+1 -1
View File
@@ -3173,7 +3173,7 @@ cond_term ()
if (term)
term->flags |= CMD_INVERT_RETURN;
}
else if (tok == WORD && test_unop (yylval.word->word))
else if (tok == WORD && yylval.word->word[0] == '-' && yylval.word->word[2] == 0 && test_unop (yylval.word->word))
{
op = yylval.word;
tok = read_token (READ);
-1
View File
@@ -4795,7 +4795,6 @@ parse_compound_assignment (retlenp)
{
last_command_exit_value = EXECUTION_FAILURE;
last_read_token = '\n'; /* XXX */
top_level_cleanup ();
if (interactive_shell == 0 && posixly_correct)
jump_to_top_level (FORCE_EOF);
else
+1 -1
View File
@@ -1,2 +1,2 @@
# Set of available languages.
en@quot en@boldquot ru sv
en@quot en@boldquot ru sv bg
+3375
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -659,7 +659,7 @@ int
test_unop (op)
char *op;
{
if (op[0] != '-')
if (op[0] != '-' || op[2] != 0)
return (0);
switch (op[1])
+825
View File
@@ -0,0 +1,825 @@
/* GNU test program (ksb and mjb) */
/* Modified to run with the GNU shell Apr 25, 1988 by bfox. */
/* Copyright (C) 1987-2005 Free Software Foundation, Inc.
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
Software Foundation; either version 2, or (at your option) any later
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
Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
/* Define PATTERN_MATCHING to get the csh-like =~ and !~ pattern-matching
binary operators. */
/* #define PATTERN_MATCHING */
#if defined (HAVE_CONFIG_H)
# include <config.h>
#endif
#include <stdio.h>
#include "bashtypes.h"
#if !defined (HAVE_LIMITS_H)
# include <sys/param.h>
#endif
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include <errno.h>
#if !defined (errno)
extern int errno;
#endif /* !errno */
#if !defined (_POSIX_VERSION) && defined (HAVE_SYS_FILE_H)
# include <sys/file.h>
#endif /* !_POSIX_VERSION */
#include "posixstat.h"
#include "filecntl.h"
#include "bashintl.h"
#include "shell.h"
#include "pathexp.h"
#include "test.h"
#include "builtins/common.h"
#include <glob/strmatch.h>
#if !defined (STRLEN)
# define STRLEN(s) ((s)[0] ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
#endif
#if !defined (STREQ)
# define STREQ(a, b) ((a)[0] == (b)[0] && strcmp (a, b) == 0)
#endif /* !STREQ */
#if !defined (R_OK)
#define R_OK 4
#define W_OK 2
#define X_OK 1
#define F_OK 0
#endif /* R_OK */
#define EQ 0
#define NE 1
#define LT 2
#define GT 3
#define LE 4
#define GE 5
#define NT 0
#define OT 1
#define EF 2
/* The following few defines control the truth and false output of each stage.
TRUE and FALSE are what we use to compute the final output value.
SHELL_BOOLEAN is the form which returns truth or falseness in shell terms.
Default is TRUE = 1, FALSE = 0, SHELL_BOOLEAN = (!value). */
#define TRUE 1
#define FALSE 0
#define SHELL_BOOLEAN(value) (!(value))
#define TEST_ERREXIT_STATUS 2
static procenv_t test_exit_buf;
static int test_error_return;
#define test_exit(val) \
do { test_error_return = val; longjmp (test_exit_buf, 1); } while (0)
extern int sh_stat __P((const char *, struct stat *));
static int pos; /* The offset of the current argument in ARGV. */
static int argc; /* The number of arguments present in ARGV. */
static char **argv; /* The argument list. */
static int noeval;
static void test_syntax_error __P((char *, char *)) __attribute__((__noreturn__));
static void beyond __P((void)) __attribute__((__noreturn__));
static void integer_expected_error __P((char *)) __attribute__((__noreturn__));
static int unary_operator __P((void));
static int binary_operator __P((void));
static int two_arguments __P((void));
static int three_arguments __P((void));
static int posixtest __P((void));
static int expr __P((void));
static int term __P((void));
static int and __P((void));
static int or __P((void));
static int filecomp __P((char *, char *, int));
static int arithcomp __P((char *, char *, int, int));
static int patcomp __P((char *, char *, int));
static void
test_syntax_error (format, arg)
char *format, *arg;
{
builtin_error (format, arg);
test_exit (TEST_ERREXIT_STATUS);
}
/*
* beyond - call when we're beyond the end of the argument list (an
* error condition)
*/
static void
beyond ()
{
test_syntax_error (_("argument expected"), (char *)NULL);
}
/* Syntax error for when an integer argument was expected, but
something else was found. */
static void
integer_expected_error (pch)
char *pch;
{
test_syntax_error (_("%s: integer expression expected"), pch);
}
/* Increment our position in the argument list. Check that we're not
past the end of the argument list. This check is supressed if the
argument is FALSE. Made a macro for efficiency. */
#define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0)
#define unary_advance() do { advance (1); ++pos; } while (0)
/*
* expr:
* or
*/
static int
expr ()
{
if (pos >= argc)
beyond ();
return (FALSE ^ or ()); /* Same with this. */
}
/*
* or:
* and
* and '-o' or
*/
static int
or ()
{
int value, v2;
value = and ();
if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2])
{
advance (0);
v2 = or ();
return (value || v2);
}
return (value);
}
/*
* and:
* term
* term '-a' and
*/
static int
and ()
{
int value, v2;
value = term ();
if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2])
{
advance (0);
v2 = and ();
return (value && v2);
}
return (value);
}
/*
* term - parse a term and return 1 or 0 depending on whether the term
* evaluates to true or false, respectively.
*
* term ::=
* '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'k'|'p'|'r'|'s'|'u'|'w'|'x') filename
* '-'('G'|'L'|'O'|'S'|'N') filename
* '-t' [int]
* '-'('z'|'n') string
* '-o' option
* string
* string ('!='|'='|'==') string
* <int> '-'(eq|ne|le|lt|ge|gt) <int>
* file '-'(nt|ot|ef) file
* '(' <expr> ')'
* int ::=
* positive and negative integers
*/
static int
term ()
{
int value;
if (pos >= argc)
beyond ();
/* Deal with leading `not's. */
if (argv[pos][0] == '!' && argv[pos][1] == '\0')
{
value = 0;
while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0')
{
advance (1);
value = 1 - value;
}
return (value ? !term() : term());
}
/* A paren-bracketed argument. */
if (argv[pos][0] == '(' && argv[pos][1] == '\0') /* ) */
{
advance (1);
value = expr ();
if (argv[pos] == 0) /* ( */
test_syntax_error (_("`)' expected"), (char *)NULL);
else if (argv[pos][0] != ')' || argv[pos][1]) /* ( */
test_syntax_error (_("`)' expected, found %s"), argv[pos]);
advance (0);
return (value);
}
/* are there enough arguments left that this could be dyadic? */
if ((pos + 3 <= argc) && test_binop (argv[pos + 1]))
value = binary_operator ();
/* Might be a switch type argument */
else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
{
if (test_unop (argv[pos]))
value = unary_operator ();
else
test_syntax_error (_("%s: unary operator expected"), argv[pos]);
}
else
{
value = argv[pos][0] != '\0';
advance (0);
}
return (value);
}
static int
filecomp (s, t, op)
char *s, *t;
int op;
{
struct stat st1, st2;
int r1, r2;
if ((r1 = sh_stat (s, &st1)) < 0)
{
if (op == EF)
return (FALSE);
}
if ((r2 = sh_stat (t, &st2)) < 0)
{
if (op == EF)
return (FALSE);
}
switch (op)
{
case OT: return (r1 < r2 || (r2 == 0 && st1.st_mtime < st2.st_mtime));
case NT: return (r1 > r2 || (r1 == 0 && st1.st_mtime > st2.st_mtime));
case EF: return ((st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino));
}
return (FALSE);
}
static int
arithcomp (s, t, op, flags)
char *s, *t;
int op, flags;
{
intmax_t l, r;
int expok;
if (flags & TEST_ARITHEXP)
{
l = evalexp (s, &expok);
if (expok == 0)
return (FALSE); /* should probably longjmp here */
r = evalexp (t, &expok);
if (expok == 0)
return (FALSE); /* ditto */
}
else
{
if (legal_number (s, &l) == 0)
integer_expected_error (s);
if (legal_number (t, &r) == 0)
integer_expected_error (t);
}
switch (op)
{
case EQ: return (l == r);
case NE: return (l != r);
case LT: return (l < r);
case GT: return (l > r);
case LE: return (l <= r);
case GE: return (l >= r);
}
return (FALSE);
}
static int
patcomp (string, pat, op)
char *string, *pat;
int op;
{
int m;
m = strmatch (pat, string, FNMATCH_EXTFLAG|FNMATCH_IGNCASE);
return ((op == EQ) ? (m == 0) : (m != 0));
}
int
binary_test (op, arg1, arg2, flags)
char *op, *arg1, *arg2;
int flags;
{
int patmatch;
patmatch = (flags & TEST_PATMATCH);
if (op[0] == '=' && (op[1] == '\0' || (op[1] == '=' && op[2] == '\0')))
return (patmatch ? patcomp (arg1, arg2, EQ) : STREQ (arg1, arg2));
else if ((op[0] == '>' || op[0] == '<') && op[1] == '\0')
return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0));
else if (op[0] == '!' && op[1] == '=' && op[2] == '\0')
return (patmatch ? patcomp (arg1, arg2, NE) : (STREQ (arg1, arg2) == 0));
else if (op[2] == 't')
{
switch (op[1])
{
case 'n': return (filecomp (arg1, arg2, NT)); /* -nt */
case 'o': return (filecomp (arg1, arg2, OT)); /* -ot */
case 'l': return (arithcomp (arg1, arg2, LT, flags)); /* -lt */
case 'g': return (arithcomp (arg1, arg2, GT, flags)); /* -gt */
}
}
else if (op[1] == 'e')
{
switch (op[2])
{
case 'f': return (filecomp (arg1, arg2, EF)); /* -ef */
case 'q': return (arithcomp (arg1, arg2, EQ, flags)); /* -eq */
}
}
else if (op[2] == 'e')
{
switch (op[1])
{
case 'n': return (arithcomp (arg1, arg2, NE, flags)); /* -ne */
case 'g': return (arithcomp (arg1, arg2, GE, flags)); /* -ge */
case 'l': return (arithcomp (arg1, arg2, LE, flags)); /* -le */
}
}
return (FALSE); /* should never get here */
}
static int
binary_operator ()
{
int value;
char *w;
w = argv[pos + 1];
if ((w[0] == '=' && (w[1] == '\0' || (w[1] == '=' && w[2] == '\0'))) || /* =, == */
((w[0] == '>' || w[0] == '<') && w[1] == '\0') || /* <, > */
(w[0] == '!' && w[1] == '=' && w[2] == '\0')) /* != */
{
value = binary_test (w, argv[pos], argv[pos + 2], 0);
pos += 3;
return (value);
}
#if defined (PATTERN_MATCHING)
if ((w[0] == '=' || w[0] == '!') && w[1] == '~' && w[2] == '\0')
{
value = patcomp (argv[pos], argv[pos + 2], w[0] == '=' ? EQ : NE);
pos += 3;
return (value);
}
#endif
if ((w[0] != '-' || w[3] != '\0') || test_binop (w) == 0)
{
test_syntax_error (_("%s: binary operator expected"), w);
/* NOTREACHED */
return (FALSE);
}
value = binary_test (w, argv[pos], argv[pos + 2], 0);
pos += 3;
return value;
}
static int
unary_operator ()
{
char *op;
intmax_t r;
op = argv[pos];
if (test_unop (op) == 0)
return (FALSE);
/* the only tricky case is `-t', which may or may not take an argument. */
if (op[1] == 't')
{
advance (0);
if (pos < argc)
{
if (legal_number (argv[pos], &r))
{
advance (0);
return (unary_test (op, argv[pos - 1]));
}
else
return (FALSE);
}
else
return (unary_test (op, "1"));
}
/* All of the unary operators take an argument, so we first call
unary_advance (), which checks to make sure that there is an
argument, and then advances pos right past it. This means that
pos - 1 is the location of the argument. */
unary_advance ();
return (unary_test (op, argv[pos - 1]));
}
int
unary_test (op, arg)
char *op, *arg;
{
intmax_t r;
struct stat stat_buf;
switch (op[1])
{
case 'a': /* file exists in the file system? */
case 'e':
return (sh_stat (arg, &stat_buf) == 0);
case 'r': /* file is readable? */
return (sh_eaccess (arg, R_OK) == 0);
case 'w': /* File is writeable? */
return (sh_eaccess (arg, W_OK) == 0);
case 'x': /* File is executable? */
return (sh_eaccess (arg, X_OK) == 0);
case 'O': /* File is owned by you? */
return (sh_stat (arg, &stat_buf) == 0 &&
(uid_t) current_user.euid == (uid_t) stat_buf.st_uid);
case 'G': /* File is owned by your group? */
return (sh_stat (arg, &stat_buf) == 0 &&
(gid_t) current_user.egid == (gid_t) stat_buf.st_gid);
case 'N':
return (sh_stat (arg, &stat_buf) == 0 &&
stat_buf.st_atime <= stat_buf.st_mtime);
case 'f': /* File is a file? */
if (sh_stat (arg, &stat_buf) < 0)
return (FALSE);
/* -f is true if the given file exists and is a regular file. */
#if defined (S_IFMT)
return (S_ISREG (stat_buf.st_mode) || (stat_buf.st_mode & S_IFMT) == 0);
#else
return (S_ISREG (stat_buf.st_mode));
#endif /* !S_IFMT */
case 'd': /* File is a directory? */
return (sh_stat (arg, &stat_buf) == 0 && (S_ISDIR (stat_buf.st_mode)));
case 's': /* File has something in it? */
return (sh_stat (arg, &stat_buf) == 0 && stat_buf.st_size > (off_t) 0);
case 'S': /* File is a socket? */
#if !defined (S_ISSOCK)
return (FALSE);
#else
return (sh_stat (arg, &stat_buf) == 0 && S_ISSOCK (stat_buf.st_mode));
#endif /* S_ISSOCK */
case 'c': /* File is character special? */
return (sh_stat (arg, &stat_buf) == 0 && S_ISCHR (stat_buf.st_mode));
case 'b': /* File is block special? */
return (sh_stat (arg, &stat_buf) == 0 && S_ISBLK (stat_buf.st_mode));
case 'p': /* File is a named pipe? */
#ifndef S_ISFIFO
return (FALSE);
#else
return (sh_stat (arg, &stat_buf) == 0 && S_ISFIFO (stat_buf.st_mode));
#endif /* S_ISFIFO */
case 'L': /* Same as -h */
case 'h': /* File is a symbolic link? */
#if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
return (FALSE);
#else
return ((arg[0] != '\0') &&
(lstat (arg, &stat_buf) == 0) && S_ISLNK (stat_buf.st_mode));
#endif /* S_IFLNK && HAVE_LSTAT */
case 'u': /* File is setuid? */
return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISUID) != 0);
case 'g': /* File is setgid? */
return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISGID) != 0);
case 'k': /* File has sticky bit set? */
#if !defined (S_ISVTX)
/* This is not Posix, and is not defined on some Posix systems. */
return (FALSE);
#else
return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISVTX) != 0);
#endif
case 't': /* File fd is a terminal? */
if (legal_number (arg, &r) == 0)
return (FALSE);
return ((r == (int)r) && isatty ((int)r));
case 'n': /* True if arg has some length. */
return (arg[0] != '\0');
case 'z': /* True if arg has no length. */
return (arg[0] == '\0');
case 'o': /* True if option `arg' is set. */
return (minus_o_option_value (arg) == 1);
}
/* We can't actually get here, but this shuts up gcc. */
return (FALSE);
}
/* Return TRUE if OP is one of the test command's binary operators. */
int
test_binop (op)
char *op;
{
if (op[0] == '=' && op[1] == '\0')
return (1); /* '=' */
else if ((op[0] == '<' || op[0] == '>') && op[1] == '\0') /* string <, > */
return (1);
else if ((op[0] == '=' || op[0] == '!') && op[1] == '=' && op[2] == '\0')
return (1); /* `==' and `!=' */
#if defined (PATTERN_MATCHING)
else if (op[2] == '\0' && op[1] == '~' && (op[0] == '=' || op[0] == '!'))
return (1);
#endif
else if (op[0] != '-' || op[2] == '\0' || op[3] != '\0')
return (0);
else
{
if (op[2] == 't')
switch (op[1])
{
case 'n': /* -nt */
case 'o': /* -ot */
case 'l': /* -lt */
case 'g': /* -gt */
return (1);
default:
return (0);
}
else if (op[1] == 'e')
switch (op[2])
{
case 'q': /* -eq */
case 'f': /* -ef */
return (1);
default:
return (0);
}
else if (op[2] == 'e')
switch (op[1])
{
case 'n': /* -ne */
case 'g': /* -ge */
case 'l': /* -le */
return (1);
default:
return (0);
}
else
return (0);
}
}
/* Return non-zero if OP is one of the test command's unary operators. */
int
test_unop (op)
char *op;
{
if (op[0] != '-')
return (0);
switch (op[1])
{
case 'a': case 'b': case 'c': case 'd': case 'e':
case 'f': case 'g': case 'h': case 'k': case 'n':
case 'o': case 'p': case 'r': case 's': case 't':
case 'u': case 'w': case 'x': case 'z':
case 'G': case 'L': case 'O': case 'S': case 'N':
return (1);
}
return (0);
}
static int
two_arguments ()
{
if (argv[pos][0] == '!' && argv[pos][1] == '\0')
return (argv[pos + 1][0] == '\0');
else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
{
if (test_unop (argv[pos]))
return (unary_operator ());
else
test_syntax_error (_("%s: unary operator expected"), argv[pos]);
}
else
test_syntax_error (_("%s: unary operator expected"), argv[pos]);
return (0);
}
#define ANDOR(s) (s[0] == '-' && !s[2] && (s[1] == 'a' || s[1] == 'o'))
/* This could be augmented to handle `-t' as equivalent to `-t 1', but
POSIX requires that `-t' be given an argument. */
#define ONE_ARG_TEST(s) ((s)[0] != '\0')
static int
three_arguments ()
{
int value;
if (test_binop (argv[pos+1]))
{
value = binary_operator ();
pos = argc;
}
else if (ANDOR (argv[pos+1]))
{
if (argv[pos+1][1] == 'a')
value = ONE_ARG_TEST(argv[pos]) && ONE_ARG_TEST(argv[pos+2]);
else
value = ONE_ARG_TEST(argv[pos]) || ONE_ARG_TEST(argv[pos+2]);
pos = argc;
}
else if (argv[pos][0] == '!' && argv[pos][1] == '\0')
{
advance (1);
value = !two_arguments ();
}
else if (argv[pos][0] == '(' && argv[pos+2][0] == ')')
{
value = ONE_ARG_TEST(argv[pos+1]);
pos = argc;
}
else
test_syntax_error (_("%s: binary operator expected"), argv[pos+1]);
return (value);
}
/* This is an implementation of a Posix.2 proposal by David Korn. */
static int
posixtest ()
{
int value;
switch (argc - 1) /* one extra passed in */
{
case 0:
value = FALSE;
pos = argc;
break;
case 1:
value = ONE_ARG_TEST(argv[1]);
pos = argc;
break;
case 2:
value = two_arguments ();
pos = argc;
break;
case 3:
value = three_arguments ();
break;
case 4:
if (argv[pos][0] == '!' && argv[pos][1] == '\0')
{
advance (1);
value = !three_arguments ();
break;
}
/* FALLTHROUGH */
default:
value = expr ();
}
return (value);
}
/*
* [:
* '[' expr ']'
* test:
* test expr
*/
int
test_command (margc, margv)
int margc;
char **margv;
{
int value;
int code;
USE_VAR(margc);
code = setjmp (test_exit_buf);
if (code)
return (test_error_return);
argv = margv;
if (margv[0] && margv[0][0] == '[' && margv[0][1] == '\0')
{
--margc;
if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1]))
test_syntax_error (_("missing `]'"), (char *)NULL);
if (margc < 2)
test_exit (SHELL_BOOLEAN (FALSE));
}
argc = margc;
pos = 1;
if (pos >= argc)
test_exit (SHELL_BOOLEAN (FALSE));
noeval = 0;
value = posixtest ();
if (pos != argc)
test_syntax_error (_("too many arguments"), (char *)NULL);
test_exit (SHELL_BOOLEAN (value));
}
+8
View File
@@ -349,6 +349,14 @@ trap_handler (sig)
{
int oerrno;
if ((sigmodes[sig] & SIG_TRAPPED) == 0)
{
#if defined (DEBUG)
internal_warning ("trap_handler: signal %d: signal not trapped", sig);
#endif
SIGRETURN (0);
}
if ((sig >= NSIG) ||
(trap_list[sig] == (char *)DEFAULT_SIG) ||
(trap_list[sig] == (char *)IGNORE_SIG))
+989
View File
@@ -0,0 +1,989 @@
/* trap.c -- Not the trap command, but useful functions for manipulating
those objects. The trap command is in builtins/trap.def. */
/* Copyright (C) 1987-2006 Free Software Foundation, Inc.
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
Software Foundation; either version 2, or (at your option) any later
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
Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
#include "config.h"
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include "bashtypes.h"
#include "bashansi.h"
#include <stdio.h>
#include <errno.h>
#include "bashintl.h"
#include "trap.h"
#include "shell.h"
#include "flags.h"
#include "input.h" /* for save_token_state, restore_token_state */
#include "signames.h"
#include "builtins.h"
#include "builtins/common.h"
#include "builtins/builtext.h"
#ifndef errno
extern int errno;
#endif
/* Flags which describe the current handling state of a signal. */
#define SIG_INHERITED 0x0 /* Value inherited from parent. */
#define SIG_TRAPPED 0x1 /* Currently trapped. */
#define SIG_HARD_IGNORE 0x2 /* Signal was ignored on shell entry. */
#define SIG_SPECIAL 0x4 /* Treat this signal specially. */
#define SIG_NO_TRAP 0x8 /* Signal cannot be trapped. */
#define SIG_INPROGRESS 0x10 /* Signal handler currently executing. */
#define SIG_CHANGED 0x20 /* Trap value changed in trap handler. */
#define SIG_IGNORED 0x40 /* The signal is currently being ignored. */
#define SPECIAL_TRAP(s) ((s) == EXIT_TRAP || (s) == DEBUG_TRAP || (s) == ERROR_TRAP || (s) == RETURN_TRAP)
/* An array of such flags, one for each signal, describing what the
shell will do with a signal. DEBUG_TRAP == NSIG; some code below
assumes this. */
static int sigmodes[BASH_NSIG];
static void free_trap_command __P((int));
static void change_signal __P((int, char *));
static void get_original_signal __P((int));
static int _run_trap_internal __P((int, char *));
static void reset_signal __P((int));
static void restore_signal __P((int));
static void reset_or_restore_signal_handlers __P((sh_resetsig_func_t *));
/* Variables used here but defined in other files. */
extern int last_command_exit_value;
extern int line_number;
extern char *this_command_name;
extern sh_builtin_func_t *this_shell_builtin;
extern procenv_t wait_intr_buf;
extern int return_catch_flag, return_catch_value;
extern int subshell_level;
/* The list of things to do originally, before we started trapping. */
SigHandler *original_signals[NSIG];
/* For each signal, a slot for a string, which is a command to be
executed when that signal is recieved. The slot can also contain
DEFAULT_SIG, which means do whatever you were going to do before
you were so rudely interrupted, or IGNORE_SIG, which says ignore
this signal. */
char *trap_list[BASH_NSIG];
/* A bitmap of signals received for which we have trap handlers. */
int pending_traps[NSIG];
/* Set to the number of the signal we're running the trap for + 1.
Used in execute_cmd.c and builtins/common.c to clean up when
parse_and_execute does not return normally after executing the
trap command (e.g., when `return' is executed in the trap command). */
int running_trap;
/* Set to last_command_exit_value before running a trap. */
int trap_saved_exit_value;
/* 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); \
set_signal_handler (sig, original_signals[sig]); \
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)
void
initialize_traps ()
{
register int i;
initialize_signames();
trap_list[EXIT_TRAP] = trap_list[DEBUG_TRAP] = trap_list[ERROR_TRAP] = trap_list[RETURN_TRAP] = (char *)NULL;
sigmodes[EXIT_TRAP] = sigmodes[DEBUG_TRAP] = sigmodes[ERROR_TRAP] = sigmodes[RETURN_TRAP] = SIG_INHERITED;
original_signals[EXIT_TRAP] = IMPOSSIBLE_TRAP_HANDLER;
for (i = 1; i < NSIG; i++)
{
pending_traps[i] = 0;
trap_list[i] = (char *)DEFAULT_SIG;
sigmodes[i] = SIG_INHERITED;
original_signals[i] = IMPOSSIBLE_TRAP_HANDLER;
}
/* Show which signals are treated specially by the shell. */
#if defined (SIGCHLD)
GETORIGSIG (SIGCHLD);
sigmodes[SIGCHLD] |= (SIG_SPECIAL | SIG_NO_TRAP);
#endif /* SIGCHLD */
GETORIGSIG (SIGINT);
sigmodes[SIGINT] |= SIG_SPECIAL;
#if defined (__BEOS__)
/* BeOS sets SIGINT to SIG_IGN! */
original_signals[SIGINT] = SIG_DFL;
sigmodes[SIGINT] &= ~SIG_HARD_IGNORE;
#endif
GETORIGSIG (SIGQUIT);
sigmodes[SIGQUIT] |= SIG_SPECIAL;
if (interactive)
{
GETORIGSIG (SIGTERM);
sigmodes[SIGTERM] |= SIG_SPECIAL;
}
}
#ifdef INCLUDE_UNUSED
/* Return a printable representation of the trap handler for SIG. */
static char *
trap_handler_string (sig)
int sig;
{
if (trap_list[sig] == (char *)DEFAULT_SIG)
return "DEFAULT_SIG";
else if (trap_list[sig] == (char *)IGNORE_SIG)
return "IGNORE_SIG";
else if (trap_list[sig] == (char *)IMPOSSIBLE_TRAP_HANDLER)
return "IMPOSSIBLE_TRAP_HANDLER";
else if (trap_list[sig])
return trap_list[sig];
else
return "NULL";
}
#endif
/* Return the print name of this signal. */
char *
signal_name (sig)
int sig;
{
char *ret;
/* on cygwin32, signal_names[sig] could be null */
ret = (sig >= BASH_NSIG || sig < 0 || signal_names[sig] == NULL)
? _("invalid signal number")
: signal_names[sig];
return ret;
}
/* Turn a string into a signal number, or a number into
a signal number. If STRING is "2", "SIGINT", or "INT",
then (int)2 is returned. Return NO_SIG if STRING doesn't
contain a valid signal descriptor. */
int
decode_signal (string, flags)
char *string;
int flags;
{
intmax_t sig;
char *name;
if (legal_number (string, &sig))
return ((sig >= 0 && sig < NSIG) ? (int)sig : NO_SIG);
/* A leading `SIG' may be omitted. */
for (sig = 0; sig < BASH_NSIG; sig++)
{
name = signal_names[sig];
if (name == 0 || name[0] == '\0')
continue;
/* Check name without the SIG prefix first case sensitivly or
insensitively depending on whether flags includes DSIG_NOCASE */
if (STREQN (name, "SIG", 3))
{
name += 3;
if ((flags & DSIG_NOCASE) && strcasecmp (string, name) == 0)
return ((int)sig);
else if ((flags & DSIG_NOCASE) == 0 && strcmp (string, name) == 0)
return ((int)sig);
/* If we can't use the `SIG' prefix to match, punt on this
name now. */
else if ((flags & DSIG_SIGPREFIX) == 0)
continue;
}
/* Check name with SIG prefix case sensitively or insensitively
depending on whether flags includes DSIG_NOCASE */
name = signal_names[sig];
if ((flags & DSIG_NOCASE) && strcasecmp (string, name) == 0)
return ((int)sig);
else if ((flags & DSIG_NOCASE) == 0 && strcmp (string, name) == 0)
return ((int)sig);
}
return (NO_SIG);
}
/* Non-zero when we catch a trapped signal. */
static int catch_flag;
void
run_pending_traps ()
{
register int sig;
int old_exit_value, *token_state;
if (catch_flag == 0) /* simple optimization */
return;
catch_flag = 0;
/* Preserve $? when running trap. */
old_exit_value = last_command_exit_value;
for (sig = 1; sig < NSIG; sig++)
{
/* XXX this could be made into a counter by using
while (pending_traps[sig]--) instead of the if statement. */
if (pending_traps[sig])
{
#if defined (HAVE_POSIX_SIGNALS)
sigset_t set, oset;
sigemptyset (&set);
sigemptyset (&oset);
sigaddset (&set, sig);
sigprocmask (SIG_BLOCK, &set, &oset);
#else
# if defined (HAVE_BSD_SIGNALS)
int oldmask = sigblock (sigmask (sig));
# endif
#endif /* HAVE_POSIX_SIGNALS */
if (sig == SIGINT)
{
run_interrupt_trap ();
CLRINTERRUPT;
}
else if (trap_list[sig] == (char *)DEFAULT_SIG ||
trap_list[sig] == (char *)IGNORE_SIG ||
trap_list[sig] == (char *)IMPOSSIBLE_TRAP_HANDLER)
{
/* This is possible due to a race condition. Say a bash
process has SIGTERM trapped. A subshell is spawned
using { list; } & and the parent does something and kills
the subshell with SIGTERM. It's possible for the subshell
to set pending_traps[SIGTERM] to 1 before the code in
execute_cmd.c eventually calls restore_original_signals
to reset the SIGTERM signal handler in the subshell. The
next time run_pending_traps is called, pending_traps[SIGTERM]
will be 1, but the trap handler in trap_list[SIGTERM] will
be invalid (probably DEFAULT_SIG, but it could be IGNORE_SIG).
Unless we catch this, the subshell will dump core when
trap_list[SIGTERM] == DEFAULT_SIG, because DEFAULT_SIG is
usually 0x0. */
internal_warning (_("run_pending_traps: bad value in trap_list[%d]: %p"),
sig, trap_list[sig]);
if (trap_list[sig] == (char *)DEFAULT_SIG)
{
internal_warning (_("run_pending_traps: signal handler is SIG_DFL, resending %d (%s) to myself"), sig, signal_name (sig));
kill (getpid (), sig);
}
}
else
{
token_state = save_token_state ();
parse_and_execute (savestring (trap_list[sig]), "trap", SEVAL_NONINT|SEVAL_NOHIST);
restore_token_state (token_state);
free (token_state);
}
pending_traps[sig] = 0;
#if defined (HAVE_POSIX_SIGNALS)
sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
#else
# if defined (HAVE_BSD_SIGNALS)
sigsetmask (oldmask);
# endif
#endif /* POSIX_VERSION */
}
}
last_command_exit_value = old_exit_value;
}
sighandler
trap_handler (sig)
int sig;
{
int oerrno;
if ((sigmodes[sig] & SIG_TRAPPED) == 0)
{
internal_warning ("trap_handler: signal %d: signal not trapped", sig);
SIGRETURN (0);
}
if ((sig >= NSIG) ||
(trap_list[sig] == (char *)DEFAULT_SIG) ||
(trap_list[sig] == (char *)IGNORE_SIG))
programming_error (_("trap_handler: bad signal %d"), sig);
else
{
oerrno = errno;
#if defined (MUST_REINSTALL_SIGHANDLERS)
set_signal_handler (sig, trap_handler);
#endif /* MUST_REINSTALL_SIGHANDLERS */
catch_flag = 1;
pending_traps[sig]++;
if (interrupt_immediately && this_shell_builtin && (this_shell_builtin == wait_builtin))
{
wait_signal_received = sig;
longjmp (wait_intr_buf, 1);
}
if (interrupt_immediately)
run_pending_traps ();
errno = oerrno;
}
SIGRETURN (0);
}
#if defined (JOB_CONTROL) && defined (SIGCHLD)
#ifdef INCLUDE_UNUSED
/* Make COMMAND_STRING be executed when SIGCHLD is caught. */
void
set_sigchld_trap (command_string)
char *command_string;
{
set_signal (SIGCHLD, command_string);
}
#endif
/* Make COMMAND_STRING be executed when SIGCHLD is caught iff SIGCHLD
is not already trapped. */
void
maybe_set_sigchld_trap (command_string)
char *command_string;
{
if ((sigmodes[SIGCHLD] & SIG_TRAPPED) == 0)
set_signal (SIGCHLD, command_string);
}
#endif /* JOB_CONTROL && SIGCHLD */
void
set_debug_trap (command)
char *command;
{
set_signal (DEBUG_TRAP, command);
}
void
set_error_trap (command)
char *command;
{
set_signal (ERROR_TRAP, command);
}
void
set_return_trap (command)
char *command;
{
set_signal (RETURN_TRAP, command);
}
#ifdef INCLUDE_UNUSED
void
set_sigint_trap (command)
char *command;
{
set_signal (SIGINT, command);
}
#endif
/* Reset the SIGINT handler so that subshells that are doing `shellsy'
things, like waiting for command substitution or executing commands
in explicit subshells ( ( cmd ) ), can catch interrupts properly. */
SigHandler *
set_sigint_handler ()
{
if (sigmodes[SIGINT] & SIG_HARD_IGNORE)
return ((SigHandler *)SIG_IGN);
else if (sigmodes[SIGINT] & SIG_IGNORED)
return ((SigHandler *)set_signal_handler (SIGINT, SIG_IGN)); /* XXX */
else if (sigmodes[SIGINT] & SIG_TRAPPED)
return ((SigHandler *)set_signal_handler (SIGINT, trap_handler));
/* The signal is not trapped, so set the handler to the shell's special
interrupt handler. */
else if (interactive) /* XXX - was interactive_shell */
return (set_signal_handler (SIGINT, sigint_sighandler));
else
return (set_signal_handler (SIGINT, termsig_sighandler));
}
/* Return the correct handler for signal SIG according to the values in
sigmodes[SIG]. */
SigHandler *
trap_to_sighandler (sig)
int sig;
{
if (sigmodes[sig] & (SIG_IGNORED|SIG_HARD_IGNORE))
return (SIG_IGN);
else if (sigmodes[sig] & SIG_TRAPPED)
return (trap_handler);
else
return (SIG_DFL);
}
/* Set SIG to call STRING as a command. */
void
set_signal (sig, string)
int sig;
char *string;
{
if (SPECIAL_TRAP (sig))
{
change_signal (sig, savestring (string));
if (sig == EXIT_TRAP && interactive == 0)
initialize_terminating_signals ();
return;
}
/* A signal ignored on entry to the shell cannot be trapped or reset, but
no error is reported when attempting to do so. -- Posix.2 */
if (sigmodes[sig] & SIG_HARD_IGNORE)
return;
/* Make sure we have original_signals[sig] if the signal has not yet
been trapped. */
if ((sigmodes[sig] & SIG_TRAPPED) == 0)
{
/* If we aren't sure of the original value, check it. */
if (original_signals[sig] == IMPOSSIBLE_TRAP_HANDLER)
GETORIGSIG (sig);
if (original_signals[sig] == SIG_IGN)
return;
}
/* Only change the system signal handler if SIG_NO_TRAP is not set.
The trap command string is changed in either case. The shell signal
handlers for SIGINT and SIGCHLD run the user specified traps in an
environment in which it is safe to do so. */
if ((sigmodes[sig] & SIG_NO_TRAP) == 0)
{
set_signal_handler (sig, SIG_IGN);
change_signal (sig, savestring (string));
set_signal_handler (sig, trap_handler);
}
else
change_signal (sig, savestring (string));
}
static void
free_trap_command (sig)
int sig;
{
if ((sigmodes[sig] & SIG_TRAPPED) && trap_list[sig] &&
(trap_list[sig] != (char *)IGNORE_SIG) &&
(trap_list[sig] != (char *)DEFAULT_SIG) &&
(trap_list[sig] != (char *)IMPOSSIBLE_TRAP_HANDLER))
free (trap_list[sig]);
}
/* If SIG has a string assigned to it, get rid of it. Then give it
VALUE. */
static void
change_signal (sig, value)
int sig;
char *value;
{
if ((sigmodes[sig] & SIG_INPROGRESS) == 0)
free_trap_command (sig);
trap_list[sig] = value;
sigmodes[sig] |= SIG_TRAPPED;
if (value == (char *)IGNORE_SIG)
sigmodes[sig] |= SIG_IGNORED;
else
sigmodes[sig] &= ~SIG_IGNORED;
if (sigmodes[sig] & SIG_INPROGRESS)
sigmodes[sig] |= SIG_CHANGED;
}
static void
get_original_signal (sig)
int sig;
{
/* If we aren't sure the of the original value, then get it. */
if (original_signals[sig] == (SigHandler *)IMPOSSIBLE_TRAP_HANDLER)
GETORIGSIG (sig);
}
/* 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
the signals the shell treats specially. */
void
restore_default_signal (sig)
int sig;
{
if (SPECIAL_TRAP (sig))
{
if ((sig != DEBUG_TRAP && sig != ERROR_TRAP && sig != RETURN_TRAP) ||
(sigmodes[sig] & SIG_INPROGRESS) == 0)
free_trap_command (sig);
trap_list[sig] = (char *)NULL;
sigmodes[sig] &= ~SIG_TRAPPED;
if (sigmodes[sig] & SIG_INPROGRESS)
sigmodes[sig] |= SIG_CHANGED;
return;
}
GET_ORIGINAL_SIGNAL (sig);
/* A signal ignored on entry to the shell cannot be trapped or reset, but
no error is reported when attempting to do so. Thanks Posix.2. */
if (sigmodes[sig] & SIG_HARD_IGNORE)
return;
/* If we aren't trapping this signal, don't bother doing anything else. */
if ((sigmodes[sig] & SIG_TRAPPED) == 0)
return;
/* Only change the signal handler for SIG if it allows it. */
if ((sigmodes[sig] & SIG_NO_TRAP) == 0)
set_signal_handler (sig, original_signals[sig]);
/* Change the trap command in either case. */
change_signal (sig, (char *)DEFAULT_SIG);
/* Mark the signal as no longer trapped. */
sigmodes[sig] &= ~SIG_TRAPPED;
}
/* Make this signal be ignored. */
void
ignore_signal (sig)
int sig;
{
if (SPECIAL_TRAP (sig) && ((sigmodes[sig] & SIG_IGNORED) == 0))
{
change_signal (sig, (char *)IGNORE_SIG);
return;
}
GET_ORIGINAL_SIGNAL (sig);
/* A signal ignored on entry to the shell cannot be trapped or reset.
No error is reported when the user attempts to do so. */
if (sigmodes[sig] & SIG_HARD_IGNORE)
return;
/* If already trapped and ignored, no change necessary. */
if (sigmodes[sig] & SIG_IGNORED)
return;
/* Only change the signal handler for SIG if it allows it. */
if ((sigmodes[sig] & SIG_NO_TRAP) == 0)
set_signal_handler (sig, SIG_IGN);
/* Change the trap command in either case. */
change_signal (sig, (char *)IGNORE_SIG);
}
/* Handle the calling of "trap 0". The only sticky situation is when
the command to be executed includes an "exit". This is why we have
to provide our own place for top_level to jump to. */
int
run_exit_trap ()
{
char *trap_command;
int code, function_code, retval;
trap_saved_exit_value = last_command_exit_value;
function_code = 0;
/* Run the trap only if signal 0 is trapped and not ignored, and we are not
currently running in the trap handler (call to exit in the list of
commands given to trap 0). */
if ((sigmodes[EXIT_TRAP] & SIG_TRAPPED) &&
(sigmodes[EXIT_TRAP] & (SIG_IGNORED|SIG_INPROGRESS)) == 0)
{
trap_command = savestring (trap_list[EXIT_TRAP]);
sigmodes[EXIT_TRAP] &= ~SIG_TRAPPED;
sigmodes[EXIT_TRAP] |= SIG_INPROGRESS;
retval = trap_saved_exit_value;
running_trap = 1;
code = setjmp (top_level);
/* If we're in a function, make sure return longjmps come here, too. */
if (return_catch_flag)
function_code = setjmp (return_catch);
if (code == 0 && function_code == 0)
{
reset_parser ();
parse_and_execute (trap_command, "exit trap", SEVAL_NONINT|SEVAL_NOHIST);
}
else if (code == ERREXIT)
retval = last_command_exit_value;
else if (code == EXITPROG)
retval = last_command_exit_value;
else if (function_code != 0)
retval = return_catch_value;
else
retval = trap_saved_exit_value;
running_trap = 0;
return retval;
}
return (trap_saved_exit_value);
}
void
run_trap_cleanup (sig)
int sig;
{
sigmodes[sig] &= ~(SIG_INPROGRESS|SIG_CHANGED);
}
/* Run a trap command for SIG. SIG is one of the signals the shell treats
specially. Returns the exit status of the executed trap command list. */
static int
_run_trap_internal (sig, tag)
int sig;
char *tag;
{
char *trap_command, *old_trap;
int trap_exit_value, *token_state;
int save_return_catch_flag, function_code;
procenv_t save_return_catch;
trap_exit_value = function_code = 0;
/* Run the trap only if SIG is trapped and not ignored, and we are not
currently executing in the trap handler. */
if ((sigmodes[sig] & SIG_TRAPPED) && ((sigmodes[sig] & SIG_IGNORED) == 0) &&
(trap_list[sig] != (char *)IMPOSSIBLE_TRAP_HANDLER) &&
((sigmodes[sig] & SIG_INPROGRESS) == 0))
{
old_trap = trap_list[sig];
sigmodes[sig] |= SIG_INPROGRESS;
sigmodes[sig] &= ~SIG_CHANGED; /* just to be sure */
trap_command = savestring (old_trap);
running_trap = sig + 1;
trap_saved_exit_value = last_command_exit_value;
token_state = save_token_state ();
/* If we're in a function, make sure return longjmps come here, too. */
save_return_catch_flag = return_catch_flag;
if (return_catch_flag)
{
COPY_PROCENV (return_catch, save_return_catch);
function_code = setjmp (return_catch);
}
if (function_code == 0)
parse_and_execute (trap_command, tag, SEVAL_NONINT|SEVAL_NOHIST);
restore_token_state (token_state);
free (token_state);
trap_exit_value = last_command_exit_value;
last_command_exit_value = trap_saved_exit_value;
running_trap = 0;
sigmodes[sig] &= ~SIG_INPROGRESS;
if (sigmodes[sig] & SIG_CHANGED)
{
#if 0
/* Special traps like EXIT, DEBUG, RETURN are handled explicitly in
the places where they can be changed using unwind-protects. For
example, look at execute_cmd.c:execute_function(). */
if (SPECIAL_TRAP (sig) == 0)
#endif
free (old_trap);
sigmodes[sig] &= ~SIG_CHANGED;
}
if (save_return_catch_flag)
{
return_catch_flag = save_return_catch_flag;
return_catch_value = trap_exit_value;
COPY_PROCENV (save_return_catch, return_catch);
if (function_code)
longjmp (return_catch, 1);
}
}
return trap_exit_value;
}
int
run_debug_trap ()
{
int trap_exit_value;
/* XXX - question: should the DEBUG trap inherit the RETURN trap? */
trap_exit_value = 0;
if ((sigmodes[DEBUG_TRAP] & SIG_TRAPPED) && ((sigmodes[DEBUG_TRAP] & SIG_IGNORED) == 0) && ((sigmodes[DEBUG_TRAP] & SIG_INPROGRESS) == 0))
{
trap_exit_value = _run_trap_internal (DEBUG_TRAP, "debug trap");
#if defined (DEBUGGER)
/* If we're in the debugger and the DEBUG trap returns 2 while we're in
a function or sourced script, we force a `return'. */
if (debugging_mode && trap_exit_value == 2 && return_catch_flag)
{
return_catch_value = trap_exit_value;
longjmp (return_catch, 1);
}
#endif
}
return trap_exit_value;
}
void
run_error_trap ()
{
if ((sigmodes[ERROR_TRAP] & SIG_TRAPPED) && ((sigmodes[ERROR_TRAP] & SIG_IGNORED) == 0) && (sigmodes[ERROR_TRAP] & SIG_INPROGRESS) == 0)
_run_trap_internal (ERROR_TRAP, "error trap");
}
void
run_return_trap ()
{
int old_exit_value;
#if 0
if ((sigmodes[DEBUG_TRAP] & SIG_TRAPPED) && (sigmodes[DEBUG_TRAP] & SIG_INPROGRESS))
return;
#endif
if ((sigmodes[RETURN_TRAP] & SIG_TRAPPED) && ((sigmodes[RETURN_TRAP] & SIG_IGNORED) == 0) && (sigmodes[RETURN_TRAP] & SIG_INPROGRESS) == 0)
{
old_exit_value = last_command_exit_value;
_run_trap_internal (RETURN_TRAP, "return trap");
last_command_exit_value = old_exit_value;
}
}
/* Run a trap set on SIGINT. This is called from throw_to_top_level (), and
declared here to localize the trap functions. */
void
run_interrupt_trap ()
{
_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. */
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;
}
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. */
static void
reset_signal (sig)
int sig;
{
set_signal_handler (sig, original_signals[sig]);
sigmodes[sig] &= ~SIG_TRAPPED;
}
/* Set the handler signal SIG to the original and free any trap
command associated with it. */
static void
restore_signal (sig)
int sig;
{
set_signal_handler (sig, original_signals[sig]);
change_signal (sig, (char *)DEFAULT_SIG);
sigmodes[sig] &= ~SIG_TRAPPED;
}
static void
reset_or_restore_signal_handlers (reset)
sh_resetsig_func_t *reset;
{
register int i;
/* Take care of the exit trap first */
if (sigmodes[EXIT_TRAP] & SIG_TRAPPED)
{
sigmodes[EXIT_TRAP] &= ~SIG_TRAPPED;
if (reset != reset_signal)
{
free_trap_command (EXIT_TRAP);
trap_list[EXIT_TRAP] = (char *)NULL;
}
}
for (i = 1; i < NSIG; i++)
{
if (sigmodes[i] & SIG_TRAPPED)
{
if (trap_list[i] == (char *)IGNORE_SIG)
set_signal_handler (i, SIG_IGN);
else
(*reset) (i);
}
else if (sigmodes[i] & SIG_SPECIAL)
(*reset) (i);
}
/* Command substitution and other child processes don't inherit the
debug, error, or return traps. If we're in the debugger, and the
`functrace' or `errtrace' options have been set, then let command
substitutions inherit them. Let command substitution inherit the
RETURN trap if we're in the debugger and tracing functions. */
if (function_trace_mode == 0)
{
sigmodes[DEBUG_TRAP] &= ~SIG_TRAPPED;
sigmodes[RETURN_TRAP] &= ~SIG_TRAPPED;
}
if (error_trace_mode == 0)
sigmodes[ERROR_TRAP] &= ~SIG_TRAPPED;
}
/* Reset trapped signals to their original values, but don't free the
trap strings. Called by the command substitution code. */
void
reset_signal_handlers ()
{
reset_or_restore_signal_handlers (reset_signal);
}
/* Reset all trapped signals to their original values. Signals set to be
ignored with trap '' SIGNAL should be ignored, so we make sure that they
are. Called by child processes after they are forked. */
void
restore_original_signals ()
{
reset_or_restore_signal_handlers (restore_signal);
}
/* If a trap handler exists for signal SIG, then call it; otherwise just
return failure. */
int
maybe_call_trap_handler (sig)
int sig;
{
/* Call the trap handler for SIG if the signal is trapped and not ignored. */
if ((sigmodes[sig] & SIG_TRAPPED) && ((sigmodes[sig] & SIG_IGNORED) == 0))
{
switch (sig)
{
case SIGINT:
run_interrupt_trap ();
break;
case EXIT_TRAP:
run_exit_trap ();
break;
case DEBUG_TRAP:
run_debug_trap ();
break;
case ERROR_TRAP:
run_error_trap ();
break;
default:
trap_handler (sig);
break;
}
return (1);
}
else
return (0);
}
int
signal_is_trapped (sig)
int sig;
{
return (sigmodes[sig] & SIG_TRAPPED);
}
int
signal_is_special (sig)
int sig;
{
return (sigmodes[sig] & SIG_SPECIAL);
}
int
signal_is_ignored (sig)
int sig;
{
return (sigmodes[sig] & SIG_IGNORED);
}
void
set_signal_ignored (sig)
int sig;
{
sigmodes[sig] |= SIG_HARD_IGNORE;
original_signals[sig] = SIG_IGN;
}
int
signal_in_progress (sig)
int sig;
{
return (sigmodes[sig] & SIG_INPROGRESS);
}