Rash Again

Exercise 5: Configurable Timeouts

by Arne Sommer

Rash - Exercise 6: Cinfigurable Timeouts

[71.2] Published 3. May 2020.

You have read the Exercise text first?

File: rash-timeout-conf (partial)
my %aliases;
my %timeout;

read-conf;

The configuration file:

sub read-conf
{
  return unless CONF_FILE.IO.r;
  
  for CONF_FILE.IO.lines
  {
    when /^alias\s+(.*)/               { do-alias $0.Str    }
    when /^timeout6?\s+ (\d+)\s+ (.*)/ { set-timeout($0.Int,$1.Str) }
  }
}

sub set-timeout(Int $limit, $commands)
{
  %timeout{$_} = $limit for $commands.words;
}

The «default» block:

File: rash-timeout-conf (partial)
    default
    {
      my ($cmd, @args) = $line.words;

      if %programs{$cmd}
      {
        my $timeout        = False;
	my $custom-timeout = False;
	
        if @args.elems
       	{
          if @args[*-1] ~~ /^\:\d+$/
          {
            my $last = @args.pop;
            $last ~~ /^\:(\d+)$/;
            if $0
	    {
	      $timeout = $0.Int;
	      $custom-timeout = True;
	    }	      
          }
        }

        unless $custom-timeout
	{
          $timeout = %timeout{$cmd} // 0;
        }
	
        $timeout
          ?? do-run-timeout %programs{$cmd}, @args, :$timeout
          !! do-run %programs{$cmd}, @args;

The full program is available as «rash-timeout-conf».