2015-01-15 06:51:17 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2015-01-15 06:51:17 +01:00
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
use feature "switch";
|
|
|
|
|
|
|
|
no if $] >= 5.018, warnings => "experimental::smartmatch";
|
|
|
|
|
|
|
|
package _default;
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
my ($class, %conf) = @_;
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
|
|
|
|
$self->{debug} = $conf{debug} // 0;
|
|
|
|
$self->{sourcefile} = $conf{sourcefile};
|
|
|
|
$self->{execfile} = $conf{execfile};
|
|
|
|
$self->{code} = $conf{code};
|
|
|
|
$self->{cmdline} = $conf{cmdline};
|
|
|
|
$self->{input} = $conf{input};
|
|
|
|
$self->{date} = $conf{date};
|
2017-09-16 01:41:36 +02:00
|
|
|
$self->{arguments} = $conf{arguments};
|
|
|
|
$self->{factoid} = $conf{factoid};
|
2017-09-19 06:36:40 +02:00
|
|
|
$self->{'persist-key'} = $conf{'persist-key'};
|
2015-01-15 06:51:17 +01:00
|
|
|
|
|
|
|
$self->initialize(%conf);
|
|
|
|
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub preprocess {
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-09-04 06:32:44 +02:00
|
|
|
if ($self->{code} =~ m/print_last_statement\(.*\);$/m) {
|
|
|
|
# remove print_last_statement wrapper in order to get warnings/errors from last statement line
|
|
|
|
my $code = $self->{code};
|
|
|
|
$code =~ s/print_last_statement\((.*)\);$/$1;/mg;
|
|
|
|
open(my $fh, '>', $self->{sourcefile}) or die $!;
|
|
|
|
print $fh $code . "\n";
|
|
|
|
close $fh;
|
|
|
|
|
|
|
|
print "Executing [$self->{cmdline}] without print_last_statement\n";
|
2017-11-27 11:08:25 +01:00
|
|
|
my ($retval, $result) = $self->execute(60, "date -s \@$self->{date} > /dev/null; ulimit -t 5; $self->{cmdline}");
|
2015-09-04 06:32:44 +02:00
|
|
|
$self->{output} = $result;
|
|
|
|
$self->{error} = $retval;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-09-04 06:32:44 +02:00
|
|
|
# now compile with print_last_statement intact, ignoring compile results
|
|
|
|
if (not $self->{error}) {
|
|
|
|
open(my $fh, '>', $self->{sourcefile}) or die $!;
|
|
|
|
print $fh $self->{code} . "\n";
|
|
|
|
close $fh;
|
|
|
|
|
|
|
|
print "Executing [$self->{cmdline}] with print_last_statement\n";
|
2017-11-27 11:08:25 +01:00
|
|
|
my ($retval, $result) = $self->execute(60, "date -s \@$self->{date} > /dev/null; ulimit -t 5; $self->{cmdline}");
|
2015-09-04 06:32:44 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
open(my $fh, '>', $self->{sourcefile}) or die $!;
|
|
|
|
print $fh $self->{code} . "\n";
|
|
|
|
close $fh;
|
|
|
|
|
|
|
|
print "Executing [$self->{cmdline}]\n";
|
2017-11-27 11:08:25 +01:00
|
|
|
my ($retval, $result) = $self->execute(60, "date -s \@$self->{date} > /dev/null; ulimit -t 5; $self->{cmdline}");
|
2015-09-04 06:32:44 +02:00
|
|
|
$self->{output} = $result;
|
|
|
|
$self->{error} = $retval;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub postprocess {
|
2015-07-18 17:12:59 +02:00
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $input = $self->{input};
|
|
|
|
|
2017-04-11 04:24:41 +02:00
|
|
|
print "writing input [$input]\n";
|
|
|
|
|
2015-07-18 17:12:59 +02:00
|
|
|
$input =~ s/(?<!\\)\\n/\n/mg;
|
|
|
|
$input =~ s/(?<!\\)\\r/\r/mg;
|
|
|
|
$input =~ s/(?<!\\)\\t/\t/mg;
|
|
|
|
$input =~ s/(?<!\\)\\b/\b/mg;
|
2017-04-11 04:24:41 +02:00
|
|
|
|
2015-07-18 17:12:59 +02:00
|
|
|
$input =~ s/\\\\/\\/mg;
|
|
|
|
|
|
|
|
open(my $fh, '>', '.input');
|
|
|
|
print $fh "$input\n";
|
|
|
|
close $fh;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub execute {
|
|
|
|
my $self = shift;
|
|
|
|
my $timeout = shift;
|
|
|
|
my ($cmdline) = @_;
|
|
|
|
|
|
|
|
my ($ret, $result);
|
|
|
|
|
|
|
|
($ret, $result) = eval {
|
|
|
|
print "eval\n";
|
|
|
|
|
|
|
|
my $result = '';
|
|
|
|
|
|
|
|
my $pid = open(my $fh, '-|', "$cmdline 2>&1");
|
|
|
|
|
|
|
|
local $SIG{ALRM} = sub { print "Time out\n"; kill 'TERM', $pid; die "$result [Timed-out]\n"; };
|
2017-09-16 01:41:36 +02:00
|
|
|
local $SIG{CHLD} = 'IGNORE';
|
2015-01-15 06:51:17 +01:00
|
|
|
alarm($timeout);
|
|
|
|
|
|
|
|
while(my $line = <$fh>) {
|
|
|
|
$result .= $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
close $fh;
|
|
|
|
my $ret = $? >> 8;
|
|
|
|
alarm 0;
|
|
|
|
return ($ret, $result);
|
|
|
|
};
|
|
|
|
|
|
|
|
print "done eval\n";
|
|
|
|
alarm 0;
|
|
|
|
|
|
|
|
if($@ =~ /Timed-out/) {
|
|
|
|
return (-1, $@);
|
|
|
|
}
|
|
|
|
|
|
|
|
print "[$ret, $result]\n";
|
|
|
|
return ($ret, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|