This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
ppport.h: Don't say unsupported if avail in earliest version
[perl5.git] / dist / Devel-PPPort / parts / inc / ppphbin
CommitLineData
adfe19db
MHM
1################################################################################
2##
b2049988 3## Version 3.x, Copyright (C) 2004-2013, Marcus Holland-Moritz.
adfe19db
MHM
4## Version 2.x, Copyright (C) 2001, Paul Marquess.
5## Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
6##
7## This program is free software; you can redistribute it and/or
8## modify it under the same terms as Perl itself.
9##
10################################################################################
11
12=provides
13
14=implementation
15
adfe19db
MHM
16use strict;
17
c83e6f19 18# Disable broken TRIE-optimization
f551177d 19BEGIN { eval '${^RE_TRIE_MAXBUF} = -1' if "$]" >= 5.009004 && "$]" <= 5.009005 }
c83e6f19 20
78b4ff79
MHM
21my $VERSION = __VERSION__;
22
adfe19db
MHM
23my %opt = (
24 quiet => 0,
25 diag => 1,
26 hints => 1,
27 changes => 1,
28 cplusplus => 0,
4a582685 29 filter => 1,
0d0f8426 30 strip => 0,
78b4ff79 31 version => 0,
adfe19db
MHM
32);
33
34my($ppport) = $0 =~ /([\w.]+)$/;
35my $LF = '(?:\r\n|[\r\n])'; # line feed
36my $HS = "[ \t]"; # horizontal whitespace
37
c83e6f19
MHM
38# Never use C comments in this file!
39my $ccs = '/'.'*';
40my $cce = '*'.'/';
41my $rccs = quotemeta $ccs;
42my $rcce = quotemeta $cce;
43
adfe19db
MHM
44eval {
45 require Getopt::Long;
46 Getopt::Long::GetOptions(\%opt, qw(
78b4ff79 47 help quiet diag! filter! hints! changes! cplusplus strip version
adfe19db 48 patch=s copy=s diff=s compat-version=s
04fc8b94 49 list-provided list-unsupported api-info=s
adfe19db
MHM
50 )) or usage();
51};
52
53if ($@ and grep /^-/, @ARGV) {
54 usage() if "@ARGV" =~ /^--?h(?:elp)?$/;
55 die "Getopt::Long not found. Please don't use any options.\n";
56}
57
78b4ff79
MHM
58if ($opt{version}) {
59 print "This is $0 $VERSION.\n";
60 exit 0;
61}
62
adfe19db 63usage() if $opt{help};
0d0f8426 64strip() if $opt{strip};
adfe19db 65
f7074f41
KW
66$opt{'compat-version'} = __MIN_PERL__ unless exists $opt{'compat-version'};
67$opt{'compat-version'} = int_parse_version($opt{'compat-version'});
adfe19db 68
adfe19db 69my %API = map { /^(\w+)\|([^|]*)\|([^|]*)\|(\w*)$/
4a582685 70 ? ( $1 => {
adfe19db
MHM
71 ($2 ? ( base => $2 ) : ()),
72 ($3 ? ( todo => $3 ) : ()),
73 (index($4, 'v') >= 0 ? ( varargs => 1 ) : ()),
74 (index($4, 'p') >= 0 ? ( provided => 1 ) : ()),
16289599 75 (index($4, 'n') >= 0 ? ( noTHXarg => 1 ) : ()),
bf9610ae
KW
76 (index($4, 'c') >= 0 ? ( core_only => 1 ) : ()),
77 (index($4, 'd') >= 0 ? ( deprecated => 1 ) : ()),
78 (index($4, 'i') >= 0 ? ( inaccessible => 1 ) : ()),
79 (index($4, 'x') >= 0 ? ( experimental => 1 ) : ()),
80 (index($4, 'u') >= 0 ? ( undocumented => 1 ) : ()),
adfe19db
MHM
81 } )
82 : die "invalid spec: $_" } qw(
83__PERL_API__
84);
85
86if (exists $opt{'list-unsupported'}) {
87 my $f;
55179e46 88 for $f (sort dictionary_order keys %API) {
b151b75a
KW
89 next if $API{$f}{core_only};
90 next if $API{$f}{beyond_depr};
91 next if $API{$f}{inaccessible};
92 next if $API{$f}{experimental};
adfe19db 93 next unless $API{$f}{todo};
644a811e 94 next if int_parse_version($API{$f}{todo}) <= int_parse_version(__MIN_PERL__);
adfe19db
MHM
95 print "$f ", '.'x(40-length($f)), " ", format_version($API{$f}{todo}), "\n";
96 }
97 exit 0;
98}
99
100# Scan for possible replacement candidates
101
679ad62d 102my(%replace, %need, %hints, %warnings, %depends);
adfe19db 103my $replace = 0;
679ad62d 104my($hint, $define, $function);
adfe19db 105
af36fda7
MHM
106sub find_api
107{
108 my $code = shift;
109 $code =~ s{
c83e6f19
MHM
110 / (?: \*[^*]*\*+(?:[^$ccs][^*]*\*+)* / | /[^\r\n]*)
111 | "[^"\\]*(?:\\.[^"\\]*)*"
112 | '[^'\\]*(?:\\.[^'\\]*)*' }{}egsx;
af36fda7
MHM
113 grep { exists $API{$_} } $code =~ /(\w+)/mg;
114}
115
adfe19db
MHM
116while (<DATA>) {
117 if ($hint) {
679ad62d 118 my $h = $hint->[0] eq 'Hint' ? \%hints : \%warnings;
adfe19db 119 if (m{^\s*\*\s(.*?)\s*$}) {
679ad62d
MHM
120 for (@{$hint->[1]}) {
121 $h->{$_} ||= ''; # suppress warning with older perls
122 $h->{$_} .= "$1\n";
123 }
124 }
c83e6f19 125 else { undef $hint }
679ad62d
MHM
126 }
127
c83e6f19
MHM
128 $hint = [$1, [split /,?\s+/, $2]]
129 if m{^\s*$rccs\s+(Hint|Warning):\s+(\w+(?:,?\s+\w+)*)\s*$};
679ad62d
MHM
130
131 if ($define) {
132 if ($define->[1] =~ /\\$/) {
133 $define->[1] .= $_;
134 }
135 else {
136 if (exists $API{$define->[0]} && $define->[1] !~ /^DPPP_\(/) {
af36fda7 137 my @n = find_api($define->[1]);
679ad62d
MHM
138 push @{$depends{$define->[0]}}, @n if @n
139 }
140 undef $define;
141 }
142 }
143
144 $define = [$1, $2] if m{^\s*#\s*define\s+(\w+)(?:\([^)]*\))?\s+(.*)};
145
146 if ($function) {
147 if (/^}/) {
148 if (exists $API{$function->[0]}) {
af36fda7 149 my @n = find_api($function->[1]);
679ad62d
MHM
150 push @{$depends{$function->[0]}}, @n if @n
151 }
c1a049cb 152 undef $function;
adfe19db
MHM
153 }
154 else {
679ad62d 155 $function->[1] .= $_;
adfe19db
MHM
156 }
157 }
679ad62d
MHM
158
159 $function = [$1, ''] if m{^DPPP_\(my_(\w+)\)};
adfe19db
MHM
160
161 $replace = $1 if m{^\s*$rccs\s+Replace:\s+(\d+)\s+$rcce\s*$};
162 $replace{$2} = $1 if $replace and m{^\s*#\s*define\s+(\w+)(?:\([^)]*\))?\s+(\w+)};
163 $replace{$2} = $1 if m{^\s*#\s*define\s+(\w+)(?:\([^)]*\))?\s+(\w+).*$rccs\s+Replace\s+$rcce};
164 $replace{$1} = $2 if m{^\s*$rccs\s+Replace (\w+) with (\w+)\s+$rcce\s*$};
165
c01be2ce
MHM
166 if (m{^\s*$rccs\s+(\w+(\s*,\s*\w+)*)\s+depends\s+on\s+(\w+(\s*,\s*\w+)*)\s+$rcce\s*$}) {
167 my @deps = map { s/\s+//g; $_ } split /,/, $3;
168 my $d;
169 for $d (map { s/\s+//g; $_ } split /,/, $1) {
170 push @{$depends{$d}}, @deps;
171 }
adfe19db
MHM
172 }
173
174 $need{$1} = 1 if m{^#if\s+defined\(NEED_(\w+)(?:_GLOBAL)?\)};
175}
176
679ad62d
MHM
177for (values %depends) {
178 my %s;
55179e46 179 $_ = [sort dictionary_order grep !$s{$_}++, @$_];
679ad62d
MHM
180}
181
04fc8b94
MHM
182if (exists $opt{'api-info'}) {
183 my $f;
184 my $count = 0;
9132e1a3 185 my $match = $opt{'api-info'} =~ m!^/(.*)/$! ? $1 : "^\Q$opt{'api-info'}\E\$";
55179e46 186 for $f (sort dictionary_order keys %API) {
9132e1a3 187 next unless $f =~ /$match/;
04fc8b94
MHM
188 print "\n=== $f ===\n\n";
189 my $info = 0;
190 if ($API{$f}{base} || $API{$f}{todo}) {
191 my $base = format_version($API{$f}{base} || $API{$f}{todo});
9132e1a3 192 print "Supported at least starting from perl-$base.\n";
04fc8b94
MHM
193 $info++;
194 }
195 if ($API{$f}{provided}) {
196 my $todo = $API{$f}{todo} ? format_version($API{$f}{todo}) : "__MIN_PERL__";
9132e1a3 197 print "Support by $ppport provided back to perl-$todo.\n";
04fc8b94
MHM
198 print "Support needs to be explicitly requested by NEED_$f.\n" if exists $need{$f};
199 print "Depends on: ", join(', ', @{$depends{$f}}), ".\n" if exists $depends{$f};
679ad62d
MHM
200 print "\n$hints{$f}" if exists $hints{$f};
201 print "\nWARNING:\n$warnings{$f}" if exists $warnings{$f};
04fc8b94
MHM
202 $info++;
203 }
bf9610ae
KW
204
205 my $email = "Send email to perl5-porters\@perl.org if you need to have this functionality.\n";
206 if ($API{$f}{inaccessible}) {
207 print "\nThis is not part of the public API, and may not even be accessible to XS code.\n";
208 $info++;
209 }
210 elsif ($API{$f}{core_only}) {
211 print "\nThis is not part of the public API, and should not be used by XS code.\n";
212 $info++;
213 }
214 elsif ($API{$f}{deprecated}) {
215 print "\nThis is deprecated and should not be used. Convert existing uses.\n";
216 $info++;
217 }
218 elsif ($API{$f}{experimental}) {
219 print "\nThe API for this is unstable and should not be used by XS code.\n", $email;
220 $info++;
221 }
222 elsif ($API{$f}{undocumented}) {
223 print "\nSince this is undocumented, the API should be considered unstable.\n";
224 if ($API{$f}{provided}) {
225 print "Consider bringing this up on the list: perl5-porters\@perl.org.\n";
226 }
227 else {
228 print "It may be that this is not intended for XS use, or it may just be\n",
229 "that no one has gotten around to documenting it.\n", $email;
230 }
231 $info++;
232 }
233 unless ($info) {
234 print "No portability information available. Check your spelling; or",
235 " this could be\na bug in Devel::PPPort. To report an issue:\n",
236 "https://github.com/Dual-Life/Devel-PPPort/issues/new\n";
237 }
04fc8b94
MHM
238 $count++;
239 }
bf9610ae 240 $count or print "\nFound no API matching '$opt{'api-info'}'.";
c83e6f19 241 print "\n";
04fc8b94
MHM
242 exit 0;
243}
244
adfe19db
MHM
245if (exists $opt{'list-provided'}) {
246 my $f;
55179e46 247 for $f (sort dictionary_order keys %API) {
adfe19db
MHM
248 next unless $API{$f}{provided};
249 my @flags;
250 push @flags, 'explicit' if exists $need{$f};
251 push @flags, 'depend' if exists $depends{$f};
252 push @flags, 'hint' if exists $hints{$f};
679ad62d 253 push @flags, 'warning' if exists $warnings{$f};
adfe19db
MHM
254 my $flags = @flags ? ' ['.join(', ', @flags).']' : '';
255 print "$f$flags\n";
256 }
257 exit 0;
258}
259
4a582685 260my @files;
679ad62d
MHM
261my @srcext = qw( .xs .c .h .cc .cpp -c.inc -xs.inc );
262my $srcext = join '|', map { quotemeta $_ } @srcext;
4a582685
NC
263
264if (@ARGV) {
265 my %seen;
679ad62d
MHM
266 for (@ARGV) {
267 if (-e) {
268 if (-f) {
269 push @files, $_ unless $seen{$_}++;
270 }
271 else { warn "'$_' is not a file.\n" }
272 }
273 else {
274 my @new = grep { -f } glob $_
275 or warn "'$_' does not exist.\n";
276 push @files, grep { !$seen{$_}++ } @new;
277 }
278 }
4a582685
NC
279}
280else {
281 eval {
282 require File::Find;
283 File::Find::find(sub {
679ad62d 284 $File::Find::name =~ /($srcext)$/i
4a582685
NC
285 and push @files, $File::Find::name;
286 }, '.');
287 };
288 if ($@) {
679ad62d 289 @files = map { glob "*$_" } @srcext;
4a582685
NC
290 }
291}
292
293if (!@ARGV || $opt{filter}) {
294 my(@in, @out);
295 my %xsc = map { /(.*)\.xs$/ ? ("$1.c" => 1, "$1.cc" => 1) : () } @files;
296 for (@files) {
679ad62d 297 my $out = exists $xsc{$_} || /\b\Q$ppport\E$/i || !/($srcext)$/i;
4a582685
NC
298 push @{ $out ? \@out : \@in }, $_;
299 }
300 if (@ARGV && @out) {
301 warning("Skipping the following files (use --nofilter to avoid this):\n| ", join "\n| ", @out);
302 }
303 @files = @in;
304}
305
c83e6f19 306die "No input files given!\n" unless @files;
4a582685 307
adfe19db
MHM
308my(%files, %global, %revreplace);
309%revreplace = reverse %replace;
310my $filename;
311my $patch_opened = 0;
312
313for $filename (@files) {
314 unless (open IN, "<$filename") {
315 warn "Unable to read from $filename: $!\n";
316 next;
317 }
318
319 info("Scanning $filename ...");
320
321 my $c = do { local $/; <IN> };
322 close IN;
323
324 my %file = (orig => $c, changes => 0);
325
c83e6f19 326 # Temporarily remove C/XS comments and strings from the code
adfe19db 327 my @ccom;
c83e6f19 328
adfe19db 329 $c =~ s{
c83e6f19
MHM
330 ( ^$HS*\#$HS*include\b[^\r\n]+\b(?:\Q$ppport\E|XSUB\.h)\b[^\r\n]*
331 | ^$HS*\#$HS*(?:define|elif|if(?:def)?)\b[^\r\n]* )
332 | ( ^$HS*\#[^\r\n]*
333 | "[^"\\]*(?:\\.[^"\\]*)*"
334 | '[^'\\]*(?:\\.[^'\\]*)*'
335 | / (?: \*[^*]*\*+(?:[^$ccs][^*]*\*+)* / | /[^\r\n]* ) )
af36fda7 336 }{ defined $2 and push @ccom, $2;
c83e6f19 337 defined $1 ? $1 : "$ccs$#ccom$cce" }mgsex;
adfe19db
MHM
338
339 $file{ccom} = \@ccom;
340 $file{code} = $c;
c83e6f19 341 $file{has_inc_ppport} = $c =~ /^$HS*#$HS*include[^\r\n]+\b\Q$ppport\E\b/m;
adfe19db
MHM
342
343 my $func;
344
345 for $func (keys %API) {
346 my $match = $func;
347 $match .= "|$revreplace{$func}" if exists $revreplace{$func};
348 if ($c =~ /\b(?:Perl_)?($match)\b/) {
349 $file{uses_replace}{$1}++ if exists $revreplace{$func} && $1 eq $revreplace{$func};
350 $file{uses_Perl}{$func}++ if $c =~ /\bPerl_$func\b/;
351 if (exists $API{$func}{provided}) {
679ad62d 352 $file{uses_provided}{$func}++;
f7074f41
KW
353 if ( ! exists $API{$func}{base}
354 || int_parse_version($API{$func}{base}) > $opt{'compat-version'})
355 {
adfe19db 356 $file{uses}{$func}++;
adfe19db
MHM
357 my @deps = rec_depend($func);
358 if (@deps) {
359 $file{uses_deps}{$func} = \@deps;
360 for (@deps) {
361 $file{uses}{$_} = 0 unless exists $file{uses}{$_};
adfe19db
MHM
362 }
363 }
364 for ($func, @deps) {
c83e6f19 365 $file{needs}{$_} = 'static' if exists $need{$_};
adfe19db
MHM
366 }
367 }
368 }
f7074f41
KW
369 if ( exists $API{$func}{todo}
370 && int_parse_version($API{$func}{todo}) > $opt{'compat-version'})
371 {
adfe19db
MHM
372 if ($c =~ /\b$func\b/) {
373 $file{uses_todo}{$func}++;
adfe19db
MHM
374 }
375 }
376 }
377 }
378
379 while ($c =~ /^$HS*#$HS*define$HS+(NEED_(\w+?)(_GLOBAL)?)\b/mg) {
380 if (exists $need{$2}) {
381 $file{defined $3 ? 'needed_global' : 'needed_static'}{$2}++;
adfe19db 382 }
c83e6f19 383 else { warning("Possibly wrong #define $1 in $filename") }
adfe19db
MHM
384 }
385
96ad942f
MHM
386 for (qw(uses needs uses_todo needed_global needed_static)) {
387 for $func (keys %{$file{$_}}) {
388 push @{$global{$_}{$func}}, $filename;
389 }
390 }
391
adfe19db
MHM
392 $files{$filename} = \%file;
393}
394
395# Globally resolve NEED_'s
396my $need;
397for $need (keys %{$global{needs}}) {
398 if (@{$global{needs}{$need}} > 1) {
399 my @targets = @{$global{needs}{$need}};
400 my @t = grep $files{$_}{needed_global}{$need}, @targets;
401 @targets = @t if @t;
402 @t = grep /\.xs$/i, @targets;
403 @targets = @t if @t;
404 my $target = shift @targets;
405 $files{$target}{needs}{$need} = 'global';
406 for (@{$global{needs}{$need}}) {
407 $files{$_}{needs}{$need} = 'extern' if $_ ne $target;
408 }
409 }
410}
411
412for $filename (@files) {
413 exists $files{$filename} or next;
414
415 info("=== Analyzing $filename ===");
416
417 my %file = %{$files{$filename}};
418 my $func;
419 my $c = $file{code};
679ad62d 420 my $warnings = 0;
adfe19db 421
55179e46 422 for $func (sort dictionary_order keys %{$file{uses_Perl}}) {
adfe19db 423 if ($API{$func}{varargs}) {
16289599 424 unless ($API{$func}{noTHXarg}) {
aab9a3b6
MHM
425 my $changes = ($c =~ s{\b(Perl_$func\s*\(\s*)(?!aTHX_?)(\)|[^\s)]*\))}
426 { $1 . ($2 eq ')' ? 'aTHX' : 'aTHX_ ') . $2 }ge);
427 if ($changes) {
428 warning("Doesn't pass interpreter argument aTHX to Perl_$func");
429 $file{changes} += $changes;
430 }
adfe19db
MHM
431 }
432 }
433 else {
434 warning("Uses Perl_$func instead of $func");
435 $file{changes} += ($c =~ s{\bPerl_$func(\s*)\((\s*aTHX_?)?\s*}
436 {$func$1(}g);
437 }
438 }
439
55179e46 440 for $func (sort dictionary_order keys %{$file{uses_replace}}) {
adfe19db
MHM
441 warning("Uses $func instead of $replace{$func}");
442 $file{changes} += ($c =~ s/\b$func\b/$replace{$func}/g);
443 }
444
55179e46 445 for $func (sort dictionary_order keys %{$file{uses_provided}}) {
679ad62d
MHM
446 if ($file{uses}{$func}) {
447 if (exists $file{uses_deps}{$func}) {
448 diag("Uses $func, which depends on ", join(', ', @{$file{uses_deps}{$func}}));
449 }
450 else {
451 diag("Uses $func");
452 }
adfe19db 453 }
679ad62d 454 $warnings += hint($func);
adfe19db
MHM
455 }
456
679ad62d 457 unless ($opt{quiet}) {
55179e46 458 for $func (sort dictionary_order keys %{$file{uses_todo}}) {
679ad62d
MHM
459 print "*** WARNING: Uses $func, which may not be portable below perl ",
460 format_version($API{$func}{todo}), ", even with '$ppport'\n";
461 $warnings++;
462 }
adfe19db
MHM
463 }
464
55179e46 465 for $func (sort dictionary_order keys %{$file{needed_static}}) {
adfe19db
MHM
466 my $message = '';
467 if (not exists $file{uses}{$func}) {
468 $message = "No need to define NEED_$func if $func is never used";
469 }
470 elsif (exists $file{needs}{$func} && $file{needs}{$func} ne 'static') {
471 $message = "No need to define NEED_$func when already needed globally";
472 }
473 if ($message) {
474 diag($message);
475 $file{changes} += ($c =~ s/^$HS*#$HS*define$HS+NEED_$func\b.*$LF//mg);
476 }
477 }
478
55179e46 479 for $func (sort dictionary_order keys %{$file{needed_global}}) {
adfe19db
MHM
480 my $message = '';
481 if (not exists $global{uses}{$func}) {
482 $message = "No need to define NEED_${func}_GLOBAL if $func is never used";
483 }
484 elsif (exists $file{needs}{$func}) {
485 if ($file{needs}{$func} eq 'extern') {
486 $message = "No need to define NEED_${func}_GLOBAL when already needed globally";
487 }
488 elsif ($file{needs}{$func} eq 'static') {
489 $message = "No need to define NEED_${func}_GLOBAL when only used in this file";
490 }
491 }
492 if ($message) {
493 diag($message);
494 $file{changes} += ($c =~ s/^$HS*#$HS*define$HS+NEED_${func}_GLOBAL\b.*$LF//mg);
495 }
496 }
497
498 $file{needs_inc_ppport} = keys %{$file{uses}};
499
500 if ($file{needs_inc_ppport}) {
501 my $pp = '';
502
55179e46 503 for $func (sort dictionary_order keys %{$file{needs}}) {
adfe19db
MHM
504 my $type = $file{needs}{$func};
505 next if $type eq 'extern';
506 my $suffix = $type eq 'global' ? '_GLOBAL' : '';
507 unless (exists $file{"needed_$type"}{$func}) {
508 if ($type eq 'global') {
509 diag("Files [@{$global{needs}{$func}}] need $func, adding global request");
510 }
511 else {
512 diag("File needs $func, adding static request");
513 }
514 $pp .= "#define NEED_$func$suffix\n";
515 }
516 }
517
518 if ($pp && ($c =~ s/^(?=$HS*#$HS*define$HS+NEED_\w+)/$pp/m)) {
519 $pp = '';
520 $file{changes}++;
521 }
522
523 unless ($file{has_inc_ppport}) {
524 diag("Needs to include '$ppport'");
525 $pp .= qq(#include "$ppport"\n)
526 }
527
528 if ($pp) {
529 $file{changes} += ($c =~ s/^($HS*#$HS*define$HS+NEED_\w+.*?)^/$1$pp/ms)
530 || ($c =~ s/^(?=$HS*#$HS*include.*\Q$ppport\E)/$pp/m)
531 || ($c =~ s/^($HS*#$HS*include.*XSUB.*\s*?)^/$1$pp/m)
532 || ($c =~ s/^/$pp/);
533 }
534 }
535 else {
536 if ($file{has_inc_ppport}) {
537 diag("No need to include '$ppport'");
538 $file{changes} += ($c =~ s/^$HS*?#$HS*include.*\Q$ppport\E.*?$LF//m);
539 }
540 }
541
542 # put back in our C comments
543 my $ix;
544 my $cppc = 0;
545 my @ccom = @{$file{ccom}};
546 for $ix (0 .. $#ccom) {
547 if (!$opt{cplusplus} && $ccom[$ix] =~ s!^//!!) {
548 $cppc++;
549 $file{changes} += $c =~ s/$rccs$ix$rcce/$ccs$ccom[$ix] $cce/;
550 }
551 else {
552 $c =~ s/$rccs$ix$rcce/$ccom[$ix]/;
553 }
554 }
555
556 if ($cppc) {
557 my $s = $cppc != 1 ? 's' : '';
558 warning("Uses $cppc C++ style comment$s, which is not portable");
559 }
560
679ad62d
MHM
561 my $s = $warnings != 1 ? 's' : '';
562 my $warn = $warnings ? " ($warnings warning$s)" : '';
563 info("Analysis completed$warn");
564
adfe19db
MHM
565 if ($file{changes}) {
566 if (exists $opt{copy}) {
567 my $newfile = "$filename$opt{copy}";
568 if (-e $newfile) {
569 error("'$newfile' already exists, refusing to write copy of '$filename'");
570 }
571 else {
572 local *F;
573 if (open F, ">$newfile") {
574 info("Writing copy of '$filename' with changes to '$newfile'");
575 print F $c;
576 close F;
577 }
578 else {
579 error("Cannot open '$newfile' for writing: $!");
580 }
581 }
582 }
583 elsif (exists $opt{patch} || $opt{changes}) {
584 if (exists $opt{patch}) {
585 unless ($patch_opened) {
586 if (open PATCH, ">$opt{patch}") {
587 $patch_opened = 1;
588 }
589 else {
590 error("Cannot open '$opt{patch}' for writing: $!");
591 delete $opt{patch};
592 $opt{changes} = 1;
593 goto fallback;
594 }
595 }
596 mydiff(\*PATCH, $filename, $c);
597 }
598 else {
599fallback:
600 info("Suggested changes:");
601 mydiff(\*STDOUT, $filename, $c);
602 }
603 }
604 else {
605 my $s = $file{changes} == 1 ? '' : 's';
606 info("$file{changes} potentially required change$s detected");
607 }
608 }
609 else {
610 info("Looks good");
611 }
612}
613
614close PATCH if $patch_opened;
615
616exit 0;
617
618#######################################################################
619
c83e6f19
MHM
620sub try_use { eval "use @_;"; return $@ eq '' }
621
adfe19db
MHM
622sub mydiff
623{
624 local *F = shift;
625 my($file, $str) = @_;
626 my $diff;
627
628 if (exists $opt{diff}) {
629 $diff = run_diff($opt{diff}, $file, $str);
630 }
631
c83e6f19 632 if (!defined $diff and try_use('Text::Diff')) {
adfe19db
MHM
633 $diff = Text::Diff::diff($file, \$str, { STYLE => 'Unified' });
634 $diff = <<HEADER . $diff;
635--- $file
636+++ $file.patched
637HEADER
638 }
639
640 if (!defined $diff) {
641 $diff = run_diff('diff -u', $file, $str);
642 }
643
644 if (!defined $diff) {
645 $diff = run_diff('diff', $file, $str);
646 }
647
648 if (!defined $diff) {
649 error("Cannot generate a diff. Please install Text::Diff or use --copy.");
650 return;
651 }
652
653 print F $diff;
adfe19db
MHM
654}
655
656sub run_diff
657{
658 my($prog, $file, $str) = @_;
659 my $tmp = 'dppptemp';
660 my $suf = 'aaa';
661 my $diff = '';
662 local *F;
663
664 while (-e "$tmp.$suf") { $suf++ }
665 $tmp = "$tmp.$suf";
666
667 if (open F, ">$tmp") {
668 print F $str;
669 close F;
670
671 if (open F, "$prog $file $tmp |") {
672 while (<F>) {
673 s/\Q$tmp\E/$file.patched/;
674 $diff .= $_;
675 }
676 close F;
677 unlink $tmp;
678 return $diff;
679 }
680
681 unlink $tmp;
682 }
683 else {
684 error("Cannot open '$tmp' for writing: $!");
685 }
686
687 return undef;
688}
689
adfe19db
MHM
690sub rec_depend
691{
af36fda7 692 my($func, $seen) = @_;
adfe19db 693 return () unless exists $depends{$func};
af36fda7
MHM
694 $seen = {%{$seen||{}}};
695 return () if $seen->{$func}++;
696 my %s;
697 grep !$s{$_}++, map { ($_, rec_depend($_, $seen)) } @{$depends{$func}};
adfe19db
MHM
698}
699
adfe19db
MHM
700sub info
701{
702 $opt{quiet} and return;
703 print @_, "\n";
704}
705
706sub diag
707{
708 $opt{quiet} and return;
709 $opt{diag} and print @_, "\n";
710}
711
712sub warning
713{
714 $opt{quiet} and return;
715 print "*** ", @_, "\n";
716}
717
718sub error
719{
720 print "*** ERROR: ", @_, "\n";
721}
722
723my %given_hints;
679ad62d 724my %given_warnings;
adfe19db
MHM
725sub hint
726{
727 $opt{quiet} and return;
adfe19db 728 my $func = shift;
679ad62d
MHM
729 my $rv = 0;
730 if (exists $warnings{$func} && !$given_warnings{$func}++) {
731 my $warn = $warnings{$func};
732 $warn =~ s!^!*** !mg;
733 print "*** WARNING: $func\n", $warn;
734 $rv++;
735 }
736 if ($opt{hints} && exists $hints{$func} && !$given_hints{$func}++) {
737 my $hint = $hints{$func};
738 $hint =~ s/^/ /mg;
739 print " --- hint for $func ---\n", $hint;
740 }
741 $rv;
adfe19db
MHM
742}
743
744sub usage
745{
746 my($usage) = do { local(@ARGV,$/)=($0); <> } =~ /^=head\d$HS+SYNOPSIS\s*^(.*?)\s*^=/ms;
747 my %M = ( 'I' => '*' );
748 $usage =~ s/^\s*perl\s+\S+/$^X $0/;
749 $usage =~ s/([A-Z])<([^>]+)>/$M{$1}$2$M{$1}/g;
750
751 print <<ENDUSAGE;
752
753Usage: $usage
754
755See perldoc $0 for details.
756
757ENDUSAGE
758
759 exit 2;
760}
0d0f8426
MHM
761
762sub strip
763{
764 my $self = do { local(@ARGV,$/)=($0); <> };
78b4ff79
MHM
765 my($copy) = $self =~ /^=head\d\s+COPYRIGHT\s*^(.*?)^=\w+/ms;
766 $copy =~ s/^(?=\S+)/ /gms;
767 $self =~ s/^$HS+Do NOT edit.*?(?=^-)/$copy/ms;
0d0f8426
MHM
768 $self =~ s/^SKIP.*(?=^__DATA__)/SKIP
769if (\@ARGV && \$ARGV[0] eq '--unstrip') {
770 eval { require Devel::PPPort };
771 \$@ and die "Cannot require Devel::PPPort, please install.\\n";
51d6c659 772 if (eval \$Devel::PPPort::VERSION < $VERSION) {
78b4ff79
MHM
773 die "$0 was originally generated with Devel::PPPort $VERSION.\\n"
774 . "Your Devel::PPPort is only version \$Devel::PPPort::VERSION.\\n"
775 . "Please install a newer version, or --unstrip will not work.\\n";
776 }
0d0f8426
MHM
777 Devel::PPPort::WriteFile(\$0);
778 exit 0;
779}
780print <<END;
781
782Sorry, but this is a stripped version of \$0.
783
784To be able to use its original script and doc functionality,
785please try to regenerate this file using:
786
787 \$^X \$0 --unstrip
788
789END
790/ms;
c83e6f19
MHM
791 my($pl, $c) = $self =~ /(.*^__DATA__)(.*)/ms;
792 $c =~ s{
793 / (?: \*[^*]*\*+(?:[^$ccs][^*]*\*+)* / | /[^\r\n]*)
794 | ( "[^"\\]*(?:\\.[^"\\]*)*"
795 | '[^'\\]*(?:\\.[^'\\]*)*' )
796 | ($HS+) }{ defined $2 ? ' ' : ($1 || '') }gsex;
797 $c =~ s!\s+$!!mg;
798 $c =~ s!^$LF!!mg;
799 $c =~ s!^\s*#\s*!#!mg;
800 $c =~ s!^\s+!!mg;
0d0f8426
MHM
801
802 open OUT, ">$0" or die "cannot strip $0: $!\n";
c83e6f19 803 print OUT "$pl$c\n";
0d0f8426
MHM
804
805 exit 0;
806}