Should fix the infinite loop on a dynamic %ENV fetch
[p5sagit/p5-mst-13.2.git] / utils / c2ph.PL
index e732d4d..91ecc04 100644 (file)
@@ -2,6 +2,29 @@
 
 use Config;
 use File::Basename qw(&basename &dirname);
+use Cwd;
+use subs qw(link);
+
+sub link { # This is a cut-down version of installperl:link().
+    my($from,$to) = @_;
+    my($success) = 0;
+
+    eval {
+       CORE::link($from, $to)
+           ? $success++
+           : ($from =~ m#^/afs/# || $to =~ m#^/afs/#)
+             ? die "AFS"  # okay inside eval {}
+             : die "Couldn't link $from to $to: $!\n";
+    };
+    if ($@) {
+       warn $@;
+       require File::Copy;
+       File::Copy::copy($from, $to)
+           ? $success++
+           : warn "Couldn't copy $from to $to: $!\n";
+    }
+    $success;
+}
 
 # List explicitly here the variables you want Configure to
 # generate.  Metaconfig only looks for shell variables, so you
@@ -12,6 +35,7 @@ use File::Basename qw(&basename &dirname);
 
 # This forces PL files to create target in same directory as PL file.
 # This is so that make depend always knows where to find PL derivatives.
+$origdir = cwd;
 chdir dirname($0);
 $file = basename($0, '.PL');
 $file .= '.com' if $^O eq 'VMS';
@@ -160,9 +184,9 @@ declarations at least, but that's quite a bit.
 
 Prior to this point, anyone programming in perl who wanted to interact
 with C programs, like the kernel, was forced to guess the layouts of
-the C strutures, and then hardwire these into his program.  Of course,
+the C structures, and then hardwire these into his program.  Of course,
 when you took your wonderfully crafted program to a system where the
-sgtty structure was laid out differently, you program broke.  Which is
+sgtty structure was laid out differently, your program broke.  Which is
 a shame.
 
 We've had Larry's h2ph translator, which helped, but that only works on
@@ -329,13 +353,25 @@ delete $intrinsics{'null'};
 $indent = 2;
 
 $CC = 'cc';
-$CFLAGS = '-g -S';
+!NO!SUBS!
+
+if (($Config{gccversion} || '') =~ /^(\d+)\.(\d+)/
+  and ($1 > 3 or ($1 == 3 and $2 >= 2))) {
+    print OUT q/$CFLAGS = '-gstabs -S';/;
+} else {
+    print OUT q/$CFLAGS = '-g -S';/;
+}
+
+print OUT <<'!NO!SUBS!';
+
 $DEFINES = '';
 
 $perl++ if $0 =~ m#/?c2ph$#;
 
 require 'getopts.pl';
 
+use File::Temp 'tempdir';
+
 eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift;
 
 &Getopts('aixdpvtnws:') || &usage(0);
@@ -464,9 +500,10 @@ if (@ARGV) {
        $ARGV[0] =~ s/\.c$/.s/;
     }
     else {
-       $TMP = "/tmp/c2ph.$$.c";
+       $TMPDIR = tempdir(CLEANUP => 1);
+       $TMP = "$TMPDIR/c2ph.$$.c";
        &system("cat @ARGV > $TMP") && exit 1;
-       &system("cd /tmp; $CC $CFLAGS $DEFINES $TMP") && exit 1;
+       &system("cd $TMPDIR; $CC $CFLAGS $DEFINES $TMP") && exit 1;
        unlink $TMP;
        $TMP =~ s/\.c$/.s/;
        @ARGV = ($TMP);
@@ -1237,7 +1274,8 @@ sub fetch_template {
 }
 
 sub compute_intrinsics {
-    local($TMP) = "/tmp/c2ph-i.$$.c";
+    $TMPDIR ||= tempdir(CLEANUP => 1);
+    local($TMP) = "$TMPDIR/c2ph-i.$$.c";
     open (TMP, ">$TMP") || die "can't open $TMP: $!";
     select(TMP);
 
@@ -1265,7 +1303,7 @@ EOF
     close TMP;
 
     select(STDOUT);
-    open(PIPE, "cd /tmp && $CC $TMP && /tmp/a.out|");
+    open(PIPE, "cd $TMPDIR && $CC $TMP && $TMPDIR/a.out|");
     while (<PIPE>) {
        chop;
        split(' ',$_,2);;
@@ -1274,7 +1312,7 @@ EOF
        $intrinsics{$_[1]} = $template{$_[0]};
     }
     close(PIPE) || die "couldn't read intrinsics!";
-    unlink($TMP, '/tmp/a.out');
+    unlink($TMP, '$TMPDIR/a.out');
     print STDERR "done\n" if $trace;
 }
 
@@ -1398,3 +1436,4 @@ if (defined $Config{d_link}) {
   File::Copy::syscopy('c2ph', 'pstruct');
 }
 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';
+chdir $origdir;