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.

533 lines
14 KiB

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