This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Sort magic and magic vtable names in files generated by mg_vtable.pl
[perl5.git] / regen / regen_lib.pl
CommitLineData
9ad884cb
JH
1#!/usr/bin/perl -w
2use strict;
3879ea51 3use vars qw($Needs_Write $Verbose @Changed $TAP);
424a4936
NC
4use File::Compare;
5use Symbol;
78102347 6use 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
17END {
18 print STDOUT "Changed: @Changed\n" if @Changed;
19}
95aa0565 20
9ad884cb
JH
21sub 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.
36sub open_new {
cc49830d 37 my ($final_name, $mode, $header) = @_;
29c22b52 38 my $name = $final_name . '-new';
cc49830d 39 my $lang = $final_name =~ /\.(?:c|h|tab|act)$/ ? 'C' : 'Perl';
424a4936 40 my $fh = gensym;
73437b64
NC
41 if (!defined $mode or $mode eq '>') {
42 if (-f $name) {
43 unlink $name or die "$name exists but can't unlink: $!";
44 }
45 open $fh, ">$name" or die "Can't create $name: $!";
46 } elsif ($mode eq '>>') {
47 open $fh, ">>$name" or die "Can't append to $name: $!";
48 } else {
49 die "Unhandled open mode '$mode#";
50 }
bb5cff7d 51 *{$fh}->{name} = $name;
29c22b52 52 *{$fh}->{final_name} = $final_name;
cc49830d 53 *{$fh}->{lang} = $lang;
424a4936 54 binmode $fh;
cc49830d 55 print $fh read_only_top(lang => $lang, %$header) if $header;
424a4936
NC
56 $fh;
57}
58
284e4287 59sub close_and_rename {
08858ed2 60 my $fh = shift;
284e4287
NC
61 my $name = *{$fh}->{name};
62 close $fh or die "Error closing $name: $!";
63 my $final_name = *{$fh}->{final_name};
64
65 if ($TAP) {
66 my $not = compare($name, $final_name) ? 'not ' : '';
67 print STDOUT $not . "ok - $0 $final_name\n";
68 safer_unlink($name);
69 return;
70 }
71 if (compare($name, $final_name) == 0) {
72 warn "no changes between '$name' & '$final_name'\n" if $Verbose > 0;
73 safer_unlink($name);
74 return;
75 }
76 warn "changed '$name' to '$final_name'\n" if $Verbose > 0;
77 push @Changed, $final_name unless $Verbose < 0;
78
79 # Some dosish systems can't rename over an existing file:
80 safer_unlink $final_name;
81 chmod 0600, $name if $Needs_Write;
82 rename $name, $final_name or die "renaming $name to $final_name: $!";
08858ed2
NC
83}
84
78102347
NC
85sub 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
102Copyright (C) @{$args{copyright}} by\0Larry\0Wall\0and\0others
103
104You may distribute under the terms of either the GNU General Public
105License or the Artistic License, as specified in the README file.
106EOM
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
56fd1190 132 $cooked =~ s! \*/\n!$args{quote}!s if $args{quote};
78102347
NC
133 return $cooked;
134}
135
515c3fe0
NC
136sub read_only_bottom_close_and_rename {
137 my ($fh, $sources) = @_;
138 my $name = *{$fh}->{name};
139 my $lang = *{$fh}->{lang};
140 die "No final name specified at open time for $name"
141 unless *{$fh}->{final_name};
88f1af60 142
c24c946d
NC
143 my $comment;
144 if ($sources) {
145 $comment = "Generated from:\n";
146 foreach my $file (sort @$sources) {
147 my $digest = digest($file);
148 $comment .= "$digest $file\n";
149 }
150 }
151 $comment .= "ex: set ro:";
152
88f1af60 153 if (defined $lang && $lang eq 'Perl') {
c24c946d
NC
154 $comment =~ s/^/# /mg;
155 } else {
156 $comment =~ s/^/ * /mg;
157 $comment =~ s! \* !/* !;
158 $comment .= " */";
159 }
515c3fe0 160 print $fh "\n$comment\n";
88f1af60 161
284e4287 162 close_and_rename($fh);
f038801a
NC
163}
164
3974d06f
NC
165sub tab {
166 my ($l, $t) = @_;
167 $t .= "\t" x ($l - (length($t) + 1) / 8);
168 $t;
169}
170
c24c946d
NC
171sub digest {
172 my $file = shift;
173 # Need to defer loading this, as the main regen scripts work back to 5.004,
174 # and likely we don't even have this module on every 5.8 install yet:
175 require Digest::SHA;
176
177 local ($/, *FH);
178 open FH, "$file" or die "Can't open $file: $!";
179 my $raw = <FH>;
180 close FH or die "Can't close $file: $!";
181 return Digest::SHA::sha256_hex($raw);
182};
183
9ad884cb 1841;