home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / sigtrap.pm < prev    next >
Text File  |  1997-05-18  |  8KB  |  294 lines

  1. package sigtrap;
  2.  
  3. =head1 NAME
  4.  
  5. sigtrap - Perl pragma to enable simple signal handling
  6.  
  7. =cut
  8.  
  9. use Carp;
  10.  
  11. $VERSION = 1.02;
  12. $Verbose ||= 0;
  13.  
  14. sub import {
  15.     my $pkg = shift;
  16.     my $handler = \&handler_traceback;
  17.     my $saw_sig = 0;
  18.     my $untrapped = 0;
  19.     local $_;
  20.  
  21.   Arg_loop:
  22.     while (@_) {
  23.     $_ = shift;
  24.     if (/^[A-Z][A-Z0-9]*$/) {
  25.         $saw_sig++;
  26.         unless ($untrapped and $SIG{$_} and $SIG{$_} ne 'DEFAULT') {
  27.         print "Installing handler $handler for $_\n" if $Verbose;
  28.         $SIG{$_} = $handler;
  29.         }
  30.     }
  31.     elsif ($_ eq 'normal-signals') {
  32.         unshift @_, grep(exists $SIG{$_}, qw(HUP INT PIPE TERM));
  33.     }
  34.     elsif ($_ eq 'error-signals') {
  35.         unshift @_, grep(exists $SIG{$_},
  36.                  qw(ABRT BUS EMT FPE ILL QUIT SEGV SYS TRAP));
  37.     }
  38.     elsif ($_ eq 'old-interface-signals') {
  39.         unshift @_,
  40.         grep(exists $SIG{$_},
  41.          qw(ABRT BUS EMT FPE ILL PIPE QUIT SEGV SYS TERM TRAP));
  42.     }
  43.         elsif ($_ eq 'stack-trace') {
  44.         $handler = \&handler_traceback;
  45.     }
  46.     elsif ($_ eq 'die') {
  47.         $handler = \&handler_die;
  48.     }
  49.     elsif ($_ eq 'handler') {
  50.         @_ or croak "No argument specified after 'handler'";
  51.         $handler = shift;
  52.         unless (ref $handler or $handler eq 'IGNORE'
  53.             or $handler eq 'DEFAULT') {
  54.                 require Symbol;
  55.         $handler = Symbol::qualify($handler, (caller)[0]);
  56.         }
  57.     }
  58.     elsif ($_ eq 'untrapped') {
  59.         $untrapped = 1;
  60.     }
  61.     elsif ($_ eq 'any') {
  62.         $untrapped = 0;
  63.     }
  64.     elsif ($_ =~ /^\d/) {
  65.         $VERSION >= $_ or croak "sigtrap.pm version $_ required,"
  66.                                 . " but this is only version $VERSION";
  67.     }
  68.     else {
  69.         croak "Unrecognized argument $_";
  70.     }
  71.     }
  72.     unless ($saw_sig) {
  73.     @_ = qw(old-interface-signals);
  74.     goto Arg_loop;
  75.     }
  76. }
  77.  
  78. sub handler_die {
  79.     croak "Caught a SIG$_[0]";
  80. }
  81.  
  82. sub handler_traceback {
  83.     package DB;        # To get subroutine args.
  84.     $SIG{'ABRT'} = DEFAULT;
  85.     kill 'ABRT', $$ if $panic++;
  86.     syswrite(STDERR, 'Caught a SIG', 12);
  87.     syswrite(STDERR, $_[0], length($_[0]));
  88.     syswrite(STDERR, ' at ', 4);
  89.     ($pack,$file,$line) = caller;
  90.     syswrite(STDERR, $file, length($file));
  91.     syswrite(STDERR, ' line ', 6);
  92.     syswrite(STDERR, $line, length($line));
  93.     syswrite(STDERR, "\n", 1);
  94.  
  95.     # Now go for broke.
  96.     for ($i = 1; ($p,$f,$l,$s,$h,$w,$e,$r) = caller($i); $i++) {
  97.         @a = ();
  98.     for $arg (@args) {
  99.         $_ = "$arg";
  100.         s/([\'\\])/\\$1/g;
  101.         s/([^\0]*)/'$1'/
  102.           unless /^(?: -?[\d.]+ | \*[\w:]* )$/x;
  103.         s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  104.         s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  105.         push(@a, $_);
  106.     }
  107.     $w = $w ? '@ = ' : '$ = ';
  108.     $a = $h ? '(' . join(', ', @a) . ')' : '';
  109.     $e =~ s/\n\s*\;\s*\Z// if $e;
  110.     $e =~ s/[\\\']/\\$1/g if $e;
  111.     if ($r) {
  112.         $s = "require '$e'";
  113.     } elsif (defined $r) {
  114.         $s = "eval '$e'";
  115.     } elsif ($s eq '(eval)') {
  116.         $s = "eval {...}";
  117.     }
  118.     if ($^O eq 'MacOS') {
  119.         $mess = "# $w$s$a called from:\nFile '$f'; Line $l\n";
  120.     } else {
  121.         $f = "file `$f'" unless $f eq '-e';
  122.         $mess = "$w$s$a called from $f line $l\n";
  123.     }
  124.     syswrite(STDERR, $mess, length($mess));
  125.     }
  126.     kill 'ABRT', $$;
  127. }
  128.  
  129. 1;
  130.  
  131. __END__
  132.  
  133. =head1 SYNOPSIS
  134.  
  135.     use sigtrap;
  136.     use sigtrap qw(stack-trace old-interface-signals);    # equivalent
  137.     use sigtrap qw(BUS SEGV PIPE ABRT);
  138.     use sigtrap qw(die INT QUIT);
  139.     use sigtrap qw(die normal-signals);
  140.     use sigtrap qw(die untrapped normal-signals);
  141.     use sigtrap qw(die untrapped normal-signals
  142.             stack-trace any error-signals);
  143.     use sigtrap 'handler' => \&my_handler, 'normal-signals';
  144.     use sigtrap qw(handler my_handler normal-signals
  145.                     stack-trace error-signals);
  146.  
  147. =head1 DESCRIPTION
  148.  
  149. The B<sigtrap> pragma is a simple interface to installing signal
  150. handlers.  You can have it install one of two handlers supplied by
  151. B<sigtrap> itself (one which provides a Perl stack trace and one which
  152. simply C<die()>s), or alternately you can supply your own handler for it
  153. to install.  It can be told only to install a handler for signals which
  154. are either untrapped or ignored.  It has a couple of lists of signals to
  155. trap, plus you can supply your own list of signals.
  156.  
  157. The arguments passed to the C<use> statement which invokes B<sigtrap>
  158. are processed in order.  When a signal name or the name of one of
  159. B<sigtrap>'s signal lists is encountered a handler is immediately
  160. installed, when an option is encountered it affects subsequently
  161. installed handlers.
  162.  
  163. =head1 OPTIONS
  164.  
  165. =head2 SIGNAL HANDLERS
  166.  
  167. These options affect which handler will be used for subsequently
  168. installed signals.
  169.  
  170. =over 4
  171.  
  172. =item B<stack-trace>
  173.  
  174. The handler used for subsequently installed signals outputs a Perl stack
  175. trace to STDERR and then tries to dump core.  This is the default signal
  176. handler.
  177.  
  178. =item B<die>
  179.  
  180. The handler used for subsequently installed signals calls C<die>
  181. (actually C<croak>) with a message indicating which signal was caught.
  182.  
  183. =item B<handler> I<your-handler>
  184.  
  185. I<your-handler> will be used as the handler for subsequently installed
  186. signals.  I<your-handler> can be any value which is valid as an
  187. assignment to an element of C<%SIG>.
  188.  
  189. =back
  190.  
  191. =head2 SIGNAL LISTS
  192.  
  193. B<sigtrap> has a few built-in lists of signals to trap.  They are:
  194.  
  195. =over 4
  196.  
  197. =item B<normal-signals>
  198.  
  199. These are the signals which a program might normally expect to encounter
  200. and which by default cause it to terminate.  They are HUP, INT, PIPE and
  201. TERM.
  202.  
  203. =item B<error-signals>
  204.  
  205. These signals usually indicate a serious problem with the Perl
  206. interpreter or with your script.  They are ABRT, BUS, EMT, FPE, ILL,
  207. QUIT, SEGV, SYS and TRAP.
  208.  
  209. =item B<old-interface-signals>
  210.  
  211. These are the signals which were trapped by default by the old
  212. B<sigtrap> interface, they are ABRT, BUS, EMT, FPE, ILL, PIPE, QUIT,
  213. SEGV, SYS, TERM, and TRAP.  If no signals or signals lists are passed to
  214. B<sigtrap>, this list is used.
  215.  
  216. =back
  217.  
  218. For each of these three lists, the collection of signals set to be
  219. trapped is checked before trapping; if your architecture does not
  220. implement a particular signal, it will not be trapped but rather
  221. silently ignored.
  222.  
  223. =head2 OTHER
  224.  
  225. =over 4
  226.  
  227. =item B<untrapped>
  228.  
  229. This token tells B<sigtrap> to install handlers only for subsequently
  230. listed signals which aren't already trapped or ignored.
  231.  
  232. =item B<any>
  233.  
  234. This token tells B<sigtrap> to install handlers for all subsequently
  235. listed signals.  This is the default behavior.
  236.  
  237. =item I<signal>
  238.  
  239. Any argument which looks like a signal name (that is,
  240. C</^[A-Z][A-Z0-9]*$/>) indicates that B<sigtrap> should install a
  241. handler for that name.
  242.  
  243. =item I<number>
  244.  
  245. Require that at least version I<number> of B<sigtrap> is being used.
  246.  
  247. =back
  248.  
  249. =head1 EXAMPLES
  250.  
  251. Provide a stack trace for the old-interface-signals:
  252.  
  253.     use sigtrap;
  254.  
  255. Ditto:
  256.  
  257.     use sigtrap qw(stack-trace old-interface-signals);
  258.  
  259. Provide a stack trace on the 4 listed signals only:
  260.  
  261.     use sigtrap qw(BUS SEGV PIPE ABRT);
  262.  
  263. Die on INT or QUIT:
  264.  
  265.     use sigtrap qw(die INT QUIT);
  266.  
  267. Die on HUP, INT, PIPE or TERM:
  268.  
  269.     use sigtrap qw(die normal-signals);
  270.  
  271. Die on HUP, INT, PIPE or TERM, except don't change the behavior for
  272. signals which are already trapped or ignored:
  273.  
  274.     use sigtrap qw(die untrapped normal-signals);
  275.  
  276. Die on receipt one of an of the B<normal-signals> which is currently
  277. B<untrapped>, provide a stack trace on receipt of B<any> of the
  278. B<error-signals>:
  279.  
  280.     use sigtrap qw(die untrapped normal-signals
  281.             stack-trace any error-signals);
  282.  
  283. Install my_handler() as the handler for the B<normal-signals>:
  284.  
  285.     use sigtrap 'handler', \&my_handler, 'normal-signals';
  286.  
  287. Install my_handler() as the handler for the normal-signals, provide a
  288. Perl stack trace on receipt of one of the error-signals:
  289.  
  290.     use sigtrap qw(handler my_handler normal-signals
  291.                     stack-trace error-signals);
  292.  
  293. =cut
  294.