1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
#!/usr/bin/perl -w
use strict;
use vars qw($Needs_Write $Verbose @Changed $TAP);
use File::Compare;
use Symbol;
use Text::Wrap;
# Common functions needed by the regen scripts
$Needs_Write = $^O eq 'cygwin' || $^O eq 'os2' || $^O eq 'MSWin32';
$Verbose = 0;
@ARGV = grep { not($_ eq '-q' and $Verbose = -1) }
grep { not($_ eq '--tap' and $TAP = 1) }
grep { not($_ eq '-v' and $Verbose = 1) } @ARGV;
END {
print STDOUT "Changed: @Changed\n" if @Changed;
}
sub safer_unlink {
my @names = @_;
my $cnt = 0;
my $name;
foreach $name (@names) {
next unless -e $name;
chmod 0777, $name if $Needs_Write;
( CORE::unlink($name) and ++$cnt
or warn "Couldn't unlink $name: $!\n" );
}
return $cnt;
}
# Open a new file.
sub open_new {
my ($final_name, $mode, $header) = @_;
my $name = $final_name . '-new';
my $lang = $final_name =~ /\.(?:c|h|tab|act)$/ ? 'C' : 'Perl';
my $fh = gensym;
if (!defined $mode or $mode eq '>') {
if (-f $name) {
unlink $name or die "$name exists but can't unlink: $!";
}
open $fh, ">$name" or die "Can't create $name: $!";
} elsif ($mode eq '>>') {
open $fh, ">>$name" or die "Can't append to $name: $!";
} else {
die "Unhandled open mode '$mode#";
}
*{$fh}->{name} = $name;
*{$fh}->{final_name} = $final_name;
*{$fh}->{lang} = $lang;
binmode $fh;
print $fh read_only_top(lang => $lang, %$header) if $header;
$fh;
}
sub close_and_rename {
my $fh = shift;
my $name = *{$fh}->{name};
close $fh or die "Error closing $name: $!";
my $final_name = *{$fh}->{final_name};
if ($TAP) {
my $not = compare($name, $final_name) ? 'not ' : '';
print STDOUT $not . "ok - $0 $final_name\n";
safer_unlink($name);
return;
}
if (compare($name, $final_name) == 0) {
warn "no changes between '$name' & '$final_name'\n" if $Verbose > 0;
safer_unlink($name);
return;
}
warn "changed '$name' to '$final_name'\n" if $Verbose > 0;
push @Changed, $final_name unless $Verbose < 0;
# Some dosish systems can't rename over an existing file:
safer_unlink $final_name;
chmod 0600, $name if $Needs_Write;
rename $name, $final_name or die "renaming $name to $final_name: $!";
}
sub read_only_top {
my %args = @_;
die "Missing language argument" unless defined $args{lang};
die "Unknown language argument '$args{lang}'"
unless $args{lang} eq 'Perl' or $args{lang} eq 'C';
my $style = $args{style} ? " $args{style} " : ' ';
my $raw = "-*- buffer-read-only: t -*-\n";
if ($args{file}) {
$raw .= "\n $args{file}\n";
}
if ($args{copyright}) {
local $" = ', ';
local $Text::Wrap::columns = 75;
$raw .= wrap(' ', ' ', <<"EOM") . "\n";
Copyright (C) @{$args{copyright}} by\0Larry\0Wall\0and\0others
You may distribute under the terms of either the GNU General Public
License or the Artistic License, as specified in the README file.
EOM
}
$raw .= "!!!!!!! DO NOT EDIT THIS FILE !!!!!!!\n";
if ($args{by}) {
$raw .= "This file is built by $args{by}";
if ($args{from}) {
my @from = ref $args{from} eq 'ARRAY' ? @{$args{from}} : $args{from};
my $last = pop @from;
if (@from) {
$raw .= ' from ' . join (', ', @from) . " and $last";
} else {
$raw .= " from $last";
}
}
$raw .= ".\n";
}
$raw .= "Any changes made here will be lost!\n";
$raw .= $args{final} if $args{final};
local $Text::Wrap::columns = 78;
my $cooked = $args{lang} eq 'Perl'
? wrap('# ', '# ', $raw) . "\n" : wrap('/* ', $style, $raw) . " */\n\n";
$cooked =~ tr/\0/ /; # Don't break Larry's name etc
$cooked =~ s/ +$//mg; # Remove all trailing spaces
$cooked =~ s! \*/\n!$args{quote}!s if $args{quote};
return $cooked;
}
sub read_only_bottom_close_and_rename {
my ($fh, $sources) = @_;
my $name = *{$fh}->{name};
my $lang = *{$fh}->{lang};
die "No final name specified at open time for $name"
unless *{$fh}->{final_name};
my $comment;
if ($sources) {
$comment = "Generated from:\n";
foreach my $file (sort @$sources) {
my $digest = digest($file);
$comment .= "$digest $file\n";
}
}
$comment .= "ex: set ro:";
if (defined $lang && $lang eq 'Perl') {
$comment =~ s/^/# /mg;
} else {
$comment =~ s/^/ * /mg;
$comment =~ s! \* !/* !;
$comment .= " */";
}
print $fh "\n$comment\n";
close_and_rename($fh);
}
sub tab {
my ($l, $t) = @_;
$t .= "\t" x ($l - (length($t) + 1) / 8);
$t;
}
sub digest {
my $file = shift;
# Need to defer loading this, as the main regen scripts work back to 5.004,
# and likely we don't even have this module on every 5.8 install yet:
require Digest::SHA;
local ($/, *FH);
open FH, "$file" or die "Can't open $file: $!";
my $raw = <FH>;
close FH or die "Can't close $file: $!";
return Digest::SHA::sha256_hex($raw);
};
1;
|