[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Portability problems in autoconf manual

From: Eric Blake
Subject: Re: Portability problems in autoconf manual
Date: Wed, 10 Jun 2009 06:41:21 -0600
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20090302 Thunderbird/ Mnenhy/

Hash: SHA1

According to Ralf Wildenhues on 6/8/2009 2:59 PM:
> If I understand this correctly, then there are one, or even two ways to
> portably reset traps to their default value: either use reset only one
> signal at a time:
>   trap 1; trap 2; trap 13; trap 15

Remember, POSIX 2001 specified that you use either 0 arguments or 2 or
more, but left the use of exactly one argument undefined.  Only POSIX 2008
introduced the possibility of a syntactically valid use of exactly 1
argument.  But you are certainly on to something; it looks like dash does
indeed recognize a 1-argument trap call as a reset:

$ dash -c 'trap echo 0; trap; trap 0; trap; echo done'
trap -- 'echo' EXIT

As do most other shells that I tested: Solaris /bin/sh, bash, zsh, pdksh,
ash.  However, I did find an exception:

$ posh -c 'trap : 0; trap; trap 0; trap; echo done'
trap -- : EXIT
posh: trap: no signals specified
$ echo $?

But posh is already the oddball, as (depending on the version) it requires
names instead of numbers (although I know that has already been brought up
with the debian folks).

> or factor the code based on whether the shell groks resetting of traps
> with a numeric first argument (and assuming it will accept '-'
> otherwise):

You are on to something here.  Notice that we don't even have to worry
about whether 0 is a command or alias, if we insist on doing things one
trap at a time and rely on the syntax error of posh as the determining
factor instead:

if (trap 0) 2>/dev/null ; then
  trap 0; trap 1; trap 2
  trap - 0 1 2

In my testing, this worked for Solaris /bin/sh, dash, bash, zsh, pdksh,
and posh, modulo posh's need for signal names.  Anyone have a
counterexample, before I publish this as a portable way to reset traps?

Unfortunately, we cannot wrap this logic in an ease-of-use function:

# trap_default TRAP[...]
# ----------------------
# For each TRAP number, reset the trap to its default state.
if (trap 0) 2>/dev/null ; then
  for num
    trap $num
  trap - $*

since using trap inside a function does not properly affect the outer
environment, on at least zsh (even with emulate sh).

But we COULD make an AS_TRAP_DEFAULT macro that uses m4 expansion time to
break a list into multiple calls as needed.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             address@hidden
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at
Comment: Using GnuPG with Mozilla -


reply via email to

[Prev in Thread] Current Thread [Next in Thread]