From e43d2ef144c352e9ee0ae47db7e6c63f5f8a3e10 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Fri, 15 Sep 2017 16:41:36 -0700 Subject: [PATCH] Improvements to code-factoids Most code languages now accept factoida arguments as command-line arguments; e.g., argc and argv in C/C++, args[] in Java, @ARGV in Perl, etc. No longer say "Same output" for code-factoids. --- PBot/Factoids.pm | 2 +- modules/compiler_vm/compiler_watchdog.pl | 2 +- modules/compiler_vm/languages/_default.pm | 6 +++++- modules/compiler_vm/languages/bash.pm | 4 ++++ modules/compiler_vm/languages/clisp.pm | 4 ++++ modules/compiler_vm/languages/go.pm | 6 +++++- modules/compiler_vm/languages/haskell.pm | 4 ++++ modules/compiler_vm/languages/javascript.pm | 4 ++++ modules/compiler_vm/languages/ksh.pm | 4 ++++ modules/compiler_vm/languages/lua.pm | 4 ++++ modules/compiler_vm/languages/perl.pm | 6 ++++-- modules/compiler_vm/languages/python.pm | 4 ++++ modules/compiler_vm/languages/python3.pm | 4 ++++ modules/compiler_vm/languages/server/_c_base.pm | 4 +--- modules/compiler_vm/languages/server/_default.pm | 3 +++ modules/compiler_vm/languages/server/java.pm | 2 +- modules/compiler_vm/languages/sh.pm | 4 ++++ 17 files changed, 57 insertions(+), 10 deletions(-) diff --git a/PBot/Factoids.pm b/PBot/Factoids.pm index 60d8b9af..2a290901 100644 --- a/PBot/Factoids.pm +++ b/PBot/Factoids.pm @@ -662,7 +662,7 @@ sub execute_code_factoid_using_vm { $code = $self->expand_action_arguments($code, $arguments, $nick); } - my %h = (nick => $nick, channel => $from, lang => $lang, code => $code, arguments => $arguments); + my %h = (nick => $nick, channel => $from, lang => $lang, code => $code, arguments => $arguments, factoid => "$chan:$keyword"); my $json = encode_json \%h; $self->{pbot}->{factoids}->{factoidmodulelauncher}->execute_module($from, $tonick, $nick, $user, $host, 'code-factoid', $chan, $root_keyword, "compiler", $json, 0); return ""; diff --git a/modules/compiler_vm/compiler_watchdog.pl b/modules/compiler_vm/compiler_watchdog.pl index 4b95d04c..61f4602d 100755 --- a/modules/compiler_vm/compiler_watchdog.pl +++ b/modules/compiler_vm/compiler_watchdog.pl @@ -136,7 +136,7 @@ sub execute { gdb $in, "set height 0\n"; gdb $in, "set auto-solib-add off\n"; gdb $in, "catch exec\n"; - gdb $in, "run < .input\n"; + gdb $in, "run @ARGV < .input\n"; next; } diff --git a/modules/compiler_vm/languages/_default.pm b/modules/compiler_vm/languages/_default.pm index fa5cca44..d191f980 100755 --- a/modules/compiler_vm/languages/_default.pm +++ b/modules/compiler_vm/languages/_default.pm @@ -32,6 +32,7 @@ sub new { $self->{code} = $conf{code}; $self->{max_history} = $conf{max_history} // 10000; $self->{arguments} = $conf{arguments}; + $self->{factoid} = $conf{factoid}; $self->{default_options} = ''; $self->{cmdline} = 'echo Hello, world!'; @@ -42,6 +43,9 @@ sub new { $self->{lang} =~ s/^\s+|\s+$//g if defined $self->{lang}; $self->{code} =~ s/^\s+|\s+$//g if defined $self->{code}; + $self->{arguments} = quotemeta $self->{arguments}; + $self->{arguments} =~ s/\\ / /g; + $self->initialize(%conf); return $self; @@ -239,7 +243,7 @@ sub show_output { } close FILE; - if(defined $last_output and $last_output eq $output) { + if((not $self->{factoid}) and defined $last_output and $last_output eq $output) { print "Same output.\n"; exit 0; } diff --git a/modules/compiler_vm/languages/bash.pm b/modules/compiler_vm/languages/bash.pm index bddf6edc..4af5a77f 100755 --- a/modules/compiler_vm/languages/bash.pm +++ b/modules/compiler_vm/languages/bash.pm @@ -18,6 +18,10 @@ sub initialize { $self->{default_options} = ''; $self->{cmdline} = 'bash $options $sourcefile'; + if (length $self->{arguments}) { + $self->{cmdline} .= " $self->{arguments}"; + } + $self->{cmdline_opening_comment} = ": <<'____CMDLINE____'\n"; $self->{cmdline_closing_comment} = "____CMDLINE____\n"; diff --git a/modules/compiler_vm/languages/clisp.pm b/modules/compiler_vm/languages/clisp.pm index 38e16e92..f5ef185f 100755 --- a/modules/compiler_vm/languages/clisp.pm +++ b/modules/compiler_vm/languages/clisp.pm @@ -18,6 +18,10 @@ sub initialize { $self->{default_options} = ''; $self->{cmdline} = 'clisp $options $sourcefile'; + if (length $self->{arguments}) { + $self->{cmdline} .= " $self->{arguments}"; + } + $self->{sprunge_lexer} = 'cl'; $self->{cmdline_opening_comment} = "#|=============== CMDLINE ===============\n"; diff --git a/modules/compiler_vm/languages/go.pm b/modules/compiler_vm/languages/go.pm index 5be2074e..13ae587d 100755 --- a/modules/compiler_vm/languages/go.pm +++ b/modules/compiler_vm/languages/go.pm @@ -16,7 +16,11 @@ sub initialize { $self->{sourcefile} = 'prog.go'; $self->{execfile} = 'prog'; $self->{default_options} = ''; - $self->{cmdline} = 'golang-go $options run $sourcefile'; + $self->{cmdline} = 'go $options run $sourcefile'; + + if (length $self->{arguments}) { + $self->{cmdline} .= " $self->{arguments}"; + } } 1; diff --git a/modules/compiler_vm/languages/haskell.pm b/modules/compiler_vm/languages/haskell.pm index e78b4415..0ebc10be 100755 --- a/modules/compiler_vm/languages/haskell.pm +++ b/modules/compiler_vm/languages/haskell.pm @@ -18,6 +18,10 @@ sub initialize { $self->{default_options} = ''; $self->{cmdline} = 'ghc -v0 $options -o $execfile $sourcefile'; + if (length $self->{arguments}) { + $self->{cmdline} .= " $self->{arguments}"; + } + $self->{cmdline_opening_comment} = "{-|\n=============== CMDLINE ===============\n"; $self->{cmdline_closing_comment} = "=============== CMDLINE ===============\n-}\n"; diff --git a/modules/compiler_vm/languages/javascript.pm b/modules/compiler_vm/languages/javascript.pm index 2f855657..0a3a7351 100755 --- a/modules/compiler_vm/languages/javascript.pm +++ b/modules/compiler_vm/languages/javascript.pm @@ -18,6 +18,10 @@ sub initialize { $self->{default_options} = ''; $self->{cmdline} = 'd8 $options $sourcefile'; + if (length $self->{arguments}) { + $self->{cmdline} .= " $self->{arguments}"; + } + $self->{cmdline_opening_comment} = "/************* CMDLINE *************\n"; $self->{cmdline_closing_comment} = "************** CMDLINE *************/\n"; diff --git a/modules/compiler_vm/languages/ksh.pm b/modules/compiler_vm/languages/ksh.pm index 0416df74..3371212e 100755 --- a/modules/compiler_vm/languages/ksh.pm +++ b/modules/compiler_vm/languages/ksh.pm @@ -18,6 +18,10 @@ sub initialize { $self->{default_options} = ''; $self->{cmdline} = 'ksh $options $sourcefile'; + if (length $self->{arguments}) { + $self->{cmdline} .= " $self->{arguments}"; + } + $self->{cmdline_opening_comment} = ": <<'____CMDLINE____'\n"; $self->{cmdline_closing_comment} = "____CMDLINE____\n"; diff --git a/modules/compiler_vm/languages/lua.pm b/modules/compiler_vm/languages/lua.pm index 99992449..4398ab27 100755 --- a/modules/compiler_vm/languages/lua.pm +++ b/modules/compiler_vm/languages/lua.pm @@ -18,6 +18,10 @@ sub initialize { $self->{default_options} = ''; $self->{cmdline} = 'lua $options $sourcefile'; + if (length $self->{arguments}) { + $self->{cmdline} .= " $self->{arguments}"; + } + $self->{cmdline_opening_comment} = "--[[--------------- CMDLINE ---------------\n"; $self->{cmdline_closing_comment} = "------------------- CMDLINE ---------------]]\n"; diff --git a/modules/compiler_vm/languages/perl.pm b/modules/compiler_vm/languages/perl.pm index f846b0a0..19863f94 100755 --- a/modules/compiler_vm/languages/perl.pm +++ b/modules/compiler_vm/languages/perl.pm @@ -19,6 +19,10 @@ sub initialize { $self->{execfile} = 'prog.pl'; $self->{default_options} = '-w'; $self->{cmdline} = 'perl $options $sourcefile'; + + if (length $self->{arguments}) { + $self->{cmdline} .= " $self->{arguments}"; + } } sub preprocess_code { @@ -26,8 +30,6 @@ sub preprocess_code { $self->SUPER::preprocess_code; if (defined $self->{arguments}) { - my $qargs = quotemeta $self->{arguments}; - $qargs =~ s/\\ / /g; my @args = shellwords($self->{arguments}); my $prelude .= "\nmy \$arglen = " . (scalar @args) . ";\n"; diff --git a/modules/compiler_vm/languages/python.pm b/modules/compiler_vm/languages/python.pm index 9d4bef93..506a2a93 100755 --- a/modules/compiler_vm/languages/python.pm +++ b/modules/compiler_vm/languages/python.pm @@ -18,6 +18,10 @@ sub initialize { $self->{default_options} = ''; $self->{cmdline} = 'python $options $sourcefile'; + if (length $self->{arguments}) { + $self->{cmdline} .= " $self->{arguments}"; + } + $self->{cmdline_opening_comment} = "'''\n=============== CMDLINE ===============\n"; $self->{cmdline_closing_comment} = "=============== CMDLINE ===============\n'''\n"; diff --git a/modules/compiler_vm/languages/python3.pm b/modules/compiler_vm/languages/python3.pm index cdcd1c21..1b568af8 100755 --- a/modules/compiler_vm/languages/python3.pm +++ b/modules/compiler_vm/languages/python3.pm @@ -18,6 +18,10 @@ sub initialize { $self->{default_options} = ''; $self->{cmdline} = 'python3 $options $sourcefile'; + if (length $self->{arguments}) { + $self->{cmdline} .= " $self->{arguments}"; + } + $self->{cmdline_opening_comment} = "'''\n=============== CMDLINE ===============\n"; $self->{cmdline_closing_comment} = "=============== CMDLINE ===============\n'''\n"; diff --git a/modules/compiler_vm/languages/server/_c_base.pm b/modules/compiler_vm/languages/server/_c_base.pm index 1b964e70..5a0e529a 100755 --- a/modules/compiler_vm/languages/server/_c_base.pm +++ b/modules/compiler_vm/languages/server/_c_base.pm @@ -15,8 +15,6 @@ sub preprocess { $self->SUPER::preprocess; if ($self->{cmdline} =~ m/--(?:version|analyze)/) { - $self->{output} =~ s/Ubuntu //; - $self->{output} =~ s/-\d+ubuntu\d+//; $self->{done} = 1; } } @@ -33,7 +31,7 @@ sub postprocess { } print "Executing gdb\n"; - my ($retval, $result) = $self->execute(60, "bash -c \"date -s \@$self->{date}; ulimit -t 5; compiler_watchdog.pl > .output\""); + my ($retval, $result) = $self->execute(60, "bash -c \"date -s \@$self->{date}; ulimit -t 5; compiler_watchdog.pl $self->{arguments} > .output\""); $result = ""; open(FILE, '.output'); diff --git a/modules/compiler_vm/languages/server/_default.pm b/modules/compiler_vm/languages/server/_default.pm index bafe1282..33baee8b 100755 --- a/modules/compiler_vm/languages/server/_default.pm +++ b/modules/compiler_vm/languages/server/_default.pm @@ -23,6 +23,8 @@ sub new { $self->{cmdline} = $conf{cmdline}; $self->{input} = $conf{input}; $self->{date} = $conf{date}; + $self->{arguments} = $conf{arguments}; + $self->{factoid} = $conf{factoid}; $self->initialize(%conf); @@ -104,6 +106,7 @@ sub execute { my $pid = open(my $fh, '-|', "$cmdline 2>&1"); local $SIG{ALRM} = sub { print "Time out\n"; kill 'TERM', $pid; die "$result [Timed-out]\n"; }; + local $SIG{CHLD} = 'IGNORE'; alarm($timeout); while(my $line = <$fh>) { diff --git a/modules/compiler_vm/languages/server/java.pm b/modules/compiler_vm/languages/server/java.pm index c3fabbe1..2f8e7d62 100755 --- a/modules/compiler_vm/languages/server/java.pm +++ b/modules/compiler_vm/languages/server/java.pm @@ -32,7 +32,7 @@ sub postprocess { print "Executing java\n"; my $input_quoted = quotemeta $self->{input}; $input_quoted =~ s/\\"/"'\\"'"/g; - my ($retval, $result) = $self->execute(60, "bash -c \"date -s \@$self->{date}; ulimit -t 5; echo $input_quoted | java prog > .output\""); + my ($retval, $result) = $self->execute(60, "bash -c \"date -s \@$self->{date}; ulimit -t 5; echo $input_quoted | java prog $self->{arguments} > .output\""); $result = ""; open(FILE, '.output'); diff --git a/modules/compiler_vm/languages/sh.pm b/modules/compiler_vm/languages/sh.pm index 4f68070e..46138384 100755 --- a/modules/compiler_vm/languages/sh.pm +++ b/modules/compiler_vm/languages/sh.pm @@ -18,6 +18,10 @@ sub initialize { $self->{default_options} = ''; $self->{cmdline} = 'sh $options $sourcefile'; + if (length $self->{arguments}) { + $self->{cmdline} .= " $self->{arguments}"; + } + $self->{cmdline_opening_comment} = ": <<'____CMDLINE____'\n"; $self->{cmdline_closing_comment} = "____CMDLINE____\n";