home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_utl.zip / splain.cmd < prev    next >
OS/2 REXX Batch file  |  1997-11-28  |  15KB  |  537 lines

  1. extproc perl -S
  2. #!f:/perllib/bin/perl
  3.     eval 'exec f:/perllib/bin/perl -S $0 ${1+"$@"}'
  4.     if $running_under_some_shell;
  5.  
  6. =head1 NAME
  7.  
  8. diagnostics - Perl compiler pragma to force verbose warning diagnostics
  9.  
  10. splain - standalone program to do the same thing
  11.  
  12. =head1 SYNOPSIS
  13.  
  14. As a pragma:
  15.  
  16.     use diagnostics;
  17.     use diagnostics -verbose;
  18.  
  19.     enable  diagnostics;
  20.     disable diagnostics;
  21.  
  22. Aa a program:
  23.  
  24.     perl program 2>diag.out
  25.     splain [-v] [-p] diag.out
  26.  
  27.  
  28. =head1 DESCRIPTION
  29.  
  30. =head2 The C<diagnostics> Pragma
  31.  
  32. This module extends the terse diagnostics normally emitted by both the
  33. perl compiler and the perl interpeter, augmenting them with the more
  34. explicative and endearing descriptions found in L<perldiag>.  Like the
  35. other pragmata, it affects the compilation phase of your program rather
  36. than merely the execution phase.
  37.  
  38. To use in your program as a pragma, merely invoke
  39.  
  40.     use diagnostics;
  41.  
  42. at the start (or near the start) of your program.  (Note 
  43. that this I<does> enable perl's B<-w> flag.)  Your whole
  44. compilation will then be subject(ed :-) to the enhanced diagnostics.
  45. These still go out B<STDERR>.
  46.  
  47. Due to the interaction between runtime and compiletime issues,
  48. and because it's probably not a very good idea anyway,
  49. you may not use C<no diagnostics> to turn them off at compiletime.
  50. However, you may control there behaviour at runtime using the 
  51. disable() and enable() methods to turn them off and on respectively.
  52.  
  53. The B<-verbose> flag first prints out the L<perldiag> introduction before
  54. any other diagnostics.  The $diagnostics::PRETTY variable can generate nicer
  55. escape sequences for pagers.
  56.  
  57. =head2 The I<splain> Program
  58.  
  59. While apparently a whole nuther program, I<splain> is actually nothing
  60. more than a link to the (executable) F<diagnostics.pm> module, as well as
  61. a link to the F<diagnostics.pod> documentation.  The B<-v> flag is like
  62. the C<use diagnostics -verbose> directive.
  63. The B<-p> flag is like the
  64. $diagnostics::PRETTY variable.  Since you're post-processing with 
  65. I<splain>, there's no sense in being able to enable() or disable() processing.
  66.  
  67. Output from I<splain> is directed to B<STDOUT>, unlike the pragma.
  68.  
  69. =head1 EXAMPLES
  70.  
  71. The following file is certain to trigger a few errors at both
  72. runtime and compiletime:
  73.  
  74.     use diagnostics;
  75.     print NOWHERE "nothing\n";
  76.     print STDERR "\n\tThis message should be unadorned.\n";
  77.     warn "\tThis is a user warning";
  78.     print "\nDIAGNOSTIC TESTER: Please enter a <CR> here: ";
  79.     my $a, $b = scalar <STDIN>;
  80.     print "\n";
  81.     print $x/$y;
  82.  
  83. If you prefer to run your program first and look at its problem
  84. afterwards, do this:
  85.  
  86.     perl -w test.pl 2>test.out
  87.     ./splain < test.out
  88.  
  89. Note that this is not in general possible in shells of more dubious heritage, 
  90. as the theoretical 
  91.  
  92.     (perl -w test.pl >/dev/tty) >& test.out
  93.     ./splain < test.out
  94.  
  95. Because you just moved the existing B<stdout> to somewhere else.
  96.  
  97. If you don't want to modify your source code, but still have on-the-fly
  98. warnings, do this:
  99.  
  100.     exec 3>&1; perl -w test.pl 2>&1 1>&3 3>&- | splain 1>&2 3>&- 
  101.  
  102. Nifty, eh?
  103.  
  104. If you want to control warnings on the fly, do something like this.
  105. Make sure you do the C<use> first, or you won't be able to get
  106. at the enable() or disable() methods.
  107.  
  108.     use diagnostics; # checks entire compilation phase 
  109.     print "\ntime for 1st bogus diags: SQUAWKINGS\n";
  110.     print BOGUS1 'nada';
  111.     print "done with 1st bogus\n";
  112.  
  113.     disable diagnostics; # only turns off runtime warnings
  114.     print "\ntime for 2nd bogus: (squelched)\n";
  115.     print BOGUS2 'nada';
  116.     print "done with 2nd bogus\n";
  117.  
  118.     enable diagnostics; # turns back on runtime warnings
  119.     print "\ntime for 3rd bogus: SQUAWKINGS\n";
  120.     print BOGUS3 'nada';
  121.     print "done with 3rd bogus\n";
  122.  
  123.     disable diagnostics;
  124.     print "\ntime for 4th bogus: (squelched)\n";
  125.     print BOGUS4 'nada';
  126.     print "done with 4th bogus\n";
  127.  
  128. =head1 INTERNALS
  129.  
  130. Diagnostic messages derive from the F<perldiag.pod> file when available at
  131. runtime.  Otherwise, they may be embedded in the file itself when the
  132. splain package is built.   See the F<Makefile> for details.
  133.  
  134. If an extant $SIG{__WARN__} handler is discovered, it will continue
  135. to be honored, but only after the diagnostics::splainthis() function 
  136. (the module's $SIG{__WARN__} interceptor) has had its way with your
  137. warnings.
  138.  
  139. There is a $diagnostics::DEBUG variable you may set if you're desperately
  140. curious what sorts of things are being intercepted.
  141.  
  142.     BEGIN { $diagnostics::DEBUG = 1 } 
  143.  
  144.  
  145. =head1 BUGS
  146.  
  147. Not being able to say "no diagnostics" is annoying, but may not be
  148. insurmountable.
  149.  
  150. The C<-pretty> directive is called too late to affect matters.
  151. You have to do this instead, and I<before> you load the module.
  152.  
  153.     BEGIN { $diagnostics::PRETTY = 1 } 
  154.  
  155. I could start up faster by delaying compilation until it should be
  156. needed, but this gets a "panic: top_level" when using the pragma form
  157. in Perl 5.001e.
  158.  
  159. While it's true that this documentation is somewhat subserious, if you use
  160. a program named I<splain>, you should expect a bit of whimsy.
  161.  
  162. =head1 AUTHOR
  163.  
  164. Tom Christiansen <F<tchrist@mox.perl.com>>, 25 June 1995.
  165.  
  166. =cut
  167.  
  168. require 5.001;
  169. use Carp;
  170.  
  171. use Config;
  172. ($privlib, $archlib) = @Config{qw(privlibexp archlibexp)};
  173. if ($^O eq 'VMS') {
  174.     require VMS::Filespec;
  175.     $privlib = VMS::Filespec::unixify($privlib);
  176.     $archlib = VMS::Filespec::unixify($archlib);
  177. }
  178. @trypod = ("$archlib/pod/perldiag.pod",
  179.        "$privlib/pod/perldiag-$].pod",
  180.        "$privlib/pod/perldiag.pod");
  181. # handy for development testing of new warnings etc
  182. unshift @trypod, "./pod/perldiag.pod" if -e "pod/perldiag.pod";
  183. ($PODFILE) = ((grep { -e } @trypod), $trypod[$#trypod])[0];
  184.  
  185. $DEBUG ||= 0;
  186. my $WHOAMI = ref bless [];  # nobody's business, prolly not even mine
  187.  
  188. $| = 1;
  189.  
  190. local $_;
  191.  
  192. CONFIG: {
  193.     $opt_p = $opt_d = $opt_v = $opt_f = '';
  194.     %HTML_2_Troff = %HTML_2_Latin_1 = %HTML_2_ASCII_7 = ();  
  195.     %exact_duplicate = ();
  196.  
  197.     unless (caller) { 
  198.     $standalone++;
  199.     require Getopt::Std;
  200.     Getopt::Std::getopts('pdvf:')
  201.         or die "Usage: $0 [-v] [-p] [-f splainpod]";
  202.     $PODFILE = $opt_f if $opt_f;
  203.     $DEBUG = 2 if $opt_d;
  204.     $VERBOSE = $opt_v;
  205.     $PRETTY = $opt_p;
  206.     } 
  207.  
  208.     if (open(POD_DIAG, $PODFILE)) {
  209.     warn "Happy happy podfile from real $PODFILE\n" if $DEBUG;
  210.     last CONFIG;
  211.     } 
  212.  
  213.     if (caller) {
  214.     INCPATH: {
  215.         for $file ( (map { "$_/$WHOAMI.pm" } @INC), $0) {
  216.         warn "Checking $file\n" if $DEBUG;
  217.         if (open(POD_DIAG, $file)) {
  218.             while (<POD_DIAG>) {
  219.             next unless /^__END__\s*# wish diag dbase were more accessible/;
  220.             print STDERR "podfile is $file\n" if $DEBUG;
  221.             last INCPATH;
  222.             }
  223.         }
  224.         } 
  225.     }
  226.     } else { 
  227.     print STDERR "podfile is <DATA>\n" if $DEBUG;
  228.     *POD_DIAG = *main::DATA;
  229.     }
  230. }
  231. if (eof(POD_DIAG)) { 
  232.     die "couldn't find diagnostic data in $PODFILE @INC $0";
  233. }
  234.  
  235.  
  236. %HTML_2_Troff = (
  237.     'amp'    =>    '&',    #   ampersand
  238.     'lt'    =>    '<',    #   left chevron, less-than
  239.     'gt'    =>    '>',    #   right chevron, greater-than
  240.     'quot'    =>    '"',    #   double quote
  241.  
  242.     "Aacute"    =>    "A\\*'",    #   capital A, acute accent
  243.     # etc
  244.  
  245. );
  246.  
  247. %HTML_2_Latin_1 = (
  248.     'amp'    =>    '&',    #   ampersand
  249.     'lt'    =>    '<',    #   left chevron, less-than
  250.     'gt'    =>    '>',    #   right chevron, greater-than
  251.     'quot'    =>    '"',    #   double quote
  252.  
  253.     "Aacute"    =>    "\xC1"    #   capital A, acute accent
  254.  
  255.     # etc
  256. );
  257.  
  258. %HTML_2_ASCII_7 = (
  259.     'amp'    =>    '&',    #   ampersand
  260.     'lt'    =>    '<',    #   left chevron, less-than
  261.     'gt'    =>    '>',    #   right chevron, greater-than
  262.     'quot'    =>    '"',    #   double quote
  263.  
  264.     "Aacute"    =>    "A"    #   capital A, acute accent
  265.     # etc
  266. );
  267.  
  268. *HTML_Escapes = do {
  269.     if ($standalone) {
  270.     $PRETTY ? \%HTML_2_Latin_1 : \%HTML_2_ASCII_7; 
  271.     } else {
  272.     \%HTML_2_Latin_1; 
  273.     }
  274. }; 
  275.  
  276. *THITHER = $standalone ? *STDOUT : *STDERR;
  277.  
  278. $transmo = <<EOFUNC;
  279. sub transmo {
  280.     local \$^W = 0;  # recursive warnings we do NOT need!
  281.     study;
  282. EOFUNC
  283.  
  284. ### sub finish_compilation {  # 5.001e panic: top_level for embedded version
  285.     print STDERR "FINISHING COMPILATION for $_\n" if $DEBUG;
  286.     ### local 
  287.     $RS = '';
  288.     local $_;
  289.     while (<POD_DIAG>) {
  290.     #s/(.*)\n//;
  291.     #$header = $1;
  292.  
  293.     unescape();
  294.     if ($PRETTY) {
  295.         sub noop   { return $_[0] }  # spensive for a noop
  296.         sub bold   { my $str =$_[0];  $str =~ s/(.)/$1\b$1/g; return $str; } 
  297.         sub italic { my $str = $_[0]; $str =~ s/(.)/_\b$1/g;  return $str; } 
  298.         s/[BC]<(.*?)>/bold($1)/ges;
  299.         s/[LIF]<(.*?)>/italic($1)/ges;
  300.     } else {
  301.         s/[BC]<(.*?)>/$1/gs;
  302.         s/[LIF]<(.*?)>/$1/gs;
  303.     } 
  304.     unless (/^=/) {
  305.         if (defined $header) { 
  306.         if ( $header eq 'DESCRIPTION' && 
  307.             (   /Optional warnings are enabled/ 
  308.              || /Some of these messages are generic./
  309.             ) )
  310.         {
  311.             next;
  312.         } 
  313.         s/^/    /gm;
  314.         $msg{$header} .= $_;
  315.         }
  316.         next;
  317.     } 
  318.     unless ( s/=item (.*)\s*\Z//) {
  319.  
  320.         if ( s/=head1\sDESCRIPTION//) {
  321.         $msg{$header = 'DESCRIPTION'} = '';
  322.         }
  323.         next;
  324.     }
  325.  
  326.     # strip formatting directives in =item line
  327.     ($header = $1) =~ s/[A-Z]<(.*?)>/$1/g;
  328.  
  329.     if ($header =~ /%[sd]/) {
  330.         $rhs = $lhs = $header;
  331.         #if ($lhs =~ s/(.*?)%d(?!%d)(.*)/\Q$1\E\\d+\Q$2\E\$/g)  {
  332.         if ($lhs =~ s/(.*?)%d(?!%d)(.*)/\Q$1\E\\d+\Q$2\E/g)  {
  333.         $lhs =~ s/\\%s/.*?/g;
  334.         } else {
  335.         # if i had lookbehind negations, i wouldn't have to do this \377 noise
  336.         $lhs =~ s/(.*?)%s/\Q$1\E.*?\377/g;
  337.         #$lhs =~ s/\377([^\377]*)$/\Q$1\E\$/;
  338.         $lhs =~ s/\377([^\377]*)$/\Q$1\E/;
  339.         $lhs =~ s/\377//g;
  340.         $lhs =~ s/\.\*\?$/.*/; # Allow %s at the end to eat it all
  341.         } 
  342.         $transmo .= "    s{^$lhs}\n     {\Q$rhs\E}s\n\t&& return 1;\n";
  343.     } else {
  344.         $transmo .= "    m{^\Q$header\E} && return 1;\n";
  345.     } 
  346.  
  347.     print STDERR "$WHOAMI: Duplicate entry: \"$header\"\n"
  348.         if $msg{$header};
  349.  
  350.     $msg{$header} = '';
  351.     } 
  352.  
  353.  
  354.     close POD_DIAG unless *main::DATA eq *POD_DIAG;
  355.  
  356.     die "No diagnostics?" unless %msg;
  357.  
  358.     $transmo .= "    return 0;\n}\n";
  359.     print STDERR $transmo if $DEBUG;
  360.     eval $transmo;
  361.     die $@ if $@;
  362.     $RS = "\n";
  363. ### }
  364.  
  365. if ($standalone) {
  366.     if (!@ARGV and -t STDIN) { print STDERR "$0: Reading from STDIN\n" } 
  367.     while (defined ($error = <>)) {
  368.     splainthis($error) || print THITHER $error;
  369.     } 
  370.     exit;
  371. } else { 
  372.     $old_w = 0; $oldwarn = ''; $olddie = '';
  373. }
  374.  
  375. sub import {
  376.     shift;
  377.     $old_w = $^W;
  378.     $^W = 1; # yup, clobbered the global variable; tough, if you
  379.          # want diags, you want diags.
  380.     return if $SIG{__WARN__} eq \&warn_trap;
  381.  
  382.     for (@_) {
  383.  
  384.     /^-d(ebug)?$/            && do {
  385.                     $DEBUG++;
  386.                     next;
  387.                    };
  388.  
  389.     /^-v(erbose)?$/     && do {
  390.                     $VERBOSE++;
  391.                     next;
  392.                    };
  393.  
  394.     /^-p(retty)?$/         && do {
  395.                     print STDERR "$0: I'm afraid it's too late for prettiness.\n";
  396.                     $PRETTY++;
  397.                     next;
  398.                    };
  399.  
  400.     warn "Unknown flag: $_";
  401.     } 
  402.  
  403.     $oldwarn = $SIG{__WARN__};
  404.     $olddie = $SIG{__DIE__};
  405.     $SIG{__WARN__} = \&warn_trap;
  406.     $SIG{__DIE__} = \&death_trap;
  407.  
  408. sub enable { &import }
  409.  
  410. sub disable {
  411.     shift;
  412.     $^W = $old_w;
  413.     return unless $SIG{__WARN__} eq \&warn_trap;
  414.     $SIG{__WARN__} = $oldwarn;
  415.     $SIG{__DIE__} = $olddie;
  416.  
  417. sub warn_trap {
  418.     my $warning = $_[0];
  419.     if (caller eq $WHOAMI or !splainthis($warning)) {
  420.     print STDERR $warning;
  421.     } 
  422.     &$oldwarn if defined $oldwarn and $oldwarn and $oldwarn ne \&warn_trap;
  423. };
  424.  
  425. sub death_trap {
  426.     my $exception = $_[0];
  427.  
  428.     # See if we are coming from anywhere within an eval. If so we don't
  429.     # want to explain the exception because it's going to get caught.
  430.     my $in_eval = 0;
  431.     my $i = 0;
  432.     while (1) {
  433.       my $caller = (caller($i++))[3] or last;
  434.       if ($caller eq '(eval)') {
  435.     $in_eval = 1;
  436.     last;
  437.       }
  438.     }
  439.  
  440.     splainthis($exception) unless $in_eval;
  441.     if (caller eq $WHOAMI) { print STDERR "INTERNAL EXCEPTION: $exception"; } 
  442.     &$olddie if defined $olddie and $olddie and $olddie ne \&death_trap;
  443.  
  444.     # We don't want to unset these if we're coming from an eval because
  445.     # then we've turned off diagnostics. (Actually what does this next
  446.     # line do?  -PSeibel)
  447.     $SIG{__DIE__} = $SIG{__WARN__} = '' unless $in_eval;
  448.     local($Carp::CarpLevel) = 1;
  449.     confess "Uncaught exception from user code:\n\t$exception";
  450.     # up we go; where we stop, nobody knows, but i think we die now
  451.     # but i'm deeply afraid of the &$olddie guy reraising and us getting
  452.     # into an indirect recursion loop
  453. };
  454.  
  455. sub splainthis {
  456.     local $_ = shift;
  457.     local $\;
  458.     ### &finish_compilation unless %msg;
  459.     s/\.?\n+$//;
  460.     my $orig = $_;
  461.     # return unless defined;
  462.     if ($exact_duplicate{$_}++) {
  463.     return 1;
  464.     } 
  465.     s/, <.*?> (?:line|chunk).*$//;
  466.     $real = s/(.*?) at .*? (?:line|chunk) \d+.*/$1/;
  467.     s/^\((.*)\)$/$1/;
  468.     return 0 unless &transmo;
  469.     $orig = shorten($orig);
  470.     if ($old_diag{$_}) {
  471.     autodescribe();
  472.     print THITHER "$orig (#$old_diag{$_})\n";
  473.     $wantspace = 1;
  474.     } else {
  475.     autodescribe();
  476.     $old_diag{$_} = ++$count;
  477.     print THITHER "\n" if $wantspace;
  478.     $wantspace = 0;
  479.     print THITHER "$orig (#$old_diag{$_})\n";
  480.     if ($msg{$_}) {
  481.         print THITHER $msg{$_};
  482.     } else {
  483.         if (0 and $standalone) { 
  484.         print THITHER "    **** Error #$old_diag{$_} ",
  485.             ($real ? "is" : "appears to be"),
  486.             " an unknown diagnostic message.\n\n";
  487.         }
  488.         return 0;
  489.     } 
  490.     }
  491.     return 1;
  492.  
  493. sub autodescribe {
  494.     if ($VERBOSE and not $count) {
  495.     print THITHER &{$PRETTY ? \&bold : \&noop}("DESCRIPTION OF DIAGNOSTICS"),
  496.         "\n$msg{DESCRIPTION}\n";
  497.     } 
  498.  
  499. sub unescape { 
  500.     s {
  501.             E<  
  502.             ( [A-Za-z]+ )       
  503.             >   
  504.     } { 
  505.          do {   
  506.              exists $HTML_Escapes{$1}
  507.                 ? do { $HTML_Escapes{$1} }
  508.                 : do {
  509.                     warn "Unknown escape: E<$1> in $_";
  510.                     "E<$1>";
  511.                 } 
  512.          } 
  513.     }egx;
  514. }
  515.  
  516. sub shorten {
  517.     my $line = $_[0];
  518.     if (length($line) > 79 and index($line, "\n") == -1) {
  519.     my $space_place = rindex($line, ' ', 79);
  520.     if ($space_place != -1) {
  521.         substr($line, $space_place, 1) = "\n\t";
  522.     } 
  523.     } 
  524.     return $line;
  525.  
  526.  
  527. # have to do this: RS isn't set until run time, but we're executing at compile time
  528. $RS = "\n";
  529.  
  530. 1 unless $standalone;  # or it'll complain about itself
  531. __END__ # wish diag dbase were more accessible
  532.