Commit | Line | Data |
---|---|---|
9ad884cb JH |
1 | #!/usr/bin/perl -w |
2 | use strict; | |
3879ea51 | 3 | use vars qw($Needs_Write $Verbose @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 | ||
bb21036d | 10 | $Needs_Write = $^O eq 'cygwin' || $^O eq 'os2' || $^O eq 'MSWin32'; |
9ad884cb | 11 | |
523b3031 NC |
12 | $Verbose = 0; |
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 NC |
39 | my $lang = $final_name =~ /\.pod$/ ? 'Pod' : |
40 | $final_name =~ /\.(?:c|h|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 | } | |
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: $!"; | |
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) { | |
e206599a NC |
70 | # Don't use compare beacuse 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. | |
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) { | |
185 | my $digest = digest($file); | |
186 | $comment .= "$digest $file\n"; | |
187 | } | |
188 | } | |
189 | $comment .= "ex: set ro:"; | |
190 | ||
88f1af60 | 191 | if (defined $lang && $lang eq 'Perl') { |
c24c946d | 192 | $comment =~ s/^/# /mg; |
0caa7203 | 193 | } elsif (!defined $lang or $lang ne 'Pod') { |
c24c946d NC |
194 | $comment =~ s/^/ * /mg; |
195 | $comment =~ s! \* !/* !; | |
196 | $comment .= " */"; | |
197 | } | |
515c3fe0 | 198 | print $fh "\n$comment\n"; |
88f1af60 | 199 | |
284e4287 | 200 | close_and_rename($fh); |
f038801a NC |
201 | } |
202 | ||
3974d06f NC |
203 | sub tab { |
204 | my ($l, $t) = @_; | |
205 | $t .= "\t" x ($l - (length($t) + 1) / 8); | |
206 | $t; | |
207 | } | |
208 | ||
c24c946d NC |
209 | sub digest { |
210 | my $file = shift; | |
211 | # Need to defer loading this, as the main regen scripts work back to 5.004, | |
212 | # and likely we don't even have this module on every 5.8 install yet: | |
213 | require Digest::SHA; | |
214 | ||
215 | local ($/, *FH); | |
216 | open FH, "$file" or die "Can't open $file: $!"; | |
217 | my $raw = <FH>; | |
218 | close FH or die "Can't close $file: $!"; | |
219 | return Digest::SHA::sha256_hex($raw); | |
220 | }; | |
221 | ||
779d6b4a NC |
222 | sub wrap { |
223 | local $Text::Wrap::columns = shift; | |
224 | Text::Wrap::wrap(@_); | |
225 | } | |
226 | ||
9ad884cb | 227 | 1; |