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