Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: localtraps



On Mon, Apr 25, 2005 at 04:32:02PM +0000, Bart Schaefer wrote:
> On Apr 25,  1:35am, Vincent Stemen wrote:
> } Subject: localtraps
> } 
> } I don't seem to be able to get the localtraps option to work.
> 
> Try it where the outer function is NOT a trap for the same signal
> that you're trapping inside the function.
> 
> I suspect that changing the trap of the same signal that is trapped
> is confusing the trap code, because low-level signal handling already
> requires that the signal handler restore itself before exiting, so in 
> effect you're restoring multiple different traps, and the order in
> which function exit and trap unwinding are processed is mixed up.

I have some new information.

I had hoped that I could get around the problem for now by explicitly
resetting the signal trap without using localtraps but, as you
indicated, it will not let me do anything with the same signal while
it is in the signal handler function.  This is a real problem that I
don't know how to get around.  Here is a new snippet of test code that
does basically what I am trying to do perfectly under the BSD shell.

#--------------------------------
#!/bin/sh

abort()
{
  echo 
  echo '** Aborting program **'
  echo 

  exit 1
}

signal()
{
  local signal=$1

  if [ -n "$script" ]
  then
      trap abort INT  
      echo -e "\n$0: $script Interrupted.  Hit ^C again to abort everything."
      sleep 2
      echo -n "Continuing "
      trap 'signal INT' INT  # Reset the signal trap for ^C
      return
  else
      abort
  fi
}


script=foo
trap 'signal' INT

echo -n "Doing something important "
for n in 1 2 3 4 5 6 7 8 9
do
    echo -n "."
    sleep 1
done

echo
echo "Exiting normally"
echo
#--------------------------------

Here is the output using the BSD /bin/sh:

# sigtest
Doing something important ..^C
sigtest: foo Interrupted.  Hit ^C again to abort everything.
Continuing ..^C
sigtest: foo Interrupted.  Hit ^C again to abort everything.
^C
** Aborting program **


After the first time I hit ^C, I waited more than 2 seconds.
The second time I hit ^C twice and it called abort() as I wanted.


Trying this under Z shell, I get

# sigtest
Doing something important ..^C
sigtest6: foo Interrupted.  Hit ^C again to abort everything.
^CContinuing
sigtest6: foo Interrupted.  Hit ^C again to abort everything.
^CContinuing
sigtest6: foo Interrupted.  Hit ^C again to abort everything.
Continuing .......
Exiting normally


Since it ignores the trap statement in signal() I have no way of
aborting.

I am using Z shell in all the scripts I am working on because it is
generally so vastly better that bourne shell, but this is a real
handycap.

Any ideas?


-- 
Vincent Stemen
Avoid the VeriSign/Network Solutions domain registration trap!
Read how Network Solutions (NSI) was involved in stealing our domain name.
http://www.InetAddresses.net



Messages sorted by: Reverse Date, Date, Thread, Author