Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use temporary files to store output of the command in perl tesh
[simgrid.git] / buildtools / Cmake / Scripts / tesh.pl
index d74a6d5..fae12cd 100755 (executable)
@@ -38,6 +38,7 @@ if($^O eq "linux"){
 }
 else{
     $OS = "WIN";
+    $ENV{"PRINTF_EXPONENT_DIGITS"} = "2"; 
 }
 
 
@@ -243,8 +244,12 @@ sub exec_cmd {
 
   ###
   # exec the command line
-  ###
-  $pid = open3(\*CHILD_IN, \*OUT, \*OUT, $cmd{'cmd'} );
+  ###  $line =~ s/\r//g;
+
+  my $e = IO::File->new_tmpfile;
+  $e->autoflush(1);
+  local *E = $e; 
+  $pid = open3(\*CHILD_IN,  ">&E",  ">&E", $cmd{'cmd'} );
 
   # push all provided input to executing child
   map { print CHILD_IN "$_\n" }  @{$cmd{'in'}};
@@ -258,15 +263,29 @@ sub exec_cmd {
     die "fork() failed: $!" unless defined $forked;
     if ( $forked == 0 ) { # child
       sleep $time_to_wait;
-      kill(9, $pid);
+      kill(SIGKILL, $pid);
       exit $time_to_wait;
     }
   }
 
+  
+  # Cleanup the executing child, and kill the timeouter brother on need
+  $cmd{'return'} = 0 unless defined($cmd{'return'});
+  my $wantret;
+  if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
+    $wantret = "got signal $cmd{'expect'}";
+  }else{
+    $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
+    $exitcode= 41;
+  }
+  my $gotret;
+  waitpid ($pid, 0);
+  $gotret = exit_status($?);
 
+  seek($e,0,0);
   # pop all output from executing child
   my @got;
-  while(defined(my $got=<OUT>)) {
+  while(defined(my $got=<$e>)) {
     $got =~ s/\r//g;
     $got =~ s/^( )*//g;
     chomp $got;
@@ -277,8 +296,7 @@ sub exec_cmd {
      }
   }
   }    
-  close OUT;
-   
+
   if ($cmd{'sort'}){   
     sub mysort{
     $a cmp $b
@@ -290,19 +308,7 @@ sub exec_cmd {
       @{$cmd{'out'}}=sort mysort @{$cmd{'out'}};
     }
   }
-  
-  # Cleanup the executing child, and kill the timeouter brother on need
-  $cmd{'return'} = 0 unless defined($cmd{'return'});
-  my $wantret;
-  if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
-    $wantret = "got signal $cmd{'expect'}";
-  }else{
-    $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
-    $exitcode= 41;
-  }
-  my $gotret;
-  waitpid ($pid, 0);
-  $gotret = exit_status($?);
+
   #Did we timeout ? If yes, handle it. If not, kill the forked process.
 
   if($timeout==-1 and $gotret eq "got signal SIGKILL"){
@@ -392,6 +398,7 @@ LINE: while (not $finished and not $error) {
 
   $line_num++;
   chomp $line;
+  $line =~ s/\r//g;
   print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
   my $next;
   # deal with line continuations
@@ -486,7 +493,7 @@ LINE: while (not $finished and not $error) {
     }
     $cmd{'output ignore'} = 1;
   }
-  elsif($line =~ /^!\s*expect signal (\w*)$/) {#expect signal SIGABRT
+  elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
     if (defined($cmd{'cmd'})) {
       exec_cmd(\%cmd);
       %cmd = ();
@@ -532,7 +539,7 @@ LINE: while (not $finished and not $error) {
     die "[TESH/CRITICAL] parse error: $line\n";
   }
   if($forked){
-   kill(9, $forked);
+   kill(SIGKILL, $forked);
    $timeout=0;
   }
 
@@ -548,7 +555,7 @@ if (defined($cmd{'cmd'})) {
 
 
 if($forked){
-   kill(9, $forked);
+   kill(SIGKILL, $forked);
    $timeout=0;
 }