This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
d8cbd12bf5f61858575c0f13c47a8f4828e70344
[perl5.git] / regen / regen_lib.pl
1 #!/usr/bin/perl -w
2 use strict;
3 use vars qw($Needs_Write $Verbose @Changed $TAP);
4 use File::Compare;
5 use Symbol;
6 use Text::Wrap;
7
8 # Common functions needed by the regen scripts
9
10 $Needs_Write = $^O eq 'cygwin' || $^O eq 'os2' || $^O eq 'MSWin32';
11
12 $Verbose = 0;
13 @ARGV = grep { not($_ eq '-q' and $Verbose = -1) }
14   grep { not($_ eq '--tap' and $TAP = 1) }
15   grep { not($_ eq '-v' and $Verbose = 1) } @ARGV;
16
17 END {
18   print STDOUT "Changed: @Changed\n" if @Changed;
19 }
20
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
44 sub rename_if_different {
45   my ($from, $to) = @_;
46
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   }
53   if (compare($from, $to) == 0) {
54       warn "no changes between '$from' & '$to'\n" if $Verbose > 0;
55       safer_unlink($from);
56       return;
57   }
58   warn "changed '$from' to '$to'\n" if $Verbose > 0;
59   push @Changed, $to unless $Verbose < 0;
60   safer_rename_silent($from, $to) or die "renaming $from to $to: $!";
61 }
62
63 # Saf*er*, but not totally safe. And assumes always open for output.
64 sub safer_open {
65     my ($name, $final_name) = @_;
66     if (-f $name) {
67         unlink $name or die "$name exists but can't unlink: $!";
68     }
69     my $fh = gensym;
70     open $fh, ">$name" or die "Can't create $name: $!";
71     *{$fh}->{name} = $name;
72     *{$fh}->{final_name} = $final_name if defined $final_name;
73     binmode $fh;
74     $fh;
75 }
76
77 sub safer_close {
78     my $fh = shift;
79     close $fh or die 'Error closing ' . *{$fh}->{name} . ": $!";
80 }
81
82 sub read_only_top {
83     my %args = @_;
84     die "Missing language argument" unless defined $args{lang};
85     die "Unknown language argument '$args{lang}'"
86         unless $args{lang} eq 'Perl' or $args{lang} eq 'C';
87     my $style = $args{style} ? " $args{style} " : '   ';
88
89     my $raw = "-*- buffer-read-only: t -*-\n";
90
91     if ($args{file}) {
92         $raw .= "\n   $args{file}\n";
93     }
94     if ($args{copyright}) {
95         local $" = ', ';
96         local $Text::Wrap::columns = 75;
97         $raw .= wrap('   ', '   ', <<"EOM") . "\n";
98
99 Copyright (C) @{$args{copyright}} by\0Larry\0Wall\0and\0others
100
101 You may distribute under the terms of either the GNU General Public
102 License or the Artistic License, as specified in the README file.
103 EOM
104     }
105
106     $raw .= "!!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!\n";
107
108     if ($args{by}) {
109         $raw .= "This file is built by $args{by}";
110         if ($args{from}) {
111             my @from = ref $args{from} eq 'ARRAY' ? @{$args{from}} : $args{from};
112             my $last = pop @from;
113             if (@from) {
114                 $raw .= ' from ' . join (', ', @from) . " and $last";
115             } else {
116                 $raw .= " from $last";
117             }
118         }
119         $raw .= ".\n";
120     }
121     $raw .= "Any changes made here will be lost!\n";
122     $raw .= $args{final} if $args{final};
123
124     local $Text::Wrap::columns = 78;
125     my $cooked = $args{lang} eq 'Perl'
126         ? wrap('# ', '# ', $raw) . "\n" : wrap('/* ', $style, $raw) . " */\n\n";
127     $cooked =~ tr/\0/ /; # Don't break Larry's name etc
128     $cooked =~ s/ +$//mg; # Remove all trailing spaces
129     return $cooked;
130 }
131
132 sub close_and_rename {
133     my $fh = shift;
134     my $name = *{$fh}->{name};
135     die "No final name specified at open time for $name"
136         unless *{$fh}->{final_name};
137     safer_close($fh);
138     rename_if_different($name, *{$fh}->{final_name});
139 }
140
141 1;