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.

577 lines
15 KiB

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