Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
4.1 KiB

  1. package Fatal;
  2. use Carp;
  3. use strict;
  4. use vars qw( $AUTOLOAD $Debug $VERSION);
  5. $VERSION = 1.02;
  6. $Debug = 0 unless defined $Debug;
  7. sub import {
  8. my $self = shift(@_);
  9. my($sym, $pkg);
  10. $pkg = (caller)[0];
  11. foreach $sym (@_) {
  12. &_make_fatal($sym, $pkg);
  13. }
  14. };
  15. sub AUTOLOAD {
  16. my $cmd = $AUTOLOAD;
  17. $cmd =~ s/.*:://;
  18. &_make_fatal($cmd, (caller)[0]);
  19. goto &$AUTOLOAD;
  20. }
  21. sub fill_protos {
  22. my $proto = shift;
  23. my ($n, $isref, @out, @out1, $seen_semi) = -1;
  24. while ($proto =~ /\S/) {
  25. $n++;
  26. push(@out1,[$n,@out]) if $seen_semi;
  27. push(@out, $1 . "{\$_[$n]}"), next if $proto =~ s/^\s*\\([\@%\$\&])//;
  28. push(@out, "\$_[$n]"), next if $proto =~ s/^\s*([*\$&])//;
  29. push(@out, "\@_[$n..\$#_]"), last if $proto =~ s/^\s*(;\s*)?\@//;
  30. $seen_semi = 1, $n--, next if $proto =~ s/^\s*;//; # XXXX ????
  31. die "Unknown prototype letters: \"$proto\"";
  32. }
  33. push(@out1,[$n+1,@out]);
  34. @out1;
  35. }
  36. sub write_invocation {
  37. my ($core, $call, $name, @argvs) = @_;
  38. if (@argvs == 1) { # No optional arguments
  39. my @argv = @{$argvs[0]};
  40. shift @argv;
  41. return "\t" . one_invocation($core, $call, $name, @argv) . ";\n";
  42. } else {
  43. my $else = "\t";
  44. my (@out, @argv, $n);
  45. while (@argvs) {
  46. @argv = @{shift @argvs};
  47. $n = shift @argv;
  48. push @out, "$ {else}if (\@_ == $n) {\n";
  49. $else = "\t} els";
  50. push @out,
  51. "\t\treturn " . one_invocation($core, $call, $name, @argv) . ";\n";
  52. }
  53. push @out, <<EOC;
  54. }
  55. die "$name(\@_): Do not expect to get ", scalar \@_, " arguments";
  56. EOC
  57. return join '', @out;
  58. }
  59. }
  60. sub one_invocation {
  61. my ($core, $call, $name, @argv) = @_;
  62. local $" = ', ';
  63. return qq{$call(@argv) || croak "Can't $name(\@_)} .
  64. ($core ? ': $!' : ', \$! is \"$!\"') . '"';
  65. }
  66. sub _make_fatal {
  67. my($sub, $pkg) = @_;
  68. my($name, $code, $sref, $real_proto, $proto, $core, $call);
  69. my $ini = $sub;
  70. $sub = "${pkg}::$sub" unless $sub =~ /::/;
  71. $name = $sub;
  72. $name =~ s/.*::// or $name =~ s/^&//;
  73. print "# _make_fatal: sub=$sub pkg=$pkg name=$name\n" if $Debug;
  74. croak "Bad subroutine name for Fatal: $name" unless $name =~ /^\w+$/;
  75. if (defined(&$sub)) { # user subroutine
  76. $sref = \&$sub;
  77. $proto = prototype $sref;
  78. $call = '&$sref';
  79. } elsif ($sub eq $ini) { # Stray user subroutine
  80. die "$sub is not a Perl subroutine"
  81. } else { # CORE subroutine
  82. $proto = eval { prototype "CORE::$name" };
  83. die "$name is neither a builtin, nor a Perl subroutine"
  84. if $@;
  85. die "Cannot make a non-overridable builtin fatal"
  86. if not defined $proto;
  87. $core = 1;
  88. $call = "CORE::$name";
  89. }
  90. if (defined $proto) {
  91. $real_proto = " ($proto)";
  92. } else {
  93. $real_proto = '';
  94. $proto = '@';
  95. }
  96. $code = <<EOS;
  97. sub$real_proto {
  98. local(\$", \$!) = (', ', 0);
  99. EOS
  100. my @protos = fill_protos($proto);
  101. $code .= write_invocation($core, $call, $name, @protos);
  102. $code .= "}\n";
  103. print $code if $Debug;
  104. {
  105. no strict 'refs'; # to avoid: Can't use string (...) as a symbol ref ...
  106. $code = eval("package $pkg; use Carp; $code");
  107. die if $@;
  108. local($^W) = 0; # to avoid: Subroutine foo redefined ...
  109. *{$sub} = $code;
  110. }
  111. }
  112. 1;
  113. __END__
  114. =head1 NAME
  115. Fatal - replace functions with equivalents which succeed or die
  116. =head1 SYNOPSIS
  117. use Fatal qw(open close);
  118. sub juggle { . . . }
  119. import Fatal 'juggle';
  120. =head1 DESCRIPTION
  121. C<Fatal> provides a way to conveniently replace functions which normally
  122. return a false value when they fail with equivalents which halt execution
  123. if they are not successful. This lets you use these functions without
  124. having to test their return values explicitly on each call. Errors are
  125. reported via C<die>, so you can trap them using C<$SIG{__DIE__}> if you
  126. wish to take some action before the program exits.
  127. The do-or-die equivalents are set up simply by calling Fatal's
  128. C<import> routine, passing it the names of the functions to be
  129. replaced. You may wrap both user-defined functions and overridable
  130. CORE operators (except C<exec>, C<system> which cannot be expressed
  131. via prototypes) in this way.
  132. =head1 AUTHOR
  133. Lionel.Cons@cern.ch
  134. prototype updates by Ilya Zakharevich ilya@math.ohio-state.edu
  135. =cut