Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[tesh] Fix arguments in tesh file
[simgrid.git] / tools / tesh / tesh.pl
index 5cb808f..3c7c380 100755 (executable)
@@ -43,7 +43,6 @@ push @INC, $path;
 
 use Getopt::Long qw(GetOptions);
 use strict;
-use Term::ANSIColor;
 use Text::ParseWords;
 use IPC::Open3;
 use IO::File;
@@ -68,24 +67,11 @@ BEGIN {
 }
 
 ##
-## Command line option handling
+## Helper functions
 ##
 
-if ( $ARGV[0] eq "--internal-killer-process" ) {
-
-    # We fork+exec a waiter process in charge of killing the command after timeout
-    # If the command stops earlier, that's fine: the killer sends a signal to an already stopped process, fails, and quits.
-    #    Nobody cares about the killer issues.
-    #    The only problem could arise if another process is given the same PID than cmd. We bet it won't happen :)
-    my $time_to_wait = $ARGV[1];
-    my $pid          = $ARGV[2];
-    sleep $time_to_wait;
-    kill( 'TERM', $pid );
-    sleep 1;
-    kill( 'KILL', $pid );
-    exit $time_to_wait;
-}
-
+# Helper function replacing any occurence of variable '$name' by its '$value'
+# As in Bash, ${$value:=BLABLA} is rewritten to $value if set or to BLABLA if $value is not set
 sub var_subst {
     my ( $text, $name, $value ) = @_;
     if ($value) {
@@ -99,9 +85,9 @@ sub var_subst {
     return $text;
 }
 
-# option handling helper subs
+# Command CD. Just change to the provided directory
 sub cd_cmd {
-    my $directory = $_[1];
+    my $directory = shift;
     my $failure   = 1;
     if ( -e $directory && -d $directory ) {
         chdir("$directory");
@@ -118,27 +104,38 @@ sub cd_cmd {
     }
 }
 
+# Command setenv. Gets "variable=content", and update the environment accordingly
 sub setenv_cmd {
-    my ( $var, $ctn );
-    if ( $_[0] =~ /^(.*)=(.*)$/ ) {
-        ( $var, $ctn ) = ( $1, $2 );
-    } elsif ( $_[1] =~ /^(.*)=(.*)$/ ) {
-        ( $var, $ctn ) = ( $1, $2 );
+    my $arg = shift;
+    if ( $arg =~ /^(.*)=(.*)$/ ) {
+        my ( $var, $ctn ) = ( $1, $2 );
+       print "[Tesh/INFO] setenv $var=$ctn\n";
+       $environ{$var} = $ctn;
     } else {
-        die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
+        die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$arg'\n";
     }
-
-    print "[Tesh/INFO] setenv $var=$ctn\n";
-    $environ{$var} = $ctn;
 }
 
-# Main option parsing sub
+##
+## Command line option handling
+##
 
-sub get_options {
+if ( $ARGV[0] eq "--internal-killer-process" ) {
 
-    # remove the tesh file from the ARGV used
-    my @ARGV = @_;
-    $tesh_file = pop @ARGV;
+    # We fork+exec a waiter process in charge of killing the command after timeout
+    # If the command stops earlier, that's fine: the killer sends a signal to an already stopped process, fails, and quits.
+    #    Nobody cares about the killer issues.
+    #    The only problem could arise if another process is given the same PID than cmd. We bet it won't happen :)
+    my $time_to_wait = $ARGV[1];
+    my $pid          = $ARGV[2];
+    sleep $time_to_wait;
+    kill( 'TERM', $pid );
+    sleep 1;
+    kill( 'KILL', $pid );
+    exit $time_to_wait;
+}
+
+sub get_options {
 
     # temporary arrays for GetOption
     my @cfg;
@@ -158,14 +155,16 @@ sub get_options {
 
         'difftool=s' => \$diff_tool,
 
-        'cd=s'             => \&cd_cmd,
+        'cd=s'             => sub { cd_cmd($_[1]) },
         'timeout=s'        => \$opt{'timeout'},
-        'setenv=s'         => \&setenv_cmd,
+        'setenv=s'         => sub { setenv_cmd($_[1]) },
         'cfg=s'            => \@cfg,
         'log=s'            => \$log,
         'enable-coverage+' => \$enable_coverage,
     );
 
+    $tesh_file = pop @ARGV;
+
     if ($enable_coverage) {
         print "Enable coverage\n";
     }
@@ -191,7 +190,7 @@ sub get_options {
     return %opt;
 }
 
-my %opts = get_options(@ARGV);
+my %opts = get_options();
 
 ##
 ## File parsing
@@ -265,7 +264,6 @@ sub exec_cmd {
 
     ###
     # exec the command line
-    ###  $line =~ s/\r//g;
 
     $cmd{'got'} = IO::File->new_tmpfile;
     $cmd{'got'}->autoflush(1);
@@ -296,7 +294,7 @@ sub exec_cmd {
     if ( $cmd{'background'} != 1 ) {
         waitpid( $cmd{'pid'}, 0 );
         $cmd{'gotret'} = exit_status($?);
-        parse_out( \%cmd );
+        parse_result( \%cmd );
     } else {
 
         # & commands, which will be handled at the end
@@ -304,7 +302,7 @@ sub exec_cmd {
     }
 }
 
-sub parse_out {
+sub parse_result {
     my %cmd    = %{ $_[0] };
     my $gotret = $cmd{'gotret'};
 
@@ -520,7 +518,7 @@ LINE: while ( defined( my $line = <$infh> ) and not $error ) {
               if scalar @{ cmd { 'out' } };
 
             $arg =~ s/^ *cd //;
-            cd_cmd( "", $arg );
+            cd_cmd( $arg );
             %cmd = ();
 
         } else {    # regular command
@@ -528,7 +526,7 @@ LINE: while ( defined( my $line = <$infh> ) and not $error ) {
             $cmd{'file'} = $tesh_file;
             $cmd{'line'} = $line_num;
         }
-    } elsif ( $cmd eq '&' ) {    # parallel command line
+    } elsif ( $cmd eq '&' ) {    # background command line
 
         if ( defined( $cmd{'cmd'} ) ) {
             exec_cmd( \%cmd );
@@ -538,6 +536,7 @@ LINE: while ( defined( my $line = <$infh> ) and not $error ) {
         $cmd{'cmd'}        = $arg;
         $cmd{'file'}       = $tesh_file;
         $cmd{'line'}       = $line_num;
+       
     } elsif ( $line =~ /^!\s*output sort/ ) {    #output sort
         if ( defined( $cmd{'cmd'} ) ) {
             exec_cmd( \%cmd );
@@ -582,14 +581,6 @@ LINE: while ( defined( my $line = <$infh> ) and not $error ) {
         $line =~ s/^! setenv //g;
         $line =~ s/\r//g;
         setenv_cmd($line);
-    } elsif ( $line =~ /^!\s*include/ ) {                #include
-        if ( defined( $cmd{'cmd'} ) ) {
-            exec_cmd( \%cmd );
-            %cmd = ();
-        }
-        print color("red"),   "[Tesh/CRITICAL] need include";
-        print color("reset"), "\n";
-        die;
     } elsif ( $line =~ /^!\s*timeout/ ) {                #timeout
         if ( defined( $cmd{'cmd'} ) ) {
             exec_cmd( \%cmd );
@@ -625,7 +616,7 @@ foreach (@bg_cmds) {
     my %test = %{$_};
     waitpid( $test{'pid'}, 0 );
     $test{'gotret'} = exit_status($?);
-    parse_out( \%test );
+    parse_result( \%test );
 }
 
 @bg_cmds = ();