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