2011-01-26 02:59:19 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2011-02-01 01:41:51 +01:00
|
|
|
my $USE_LOCAL = defined $ENV{'CC_LOCAL'};
|
|
|
|
|
2011-01-26 02:59:19 +01:00
|
|
|
my %languages = (
|
|
|
|
'C' => {
|
2012-10-07 19:48:18 +02:00
|
|
|
'cmdline' => 'gcc $file $args -o prog -ggdb -g3',
|
2012-03-12 04:33:54 +01:00
|
|
|
'args' => '-Wextra -Wall -Wno-unused -std=gnu89 -lm -Wfatal-errors',
|
2011-01-26 02:59:19 +01:00
|
|
|
'file' => 'prog.c',
|
|
|
|
},
|
|
|
|
'C++' => {
|
2012-10-07 19:48:18 +02:00
|
|
|
'cmdline' => 'g++ $file $args -o prog -ggdb',
|
2012-01-30 00:50:33 +01:00
|
|
|
'args' => '-lm',
|
2011-01-26 02:59:19 +01:00
|
|
|
'file' => 'prog.cpp',
|
|
|
|
},
|
|
|
|
'C99' => {
|
2012-10-07 19:48:18 +02:00
|
|
|
'cmdline' => 'gcc $file $args -o prog -ggdb -g3',
|
2012-07-22 21:22:30 +02:00
|
|
|
'args' => '-Wextra -Wall -Wno-unused -pedantic -Wfloat-equal -std=c99 -lm -Wfatal-errors',
|
2011-01-26 02:59:19 +01:00
|
|
|
'file' => 'prog.c',
|
|
|
|
},
|
2012-01-30 00:50:33 +01:00
|
|
|
'C11' => {
|
2012-10-07 19:48:18 +02:00
|
|
|
'cmdline' => 'gcc $file $args -o prog -ggdb -g3',
|
2012-07-22 21:22:30 +02:00
|
|
|
'args' => '-Wextra -Wall -Wno-unused -pedantic -Wfloat-equal -std=c11 -lm -Wfatal-errors',
|
2012-01-30 00:50:33 +01:00
|
|
|
'file' => 'prog.c',
|
|
|
|
},
|
2011-01-26 02:59:19 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
sub runserver {
|
2011-02-01 01:41:51 +01:00
|
|
|
my ($input, $output);
|
|
|
|
|
|
|
|
if(not defined $USE_LOCAL or $USE_LOCAL == 0) {
|
|
|
|
open($input, '<', "/dev/ttyS0") or die $!;
|
|
|
|
open($output, '>', "/dev/ttyS0") or die $!;
|
|
|
|
} else {
|
|
|
|
open($input, '<', "/dev/stdin") or die $!;
|
|
|
|
open($output, '>', "/dev/stdout") or die $!;
|
|
|
|
}
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2012-10-24 14:26:18 +02:00
|
|
|
my $date;
|
2011-01-26 02:59:19 +01:00
|
|
|
my $lang;
|
|
|
|
my $code;
|
|
|
|
my $user_args;
|
|
|
|
my $user_input;
|
|
|
|
|
|
|
|
print "Waiting for input...\n";
|
|
|
|
|
|
|
|
while(my $line = <$input>) {
|
|
|
|
chomp $line;
|
|
|
|
next unless length $line;
|
|
|
|
|
|
|
|
print "Got [$line]\n";
|
|
|
|
|
|
|
|
if($line =~ m/^compile:\s*end/) {
|
|
|
|
next if not defined $lang or not defined $code;
|
|
|
|
|
|
|
|
print "Attempting compile [$lang] ...\n";
|
|
|
|
|
2012-10-24 14:26:18 +02:00
|
|
|
my $result = interpret($lang, $code, $user_args, $user_input, $date);
|
2011-01-26 02:59:19 +01:00
|
|
|
|
|
|
|
print "Done compiling; result: [$result]\n";
|
|
|
|
print $output "result:$result\n";
|
|
|
|
print $output "result:end\n";
|
|
|
|
|
2012-07-22 21:22:30 +02:00
|
|
|
# system("rm *");
|
2012-01-30 00:50:33 +01:00
|
|
|
|
2011-02-01 01:41:51 +01:00
|
|
|
if(not defined $USE_LOCAL or $USE_LOCAL == 0) {
|
|
|
|
print "input: ";
|
|
|
|
next;
|
|
|
|
} else {
|
|
|
|
exit;
|
|
|
|
}
|
2011-01-26 02:59:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if($line =~ m/^compile:\s*(.*)/) {
|
|
|
|
my $options = $1;
|
|
|
|
$user_args = undef;
|
|
|
|
$user_input = undef;
|
|
|
|
$lang = undef;
|
|
|
|
|
2012-10-24 14:26:18 +02:00
|
|
|
($lang, $user_args, $user_input, $date) = split /:/, $options;
|
2011-01-26 02:59:19 +01:00
|
|
|
|
|
|
|
$code = "";
|
2012-01-30 00:50:33 +01:00
|
|
|
$lang = "C11" if not defined $lang;
|
2011-01-26 02:59:19 +01:00
|
|
|
$user_args = "" if not defined $user_args;
|
|
|
|
$user_input = "" if not defined $user_input;
|
|
|
|
|
2012-10-24 14:26:18 +02:00
|
|
|
print "Setting lang [$lang]; [$user_args]; [$user_input]; [$date]\n";
|
2011-01-26 02:59:19 +01:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
$code .= $line . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
close $input;
|
|
|
|
close $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub interpret {
|
2012-10-24 14:26:18 +02:00
|
|
|
my ($lang, $code, $user_args, $user_input, $date) = @_;
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2012-10-24 14:26:18 +02:00
|
|
|
print "lang: [$lang], code: [$code], user_args: [$user_args], input: [$user_input], date: [$date]\n";
|
2011-01-26 02:59:19 +01:00
|
|
|
|
|
|
|
$lang = uc $lang;
|
|
|
|
|
|
|
|
if(not exists $languages{$lang}) {
|
|
|
|
return "No support for language '$lang' at this time.\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
open(my $fh, '>', $languages{$lang}{'file'}) or die $!;
|
|
|
|
print $fh $code . "\n";
|
|
|
|
close $fh;
|
|
|
|
|
|
|
|
my $cmdline = $languages{$lang}{'cmdline'};
|
|
|
|
|
|
|
|
if(length $user_args) {
|
|
|
|
print "Replacing args with $user_args\n";
|
|
|
|
$user_args = quotemeta($user_args);
|
|
|
|
$user_args =~ s/\\ / /g;
|
|
|
|
$cmdline =~ s/\$args/$user_args/;
|
|
|
|
} else {
|
|
|
|
$cmdline =~ s/\$args/$languages{$lang}{'args'}/;
|
|
|
|
}
|
|
|
|
|
|
|
|
$cmdline =~ s/\$file/$languages{$lang}{'file'}/;
|
|
|
|
|
|
|
|
print "Executing [$cmdline]\n";
|
2012-10-07 19:48:18 +02:00
|
|
|
my ($ret, $result) = execute(60, $cmdline);
|
2011-02-01 01:41:51 +01:00
|
|
|
# print "Got result: ($ret) [$result]\n";
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2011-02-01 01:41:51 +01:00
|
|
|
# if exit code was not 0, then there was a problem compiling, such as an error diagnostic
|
|
|
|
# so return the compiler output
|
2011-01-26 02:59:19 +01:00
|
|
|
if($ret != 0) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $output = "";
|
|
|
|
|
2011-02-01 01:41:51 +01:00
|
|
|
# no errors compiling, but if $result contains something, it must be a warning message
|
|
|
|
# so prepend it to the output
|
2012-07-22 21:22:30 +02:00
|
|
|
if(length $result) {
|
2011-01-26 02:59:19 +01:00
|
|
|
$result =~ s/^\s+//;
|
|
|
|
$result =~ s/\s+$//;
|
2012-07-22 21:22:30 +02:00
|
|
|
$output = "[$result]\n";
|
2011-01-26 02:59:19 +01:00
|
|
|
}
|
|
|
|
|
2011-12-31 00:20:29 +01:00
|
|
|
my $user_input_quoted = quotemeta $user_input;
|
2012-10-24 14:26:18 +02:00
|
|
|
($ret, $result) = execute(60, "bash -c 'date -s \@$date; ulimit -t 1; compiler_watchdog.pl $user_input_quoted > .output'");
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2012-10-07 19:48:18 +02:00
|
|
|
$result = "";
|
|
|
|
|
|
|
|
open(FILE, '.output');
|
|
|
|
while(<FILE>) {
|
|
|
|
$result .= $_;
|
|
|
|
last if length $result >= 2048 * 20;
|
|
|
|
}
|
|
|
|
close(FILE);
|
|
|
|
|
2011-01-26 02:59:19 +01:00
|
|
|
$result =~ s/\s+$//;
|
|
|
|
|
2011-02-01 01:41:51 +01:00
|
|
|
# print "Executed prog; got result: ($ret) [$result]\n";
|
|
|
|
|
2011-01-26 02:59:19 +01:00
|
|
|
if(not length $result) {
|
|
|
|
$result = "Success (no output).\n" if $ret == 0;
|
|
|
|
$result = "Success (exit code $ret).\n" if $ret != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output . "\n" . $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub execute {
|
|
|
|
my $timeout = shift @_;
|
|
|
|
my ($cmdline) = @_;
|
|
|
|
|
|
|
|
my ($ret, $result);
|
|
|
|
|
|
|
|
($ret, $result) = eval {
|
|
|
|
print "eval\n";
|
|
|
|
|
|
|
|
my $result = '';
|
|
|
|
|
2012-07-22 21:22:30 +02:00
|
|
|
my $pid = open(my $fh, '-|', "$cmdline 2>&1");
|
2011-01-26 02:59:19 +01:00
|
|
|
|
|
|
|
local $SIG{ALRM} = sub { print "Time out\n"; kill 'TERM', $pid; die "$result [Timed-out]\n"; };
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
runserver;
|