8 # Common functions needed by the regen scripts
10 our $Needs_Write = $^O eq 'cygwin' || $^O eq 'os2' || $^O eq 'MSWin32';
13 @ARGV = grep { not($_ eq '-q' and $Verbose = -1) }
14 grep { not($_ eq '--tap' and $TAP = 1) }
15 grep { not($_ eq '-v' and $Verbose = 1) } @ARGV;
18 print STDOUT "Changed: @Changed\n" if @Changed;
26 foreach $name (@names) {
28 chmod 0777, $name if $Needs_Write;
29 ( CORE::unlink($name) and ++$cnt
30 or warn "Couldn't unlink $name: $!\n" );
37 my ($final_name, $mode, $header, $force) = @_;
38 my $name = $final_name . '-new';
39 my $lang = $final_name =~ /\.pod$/ ? 'Pod' :
40 $final_name =~ /\.(?:c|h|inc|tab|act)$/ ? 'C' : 'Perl';
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";
47 if (!defined $mode or $mode eq '>') {
49 unlink $name or die "$name exists but can't unlink: $!";
51 open $fh, '>', $name or die "Can't create $name: $!";
52 } elsif ($mode eq '>>') {
53 open $fh, '>>', $name or die "Can't append to $name: $!";
55 die "Unhandled open mode '$mode'";
57 @{*$fh}{qw(name final_name lang force)}
58 = ($name, $final_name, $lang, $force);
60 print {$fh} read_only_top(lang => $lang, %$header) if $header;
64 sub close_and_rename {
66 my ($name, $final_name, $force) = @{*{$fh}}{qw(name final_name force)};
67 close $fh or die "Error closing $name: $!";
70 # Don't use compare because if there are errors it doesn't give any
71 # way to generate diagnostics about what went wrong.
72 # These files are small enough to read into memory.
74 # This is the file we just closed, so it should open cleanly:
76 or die "Can't open '$name': $!";
78 die "Can't read '$name': $!"
81 or die "Can't close '$name': $!";
84 if (!open $fh, '<', $final_name) {
85 $fail = "Can't open '$final_name': $!";
89 $fail = "Can't read '$final_name': $!";
91 } elsif (!close $fh) {
92 $fail = "Can't close '$final_name': $!";
93 } elsif ($want ne $have) {
94 $fail = "'$name' and '$final_name' differ";
98 print STDOUT "not ok - $0 $final_name\n";
101 print STDOUT "ok - $0 $final_name\n";
107 if (compare($name, $final_name) == 0) {
108 warn "no changes between '$name' & '$final_name'\n" if $Verbose > 0;
112 warn "changed '$name' to '$final_name'\n" if $Verbose > 0;
113 push @Changed, $final_name unless $Verbose < 0;
116 # Some DOSish systems can't rename over an existing file:
117 safer_unlink $final_name;
118 chmod 0600, $name if $Needs_Write;
119 rename $name, $final_name or die "renaming $name to $final_name: $!";
122 my %lang_opener = (Perl => '# ', Pod => '', C => '/* ');
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};
130 my $style = $args{style} ? " $args{style} " : ' ';
132 my $raw = "-*- buffer-read-only: t -*-\n";
135 $raw .= "\n $args{file}\n";
137 if ($args{copyright}) {
139 $raw .= wrap(75, ' ', ' ', <<"EOM") . "\n";
141 Copyright (C) @{$args{copyright}} by\0Larry\0Wall\0and\0others
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.
148 $raw .= "!!!!!!! DO NOT EDIT THIS FILE !!!!!!!\n";
151 $raw .= "This file is built by $args{by}";
153 my @from = ref $args{from} eq 'ARRAY' ? @{$args{from}} : $args{from};
154 my $last = pop @from;
156 $raw .= ' from ' . join (', ', @from) . " and $last";
158 $raw .= " from $last";
163 $raw .= "Any changes made here will be lost!\n";
164 $raw .= $args{final} if $args{final};
166 my $cooked = $lang eq 'C'
167 ? wrap(78, '/* ', $style, $raw) . " */\n\n"
168 : wrap(78, $lang_opener{$lang}, $lang_opener{$lang}, $raw) . "\n";
169 $cooked =~ tr/\0/ /; # Don't break Larry's name etc
170 $cooked =~ s/ +$//mg; # Remove all trailing spaces
171 $cooked =~ s! \*/\n!$args{quote}!s if $args{quote};
175 sub read_only_bottom_close_and_rename {
176 my ($fh, $sources) = @_;
177 my ($name, $lang, $final_name) = @{*{$fh}}{qw(name lang final_name)};
178 die "No final name specified at open time for $name"
183 $comment = "Generated from:\n";
184 foreach my $file (sort @$sources) {
185 my $digest = (-e $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
191 : int(rand(1_000_000));
192 $comment .= "$digest $file\n";
195 $comment .= "ex: set ro:";
197 if (defined $lang && $lang eq 'Perl') {
198 $comment =~ s/^/# /mg;
199 } elsif (!defined $lang or $lang ne 'Pod') {
200 $comment =~ s/^/ * /mg;
201 $comment =~ s! \* !/* !;
204 print $fh "\n$comment\n";
206 close_and_rename($fh);
213 $t .= "\t" x ($l - (length($t) + 1) / 8);
219 # Need to defer loading this, as the main regen scripts work back to 5.004,
220 # and likely we don't even have this module on every 5.8 install yet:
224 open FH, '<', $file or die "Can't open $file: $!";
226 close FH or die "Can't close $file: $!";
227 return Digest::SHA::sha256_hex($raw);
231 local $Text::Wrap::columns = shift;
232 local $Text::Wrap::unexpand = 0;
233 Text::Wrap::wrap(@_);
236 # return the perl version as defined in patchlevel.h.
237 # (we may be being run by another perl, so $] won't be right)
238 # return e.g. (5, 14, 3, "5.014003")
241 my $plh = 'patchlevel.h';
242 open my $fh, "<", $plh or die "can't open '$plh': $!\n";
245 $v1 = $1 if /PERL_REVISION\s+(\d+)/;
246 $v2 = $1 if /PERL_VERSION\s+(\d+)/;
247 $v3 = $1 if /PERL_SUBVERSION\s+(\d+)/;
249 die "can't locate PERL_REVISION in '$plh'" unless defined $v1;
250 die "can't locate PERL_VERSION in '$plh'" unless defined $v2;
251 die "can't locate PERL_SUBVERSION in '$plh'" unless defined $v3;
252 return ($v1,$v2,$v3, sprintf("%d.%03d%03d", $v1, $v2, $v3));