add -v to regen.pl and friends
[p5sagit/p5-mst-13.2.git] / regen_lib.pl
1 #!/usr/bin/perl -w
2 use strict;
3 use vars qw($Is_W32 $Is_OS2 $Is_Cygwin $Is_NetWare $Needs_Write $Verbose);
4 use Config; # Remember, this is running using an existing perl
5 use File::Compare;
6 use Symbol;
7
8 # Common functions needed by the regen scripts
9
10 $Is_W32 = $^O eq 'MSWin32';
11 $Is_OS2 = $^O eq 'os2';
12 $Is_Cygwin = $^O eq 'cygwin';
13 $Is_NetWare = $Config{osname} eq 'NetWare';
14 if ($Is_NetWare) {
15   $Is_W32 = 0;
16 }
17
18 $Needs_Write = $Is_OS2 || $Is_W32 || $Is_Cygwin || $Is_NetWare;
19
20 @ARGV = grep { not($_ eq '-v' and $Verbose = 1) } @ARGV;
21
22 sub safer_unlink {
23   my @names = @_;
24   my $cnt = 0;
25
26   my $name;
27   foreach $name (@names) {
28     next unless -e $name;
29     chmod 0777, $name if $Needs_Write;
30     ( CORE::unlink($name) and ++$cnt
31       or warn "Couldn't unlink $name: $!\n" );
32   }
33   return $cnt;
34 }
35
36 sub safer_rename_silent {
37   my ($from, $to) = @_;
38
39   # Some dosish systems can't rename over an existing file:
40   safer_unlink $to;
41   chmod 0600, $from if $Needs_Write;
42   rename $from, $to;
43 }
44
45 sub rename_if_different {
46   my ($from, $to) = @_;
47
48   if (compare($from, $to) == 0) {
49       warn "no changes between '$from' & '$to'\n" if $Verbose;
50       safer_unlink($from);
51       return;
52   }
53   warn "changed '$from' to '$to'\n";
54   safer_rename_silent($from, $to) or die "renaming $from to $to: $!";
55 }
56
57 # Saf*er*, but not totally safe. And assumes always open for output.
58 sub safer_open {
59     my $name = shift;
60     my $fh = gensym;
61     open $fh, ">$name" or die "Can't create $name: $!";
62     *{$fh}->{SCALAR} = $name;
63     binmode $fh;
64     $fh;
65 }
66
67 sub safer_close {
68     my $fh = shift;
69     close $fh or die 'Error closing ' . *{$fh}->{SCALAR} . ": $!";
70 }
71
72 1;