Leaked source code of windows server 2003
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.

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