Commit | Line | Data |
---|---|---|
9ad884cb JH |
1 | #!/usr/bin/perl -w |
2 | use strict; | |
e64a0c47 | 3 | our (@Changed, $TAP); |
424a4936 NC |
4 | use File::Compare; |
5 | use Symbol; | |
779d6b4a | 6 | use Text::Wrap(); |
9ad884cb JH |
7 | |
8 | # Common functions needed by the regen scripts | |
9 | ||
e64a0c47 | 10 | our $Needs_Write = $^O eq 'cygwin' || $^O eq 'os2' || $^O eq 'MSWin32'; |
9ad884cb | 11 | |
e64a0c47 | 12 | our $Verbose = 0; |
523b3031 | 13 | @ARGV = grep { not($_ eq '-q' and $Verbose = -1) } |
3879ea51 | 14 | grep { not($_ eq '--tap' and $TAP = 1) } |
523b3031 NC |
15 | grep { not($_ eq '-v' and $Verbose = 1) } @ARGV; |
16 | ||
17 | END { | |
18 | print STDOUT "Changed: @Changed\n" if @Changed; | |
19 | } | |
95aa0565 | 20 | |
9ad884cb JH |
21 | sub safer_unlink { |
22 | my @names = @_; | |
23 | my $cnt = 0; | |
24 | ||
25 | my $name; | |
26 | foreach $name (@names) { | |
27 | next unless -e $name; | |
28 | chmod 0777, $name if $Needs_Write; | |
29 | ( CORE::unlink($name) and ++$cnt | |
30 | or warn "Couldn't unlink $name: $!\n" ); | |
31 | } | |
32 | return $cnt; | |
33 | } | |
34 | ||
29c22b52 NC |
35 | # Open a new file. |
36 | sub open_new { | |
f1f44974 | 37 | my ($final_name, $mode, $header, $force) = @_; |
29c22b52 | 38 | my $name = $final_name . '-new'; |
0caa7203 | 39 | my $lang = $final_name =~ /\.pod$/ ? 'Pod' : |
23fd77c0 | 40 | $final_name =~ /\.(?:c|h|inc|tab|act)$/ ? 'C' : 'Perl'; |
f1f44974 NC |
41 | if ($force && -e $final_name) { |
42 | chmod 0777, $name if $Needs_Write; | |
43 | CORE::unlink $final_name | |
44 | or die "Couldn't unlink $final_name: $!\n"; | |
45 | } | |
424a4936 | 46 | my $fh = gensym; |
73437b64 NC |
47 | if (!defined $mode or $mode eq '>') { |
48 | if (-f $name) { | |
49 | unlink $name or die "$name exists but can't unlink: $!"; | |
50 | } | |
1ae6ead9 | 51 | open $fh, '>', $name or die "Can't create $name: $!"; |
73437b64 | 52 | } elsif ($mode eq '>>') { |
1ae6ead9 | 53 | open $fh, '>>', $name or die "Can't append to $name: $!"; |
73437b64 | 54 | } else { |
f1f44974 | 55 | die "Unhandled open mode '$mode'"; |
73437b64 | 56 | } |
f1f44974 NC |
57 | @{*$fh}{qw(name final_name lang force)} |
58 | = ($name, $final_name, $lang, $force); | |
424a4936 | 59 | binmode $fh; |
b9f84aa6 | 60 | print {$fh} read_only_top(lang => $lang, %$header) if $header; |
424a4936 NC |
61 | $fh; |
62 | } | |
63 | ||
284e4287 | 64 | sub close_and_rename { |
08858ed2 | 65 | my $fh = shift; |
f1f44974 | 66 | my ($name, $final_name, $force) = @{*{$fh}}{qw(name final_name force)}; |
284e4287 | 67 | close $fh or die "Error closing $name: $!"; |
284e4287 NC |
68 | |
69 | if ($TAP) { | |
5c25e937 | 70 | # Don't use compare because if there are errors it doesn't give any |
e206599a NC |
71 | # way to generate diagnostics about what went wrong. |
72 | # These files are small enough to read into memory. | |
73 | local $/; | |
74 | # This is the file we just closed, so it should open cleanly: | |
75 | open $fh, '<', $name | |
76 | or die "Can't open '$name': $!"; | |
77 | my $want = <$fh>; | |
78 | die "Can't read '$name': $!" | |
79 | unless defined $want; | |
80 | close $fh | |
81 | or die "Can't close '$name': $!"; | |
82 | ||
83 | my $fail; | |
84 | if (!open $fh, '<', $final_name) { | |
85 | $fail = "Can't open '$final_name': $!"; | |
86 | } else { | |
87 | my $have = <$fh>; | |
88 | if (!defined $have) { | |
89 | $fail = "Can't read '$final_name': $!"; | |
90 | close $fh; | |
91 | } elsif (!close $fh) { | |
92 | $fail = "Can't close '$final_name': $!"; | |
93 | } elsif ($want ne $have) { | |
94 | $fail = "'$name' and '$final_name' differ"; | |
95 | } | |
96 | } | |
97 | if ($fail) { | |
98 | print STDOUT "not ok - $0 $final_name\n"; | |
99 | print STDERR "$fail\n"; | |
100 | } else { | |
101 | print STDOUT "ok - $0 $final_name\n"; | |
102 | } | |
284e4287 NC |
103 | safer_unlink($name); |
104 | return; | |
105 | } | |
f1f44974 NC |
106 | unless ($force) { |
107 | if (compare($name, $final_name) == 0) { | |
108 | warn "no changes between '$name' & '$final_name'\n" if $Verbose > 0; | |
109 | safer_unlink($name); | |
110 | return; | |
111 | } | |
112 | warn "changed '$name' to '$final_name'\n" if $Verbose > 0; | |
113 | push @Changed, $final_name unless $Verbose < 0; | |
284e4287 | 114 | } |
284e4287 | 115 | |
b6a6e956 | 116 | # Some DOSish systems can't rename over an existing file: |
284e4287 NC |
117 | safer_unlink $final_name; |
118 | chmod 0600, $name if $Needs_Write; | |
119 | rename $name, $final_name or die "renaming $name to $final_name: $!"; | |
08858ed2 NC |
120 | } |
121 | ||
0caa7203 NC |
122 | my %lang_opener = (Perl => '# ', Pod => '', C => '/* '); |
123 | ||
78102347 NC |
124 | sub read_only_top { |
125 | my %args = @_; | |
755b4f2b NC |
126 | my $lang = $args{lang}; |
127 | die "Missing language argument" unless defined $lang; | |
128 | die "Unknown language argument '$lang'" | |
129 | unless exists $lang_opener{$lang}; | |
78102347 NC |
130 | my $style = $args{style} ? " $args{style} " : ' '; |
131 | ||
132 | my $raw = "-*- buffer-read-only: t -*-\n"; | |
133 | ||
134 | if ($args{file}) { | |
135 | $raw .= "\n $args{file}\n"; | |
136 | } | |
137 | if ($args{copyright}) { | |
138 | local $" = ', '; | |
779d6b4a | 139 | $raw .= wrap(75, ' ', ' ', <<"EOM") . "\n"; |
78102347 NC |
140 | |
141 | Copyright (C) @{$args{copyright}} by\0Larry\0Wall\0and\0others | |
142 | ||
143 | You may distribute under the terms of either the GNU General Public | |
144 | License or the Artistic License, as specified in the README file. | |
145 | EOM | |
146 | } | |
147 | ||
148 | $raw .= "!!!!!!! DO NOT EDIT THIS FILE !!!!!!!\n"; | |
149 | ||
150 | if ($args{by}) { | |
151 | $raw .= "This file is built by $args{by}"; | |
152 | if ($args{from}) { | |
153 | my @from = ref $args{from} eq 'ARRAY' ? @{$args{from}} : $args{from}; | |
154 | my $last = pop @from; | |
155 | if (@from) { | |
156 | $raw .= ' from ' . join (', ', @from) . " and $last"; | |
157 | } else { | |
158 | $raw .= " from $last"; | |
159 | } | |
160 | } | |
161 | $raw .= ".\n"; | |
162 | } | |
163 | $raw .= "Any changes made here will be lost!\n"; | |
164 | $raw .= $args{final} if $args{final}; | |
165 | ||
755b4f2b | 166 | my $cooked = $lang eq 'C' |
779d6b4a NC |
167 | ? wrap(78, '/* ', $style, $raw) . " */\n\n" |
168 | : wrap(78, $lang_opener{$lang}, $lang_opener{$lang}, $raw) . "\n"; | |
78102347 NC |
169 | $cooked =~ tr/\0/ /; # Don't break Larry's name etc |
170 | $cooked =~ s/ +$//mg; # Remove all trailing spaces | |
56fd1190 | 171 | $cooked =~ s! \*/\n!$args{quote}!s if $args{quote}; |
78102347 NC |
172 | return $cooked; |
173 | } | |
174 | ||
515c3fe0 NC |
175 | sub read_only_bottom_close_and_rename { |
176 | my ($fh, $sources) = @_; | |
755b4f2b | 177 | my ($name, $lang, $final_name) = @{*{$fh}}{qw(name lang final_name)}; |
515c3fe0 | 178 | die "No final name specified at open time for $name" |
755b4f2b | 179 | unless $final_name; |
88f1af60 | 180 | |
c24c946d NC |
181 | my $comment; |
182 | if ($sources) { | |
183 | $comment = "Generated from:\n"; | |
184 | foreach my $file (sort @$sources) { | |
fc809980 KW |
185 | my $digest = (-e $file) |
186 | ? digest($file) | |
187 | # Use a random number that won't match the real | |
188 | # digest, so will always show as out-of-date, so | |
189 | # Porting tests likely will fail drawing attention | |
190 | # to the problem. | |
191 | : int(rand(1_000_000)); | |
c24c946d NC |
192 | $comment .= "$digest $file\n"; |
193 | } | |
194 | } | |
195 | $comment .= "ex: set ro:"; | |
196 | ||
88f1af60 | 197 | if (defined $lang && $lang eq 'Perl') { |
c24c946d | 198 | $comment =~ s/^/# /mg; |
0caa7203 | 199 | } elsif (!defined $lang or $lang ne 'Pod') { |
c24c946d NC |
200 | $comment =~ s/^/ * /mg; |
201 | $comment =~ s! \* !/* !; | |
202 | $comment .= " */"; | |
203 | } | |
515c3fe0 | 204 | print $fh "\n$comment\n"; |
88f1af60 | 205 | |
284e4287 | 206 | close_and_rename($fh); |
f038801a NC |
207 | } |
208 | ||
3974d06f NC |
209 | sub tab { |
210 | my ($l, $t) = @_; | |
211 | $t .= "\t" x ($l - (length($t) + 1) / 8); | |
212 | $t; | |
213 | } | |
214 | ||
c24c946d NC |
215 | sub digest { |
216 | my $file = shift; | |
217 | # Need to defer loading this, as the main regen scripts work back to 5.004, | |
218 | # and likely we don't even have this module on every 5.8 install yet: | |
219 | require Digest::SHA; | |
220 | ||
221 | local ($/, *FH); | |
1ae6ead9 | 222 | open FH, '<', $file or die "Can't open $file: $!"; |
c24c946d NC |
223 | my $raw = <FH>; |
224 | close FH or die "Can't close $file: $!"; | |
225 | return Digest::SHA::sha256_hex($raw); | |
226 | }; | |
227 | ||
779d6b4a NC |
228 | sub wrap { |
229 | local $Text::Wrap::columns = shift; | |
230 | Text::Wrap::wrap(@_); | |
231 | } | |
232 | ||
99275276 DM |
233 | # return the perl version as defined in patchlevel.h. |
234 | # (we may be being run by another perl, so $] won't be right) | |
235 | # return e.g. (5, 14, 3, "5.014003") | |
236 | ||
237 | sub perl_version { | |
238 | my $plh = 'patchlevel.h'; | |
239 | open my $fh, "<", $plh or die "can't open '$plh': $!\n"; | |
240 | my ($v1,$v2,$v3); | |
241 | while (<$fh>) { | |
242 | $v1 = $1 if /PERL_REVISION\s+(\d+)/; | |
243 | $v2 = $1 if /PERL_VERSION\s+(\d+)/; | |
244 | $v3 = $1 if /PERL_SUBVERSION\s+(\d+)/; | |
245 | } | |
246 | die "can't locate PERL_REVISION in '$plh'" unless defined $v1; | |
247 | die "can't locate PERL_VERSION in '$plh'" unless defined $v2; | |
248 | die "can't locate PERL_SUBVERSION in '$plh'" unless defined $v3; | |
249 | return ($v1,$v2,$v3, sprintf("%d.%03d%03d", $v1, $v2, $v3)); | |
250 | } | |
251 | ||
252 | ||
9ad884cb | 253 | 1; |