This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In regen_lib.pl, when opening, first unlink a previous file of that name.
[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
35sub 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 44sub 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.
64sub safer_open {
65 my $name = shift;
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;
424a4936
NC
72 binmode $fh;
73 $fh;
74}
75
08858ed2
NC
76sub safer_close {
77 my $fh = shift;
bb5cff7d 78 close $fh or die 'Error closing ' . *{$fh}->{name} . ": $!";
08858ed2
NC
79}
80
78102347
NC
81sub read_only_top {
82 my %args = @_;
83 die "Missing language argument" unless defined $args{lang};
84 die "Unknown language argument '$args{lang}'"
85 unless $args{lang} eq 'Perl' or $args{lang} eq 'C';
86 my $style = $args{style} ? " $args{style} " : ' ';
87
88 my $raw = "-*- buffer-read-only: t -*-\n";
89
90 if ($args{file}) {
91 $raw .= "\n $args{file}\n";
92 }
93 if ($args{copyright}) {
94 local $" = ', ';
95 local $Text::Wrap::columns = 75;
96 $raw .= wrap(' ', ' ', <<"EOM") . "\n";
97
98Copyright (C) @{$args{copyright}} by\0Larry\0Wall\0and\0others
99
100You may distribute under the terms of either the GNU General Public
101License or the Artistic License, as specified in the README file.
102EOM
103 }
104
105 $raw .= "!!!!!!! DO NOT EDIT THIS FILE !!!!!!!\n";
106
107 if ($args{by}) {
108 $raw .= "This file is built by $args{by}";
109 if ($args{from}) {
110 my @from = ref $args{from} eq 'ARRAY' ? @{$args{from}} : $args{from};
111 my $last = pop @from;
112 if (@from) {
113 $raw .= ' from ' . join (', ', @from) . " and $last";
114 } else {
115 $raw .= " from $last";
116 }
117 }
118 $raw .= ".\n";
119 }
120 $raw .= "Any changes made here will be lost!\n";
121 $raw .= $args{final} if $args{final};
122
123 local $Text::Wrap::columns = 78;
124 my $cooked = $args{lang} eq 'Perl'
125 ? wrap('# ', '# ', $raw) . "\n" : wrap('/* ', $style, $raw) . " */\n\n";
126 $cooked =~ tr/\0/ /; # Don't break Larry's name etc
127 $cooked =~ s/ +$//mg; # Remove all trailing spaces
128 return $cooked;
129}
130
9ad884cb 1311;