3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-09 21:49:34 +01:00
pbot/modules/compiler_vm/languages/server/_default.pm

200 lines
4.5 KiB
Perl
Raw Normal View History

#!/usr/bin/perl
2019-06-13 06:35:04 +02:00
package _default;
use warnings;
use strict;
2019-06-13 06:35:04 +02:00
use feature "switch";
no if $] >= 5.018, warnings => "experimental::smartmatch";
2019-06-13 06:35:04 +02:00
use IPC::Run qw/run timeout/;
use Data::Dumper;
sub new {
my ($class, %conf) = @_;
my $self = bless {}, $class;
2019-06-13 06:35:04 +02:00
$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};
$self->{arguments} = $conf{arguments};
$self->{factoid} = $conf{factoid};
$self->{'persist-key'} = $conf{'persist-key'};
$self->initialize(%conf);
return $self;
}
sub initialize {
my ($self, %conf) = @_;
}
sub preprocess {
my $self = shift;
2019-06-13 06:35:04 +02:00
open(my $fh, '>', $self->{sourcefile}) or die $!;
print $fh $self->{code} . "\n";
close $fh;
2019-06-13 06:35:04 +02:00
$self->execute(10, undef, 'date', '-s', "\@$self->{date}");
2019-06-13 06:35:04 +02:00
print "Executing [$self->{cmdline}] with args [$self->{arguments}]\n";
my @cmdline = $self->split_line($self->{cmdline}, strip_quotes => 1, preserve_escapes => 0);
push @cmdline, $self->split_line($self->{arguments}, strip_quotes => 1, preserve_escapes => 0);
2019-06-13 06:35:04 +02:00
my ($retval, $stdout, $stderr) = $self->execute(60, $self->{input}, @cmdline);
$self->{output} = $stderr;
$self->{output} .= ' ' if length $self->{output};
$self->{output} .= $stdout;
$self->{error} = $retval;
}
sub postprocess {
my $self = shift;
}
sub execute {
2019-06-13 06:35:04 +02:00
my ($self, $timeout, $stdin, @cmdline) = @_;
2019-06-13 06:35:04 +02:00
$stdin //= '';
print "execute($timeout) [$stdin] ", Dumper \@cmdline, "\n";
2019-06-13 06:35:04 +02:00
my ($exitval, $stdout, $stderr) = eval {
my ($stdout, $stderr);
run \@cmdline, \$stdin, \$stdout, \$stderr, timeout($timeout);
my $exitval = $? >> 8;
return ($exitval, $stdout, $stderr);
};
2019-06-13 06:35:04 +02:00
if ($@) {
my $error = $@;
$error = "[Timed-out]" if $error =~ m/timeout on timer/;
($exitval, $stdout, $stderr) = (-1, '', $error);
}
print "exitval $exitval stdout [$stdout]\nstderr [$stderr]\n";
return ($exitval, $stdout, $stderr);
}
2019-06-26 08:21:15 +02:00
# splits line into quoted arguments while preserving quotes.
# a string is considered quoted only if they are surrounded by
# whitespace or json separators.
# handles unbalanced quotes gracefully by treating them as
# part of the argument they were found within.
2019-06-13 06:35:04 +02:00
sub split_line {
my ($self, $line, %opts) = @_;
my %default_opts = (
strip_quotes => 0,
keep_spaces => 0,
preserve_escapes => 1,
2019-06-13 06:35:04 +02:00
);
%opts = (%default_opts, %opts);
my @chars = split //, $line;
my @args;
my $escaped = 0;
my $quote;
my $token = '';
my $last_token = '';
2019-06-13 06:35:04 +02:00
my $ch = ' ';
my $last_ch;
2019-06-26 08:21:15 +02:00
my $next_ch;
2019-06-13 06:35:04 +02:00
my $i = 0;
my $pos;
my $ignore_quote = 0;
my $spaces = 0;
while (1) {
$last_ch = $ch;
if ($i >= @chars) {
if (defined $quote) {
# reached end, but unbalanced quote... reset to beginning of quote and ignore it
$i = $pos;
$ignore_quote = 1;
$quote = undef;
$last_ch = ' ';
$token = $last_token;
2019-06-13 06:35:04 +02:00
} else {
# add final token and exit
push @args, $token if length $token;
last;
}
}
$ch = $chars[$i++];
2019-06-26 08:21:15 +02:00
$next_ch = $chars[$i];
2019-06-13 06:35:04 +02:00
$spaces = 0 if $ch ne ' ';
2019-06-13 06:35:04 +02:00
if ($escaped) {
if ($opts{preserve_escapes}) {
$token .= "\\$ch";
} else {
$token .= $ch;
}
2019-06-13 06:35:04 +02:00
$escaped = 0;
next;
}
2019-06-13 06:35:04 +02:00
if ($ch eq '\\') {
$escaped = 1;
next;
}
if (defined $quote) {
2019-06-26 08:21:15 +02:00
if ($ch eq $quote and (not defined $next_ch or $next_ch =~ /[\s,:;})\].+=]/)) {
2019-06-13 06:35:04 +02:00
# closing quote
$token .= $ch unless $opts{strip_quotes};
push @args, $token;
$quote = undef;
$token = '';
} else {
# still within quoted argument
$token .= $ch;
}
next;
}
2019-06-26 08:21:15 +02:00
if (($last_ch =~ /[\s:{(\[.+=]/) and not defined $quote and ($ch eq "'" or $ch eq '"')) {
2019-06-13 06:35:04 +02:00
if ($ignore_quote) {
# treat unbalanced quote as part of this argument
$token .= $ch;
$ignore_quote = 0;
} else {
# begin potential quoted argument
$pos = $i - 1;
$quote = $ch;
$last_token = $token;
2019-06-13 06:35:04 +02:00
$token .= $ch unless $opts{strip_quotes};
}
next;
}
if ($ch eq ' ') {
if (++$spaces > 1 and $opts{keep_spaces}) {
$token .= $ch;
next;
} else {
push @args, $token if length $token;
$token = '';
next;
}
}
2019-06-13 06:35:04 +02:00
$token .= $ch;
}
2019-06-13 06:35:04 +02:00
return @args;
}
1;