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