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