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; | |
78102347 | 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 | ||
35 | sub safer_rename_silent { | |
36 | my ($from, $to) = @_; | |
37 | ||
38 | # Some dosish systems can't rename over an existing file: | |
39 | safer_unlink $to; | |
40 | chmod 0600, $from if $Needs_Write; | |
41 | rename $from, $to; | |
42 | } | |
43 | ||
424a4936 | 44 | sub rename_if_different { |
9ad884cb | 45 | my ($from, $to) = @_; |
b6b9a099 | 46 | |
3879ea51 NC |
47 | if ($TAP) { |
48 | my $not = compare($from, $to) ? 'not ' : ''; | |
49 | print STDOUT $not . "ok - $0 $to\n"; | |
50 | safer_unlink($from); | |
51 | return; | |
52 | } | |
424a4936 | 53 | if (compare($from, $to) == 0) { |
523b3031 | 54 | warn "no changes between '$from' & '$to'\n" if $Verbose > 0; |
b6b9a099 JC |
55 | safer_unlink($from); |
56 | return; | |
57 | } | |
523b3031 NC |
58 | warn "changed '$from' to '$to'\n" if $Verbose > 0; |
59 | push @Changed, $to unless $Verbose < 0; | |
9ad884cb JH |
60 | safer_rename_silent($from, $to) or die "renaming $from to $to: $!"; |
61 | } | |
424a4936 NC |
62 | |
63 | # Saf*er*, but not totally safe. And assumes always open for output. | |
64 | sub safer_open { | |
f038801a | 65 | my ($name, $final_name) = @_; |
396ce246 NC |
66 | if (-f $name) { |
67 | unlink $name or die "$name exists but can't unlink: $!"; | |
68 | } | |
424a4936 NC |
69 | my $fh = gensym; |
70 | open $fh, ">$name" or die "Can't create $name: $!"; | |
bb5cff7d | 71 | *{$fh}->{name} = $name; |
ce716c52 NC |
72 | if (defined $final_name) { |
73 | *{$fh}->{final_name} = $final_name; | |
e8fb9efb | 74 | *{$fh}->{lang} = ($final_name =~ /\.(?:c|h|tab|act)$/ ? 'C' : 'Perl'); |
ce716c52 | 75 | } |
424a4936 NC |
76 | binmode $fh; |
77 | $fh; | |
78 | } | |
79 | ||
08858ed2 NC |
80 | sub safer_close { |
81 | my $fh = shift; | |
bb5cff7d | 82 | close $fh or die 'Error closing ' . *{$fh}->{name} . ": $!"; |
08858ed2 NC |
83 | } |
84 | ||
78102347 NC |
85 | sub read_only_top { |
86 | my %args = @_; | |
87 | die "Missing language argument" unless defined $args{lang}; | |
88 | die "Unknown language argument '$args{lang}'" | |
89 | unless $args{lang} eq 'Perl' or $args{lang} eq 'C'; | |
90 | my $style = $args{style} ? " $args{style} " : ' '; | |
91 | ||
92 | my $raw = "-*- buffer-read-only: t -*-\n"; | |
93 | ||
94 | if ($args{file}) { | |
95 | $raw .= "\n $args{file}\n"; | |
96 | } | |
97 | if ($args{copyright}) { | |
98 | local $" = ', '; | |
99 | local $Text::Wrap::columns = 75; | |
100 | $raw .= wrap(' ', ' ', <<"EOM") . "\n"; | |
101 | ||
102 | Copyright (C) @{$args{copyright}} by\0Larry\0Wall\0and\0others | |
103 | ||
104 | You may distribute under the terms of either the GNU General Public | |
105 | License or the Artistic License, as specified in the README file. | |
106 | EOM | |
107 | } | |
108 | ||
109 | $raw .= "!!!!!!! DO NOT EDIT THIS FILE !!!!!!!\n"; | |
110 | ||
111 | if ($args{by}) { | |
112 | $raw .= "This file is built by $args{by}"; | |
113 | if ($args{from}) { | |
114 | my @from = ref $args{from} eq 'ARRAY' ? @{$args{from}} : $args{from}; | |
115 | my $last = pop @from; | |
116 | if (@from) { | |
117 | $raw .= ' from ' . join (', ', @from) . " and $last"; | |
118 | } else { | |
119 | $raw .= " from $last"; | |
120 | } | |
121 | } | |
122 | $raw .= ".\n"; | |
123 | } | |
124 | $raw .= "Any changes made here will be lost!\n"; | |
125 | $raw .= $args{final} if $args{final}; | |
126 | ||
127 | local $Text::Wrap::columns = 78; | |
128 | my $cooked = $args{lang} eq 'Perl' | |
129 | ? wrap('# ', '# ', $raw) . "\n" : wrap('/* ', $style, $raw) . " */\n\n"; | |
130 | $cooked =~ tr/\0/ /; # Don't break Larry's name etc | |
131 | $cooked =~ s/ +$//mg; # Remove all trailing spaces | |
132 | return $cooked; | |
133 | } | |
134 | ||
ce716c52 | 135 | sub read_only_bottom_close_and_rename { |
c24c946d | 136 | my ($fh, $sources) = @_; |
f038801a | 137 | my $name = *{$fh}->{name}; |
ce716c52 | 138 | my $lang = *{$fh}->{lang}; |
f038801a NC |
139 | die "No final name specified at open time for $name" |
140 | unless *{$fh}->{final_name}; | |
c24c946d NC |
141 | my $comment; |
142 | if ($sources) { | |
143 | $comment = "Generated from:\n"; | |
144 | foreach my $file (sort @$sources) { | |
145 | my $digest = digest($file); | |
146 | $comment .= "$digest $file\n"; | |
147 | } | |
148 | } | |
149 | $comment .= "ex: set ro:"; | |
150 | ||
151 | if ($lang eq 'Perl') { | |
152 | $comment =~ s/^/# /mg; | |
153 | } else { | |
154 | $comment =~ s/^/ * /mg; | |
155 | $comment =~ s! \* !/* !; | |
156 | $comment .= " */"; | |
157 | } | |
158 | print $fh "\n$comment\n"; | |
f038801a NC |
159 | safer_close($fh); |
160 | rename_if_different($name, *{$fh}->{final_name}); | |
161 | } | |
162 | ||
3974d06f NC |
163 | sub tab { |
164 | my ($l, $t) = @_; | |
165 | $t .= "\t" x ($l - (length($t) + 1) / 8); | |
166 | $t; | |
167 | } | |
168 | ||
c24c946d NC |
169 | sub digest { |
170 | my $file = shift; | |
171 | # Need to defer loading this, as the main regen scripts work back to 5.004, | |
172 | # and likely we don't even have this module on every 5.8 install yet: | |
173 | require Digest::SHA; | |
174 | ||
175 | local ($/, *FH); | |
176 | open FH, "$file" or die "Can't open $file: $!"; | |
177 | my $raw = <FH>; | |
178 | close FH or die "Can't close $file: $!"; | |
179 | return Digest::SHA::sha256_hex($raw); | |
180 | }; | |
181 | ||
9ad884cb | 182 | 1; |