This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
avoid copying the while ebcidic mapping to the stack calling get_a2n()
[perl5.git] / regen / regen_lib.pl
index 9008629..b64e0b0 100644 (file)
@@ -3,6 +3,7 @@ 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
 
@@ -31,47 +32,196 @@ sub safer_unlink {
   return $cnt;
 }
 
-sub safer_rename_silent {
-  my ($from, $to) = @_;
+# Open a new file.
+sub open_new {
+    my ($final_name, $mode, $header, $force) = @_;
+    my $name = $final_name . '-new';
+    my $lang = $final_name =~ /\.pod$/ ? 'Pod' :
+       $final_name =~ /\.(?:c|h|tab|act)$/ ? 'C' : 'Perl';
+    if ($force && -e $final_name) {
+        chmod 0777, $name if $Needs_Write;
+        CORE::unlink $final_name
+                or die "Couldn't unlink $final_name: $!\n";
+    }
+    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}{qw(name final_name lang force)}
+        = ($name, $final_name, $lang, $force);
+    binmode $fh;
+    print {$fh} read_only_top(lang => $lang, %$header) if $header;
+    $fh;
+}
+
+sub close_and_rename {
+    my $fh = shift;
+    my ($name, $final_name, $force) = @{*{$fh}}{qw(name final_name force)};
+    close $fh or die "Error closing $name: $!";
+
+    if ($TAP) {
+        # Don't use compare beacuse if there are errors it doesn't give any
+        # way to generate diagnostics about what went wrong.
+        # These files are small enough to read into memory.
+        local $/;
+        # This is the file we just closed, so it should open cleanly:
+        open $fh, '<', $name
+            or die "Can't open '$name': $!";
+        my $want = <$fh>;
+        die "Can't read '$name': $!"
+            unless defined $want;
+        close $fh
+            or die "Can't close '$name': $!";
 
-  # Some dosish systems can't rename over an existing file:
-  safer_unlink $to;
-  chmod 0600, $from if $Needs_Write;
-  rename $from, $to;
+        my $fail;
+        if (!open $fh, '<', $final_name) {
+            $fail = "Can't open '$final_name': $!";
+        } else {
+            my $have = <$fh>;
+            if (!defined $have) {
+                $fail = "Can't read '$final_name': $!";
+                close $fh;
+            } elsif (!close $fh) {
+                $fail = "Can't close '$final_name': $!";
+            } elsif ($want ne $have) {
+                $fail = "'$name' and '$final_name' differ";
+            }
+        }
+        if ($fail) {
+            print STDOUT "not ok - $0 $final_name\n";
+            print STDERR "$fail\n";
+        } else {
+            print STDOUT "ok - $0 $final_name\n";
+        }
+       safer_unlink($name);
+       return;
+    }
+    unless ($force) {
+        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 rename_if_different {
-  my ($from, $to) = @_;
+my %lang_opener = (Perl => '# ', Pod => '', C => '/* ');
 
-  if ($TAP) {
-      my $not = compare($from, $to) ? 'not ' : '';
-      print STDOUT $not . "ok - $0 $to\n";
-      safer_unlink($from);
-      return;
-  }
-  if (compare($from, $to) == 0) {
-      warn "no changes between '$from' & '$to'\n" if $Verbose > 0;
-      safer_unlink($from);
-      return;
-  }
-  warn "changed '$from' to '$to'\n" if $Verbose > 0;
-  push @Changed, $to unless $Verbose < 0;
-  safer_rename_silent($from, $to) or die "renaming $from to $to: $!";
+sub read_only_top {
+    my %args = @_;
+    my $lang = $args{lang};
+    die "Missing language argument" unless defined $lang;
+    die "Unknown language argument '$lang'"
+        unless exists $lang_opener{$lang};
+    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 $" = ', ';
+         $raw .= wrap(75, '   ', '   ', <<"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};
+
+    my $cooked = $lang eq 'C'
+        ? wrap(78, '/* ', $style, $raw) . " */\n\n"
+        : wrap(78, $lang_opener{$lang}, $lang_opener{$lang}, $raw) . "\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;
 }
 
-# Saf*er*, but not totally safe. And assumes always open for output.
-sub safer_open {
-    my $name = shift;
-    my $fh = gensym;
-    open $fh, ">$name" or die "Can't create $name: $!";
-    *{$fh}->{SCALAR} = $name;
-    binmode $fh;
-    $fh;
+sub read_only_bottom_close_and_rename {
+    my ($fh, $sources) = @_;
+    my ($name, $lang, $final_name) = @{*{$fh}}{qw(name lang final_name)};
+    die "No final name specified at open time for $name"
+        unless $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;
+    } elsif (!defined $lang or $lang ne 'Pod') {
+       $comment =~ s/^/ * /mg;
+       $comment =~ s! \* !/* !;
+       $comment .= " */";
+    }
+    print $fh "\n$comment\n";
+
+    close_and_rename($fh);
 }
 
-sub safer_close {
-    my $fh = shift;
-    close $fh or die 'Error closing ' . *{$fh}->{SCALAR} . ": $!";
+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);
+};
+
+sub wrap {
+    local $Text::Wrap::columns = shift;
+    Text::Wrap::wrap(@_);
 }
 
 1;