2015-01-15 06:51:17 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
use 5.020;
|
|
|
|
|
2015-01-15 06:51:17 +01:00
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
use feature qw(switch unicode_strings signatures);
|
|
|
|
no warnings qw(experimental::smartmatch experimental::signatures);
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-01-23 16:49:23 +01:00
|
|
|
package Languages::_default;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2019-07-11 03:40:53 +02:00
|
|
|
use Encode;
|
2022-02-14 20:18:49 +01:00
|
|
|
use JSON::XS;
|
|
|
|
use Getopt::Long qw(GetOptionsFromArray :config pass_through no_ignore_case no_auto_abbrev);
|
|
|
|
use Time::HiRes qw(gettimeofday);
|
2022-02-24 19:39:15 +01:00
|
|
|
use POSIX;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-01-23 16:49:23 +01:00
|
|
|
use FindBin qw($RealBin);
|
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
use InteractiveEdit;
|
|
|
|
use Paste;
|
|
|
|
use SplitLine;
|
|
|
|
|
2015-01-15 06:51:17 +01:00
|
|
|
sub new {
|
2022-02-07 04:32:34 +01:00
|
|
|
my ($class, %conf) = @_;
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
%$self = %conf;
|
2022-02-07 04:32:34 +01:00
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
$self->{debug} //= 0;
|
|
|
|
$self->{arguments} //= '';
|
|
|
|
$self->{default_options} //= '';
|
|
|
|
$self->{max_history} //= 10000;
|
2022-02-07 04:32:34 +01:00
|
|
|
|
|
|
|
$self->initialize(%conf);
|
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
# remove leading and trailing whitespace
|
|
|
|
$self->{nick} =~ s/^\s+|\s+$//g;
|
|
|
|
$self->{channel} =~ s/^\s+|\s+$//g;
|
|
|
|
$self->{lang} =~ s/^\s+|\s+$//g;
|
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
return $self;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
sub initialize($self, %conf) {}
|
|
|
|
|
|
|
|
sub process_interactive_edit($self) {
|
|
|
|
return interactive_edit($self);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub process_standard_options($self) {
|
|
|
|
my @opt_args = split_line($self->{code}, preserve_escapes => 1, keep_spaces => 0);
|
|
|
|
|
|
|
|
my $getopt_error;
|
|
|
|
local $SIG{__WARN__} = sub {
|
|
|
|
$getopt_error = shift;
|
|
|
|
chomp $getopt_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
my ($info, $arguments, $paste);
|
|
|
|
GetOptionsFromArray(\@opt_args,
|
|
|
|
'info!' => \$info,
|
|
|
|
'args|arguments=s' => \$arguments,
|
|
|
|
'paste!' => \$paste);
|
|
|
|
|
|
|
|
if ($info) {
|
|
|
|
my $cmdline = $self->{cmdline};
|
|
|
|
if (length $self->{default_options}) {
|
|
|
|
$cmdline =~ s/\$options/$self->{default_options}/;
|
|
|
|
} else {
|
|
|
|
$cmdline =~ s/\$options\s+//;
|
|
|
|
}
|
|
|
|
$cmdline =~ s/\$sourcefile/$self->{sourcefile}/g;
|
|
|
|
$cmdline =~ s/\$execfile/$self->{execfile}/g;
|
|
|
|
my $name = exists $self->{name} ? $self->{name} : $self->{lang};
|
|
|
|
print "$name cmdline: $cmdline\n";
|
2022-02-24 19:39:15 +01:00
|
|
|
$self->done;
|
2022-02-14 20:18:49 +01:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined $arguments) {
|
|
|
|
if (not $arguments =~ s/^"(.*)"$/$1/) {
|
|
|
|
$arguments =~ s/^'(.*)'$/$1/;
|
|
|
|
}
|
|
|
|
$self->{arguments} = $arguments;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($paste) {
|
|
|
|
$self->add_option("-paste");
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->{code} = join ' ', @opt_args;
|
|
|
|
|
|
|
|
if ($self->{code} =~ s/-stdin[ =]?(.*)$//) {
|
|
|
|
$self->add_option("-stdin", $1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub process_custom_options {}
|
|
|
|
|
|
|
|
sub process_cmdline_options($self) {
|
|
|
|
my $code = $self->{code};
|
|
|
|
|
|
|
|
$self->{cmdline_options} = "";
|
|
|
|
|
|
|
|
while ($code =~ s/^\s*(-[^ ]+)\s*//) {
|
|
|
|
$self->{cmdline_options} .= "$1 ";
|
|
|
|
$self->add_option($1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->{cmdline_options} =~ s/\s$//;
|
|
|
|
|
|
|
|
$self->{code} = $code;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
2022-02-15 20:40:06 +01:00
|
|
|
sub add_option($self, $option, $value = '') {
|
2022-02-14 20:18:49 +01:00
|
|
|
$self->{options_order} //= [];
|
|
|
|
$self->{options}->{$option} = $value;
|
|
|
|
push @{$self->{options_order}}, $option;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
sub pretty_format($self, $code) {
|
|
|
|
return $code;
|
|
|
|
}
|
2015-04-05 11:24:01 +02:00
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
sub preprocess_code($self, %opts) {
|
2022-02-07 04:32:34 +01:00
|
|
|
if ($self->{only_show}) {
|
|
|
|
print "$self->{code}\n";
|
2022-02-24 19:39:15 +01:00
|
|
|
$self->done;
|
2022-02-07 04:32:34 +01:00
|
|
|
exit;
|
|
|
|
}
|
2015-04-05 11:24:01 +02:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
unless($self->{got_run} and $self->{copy_code}) {
|
2022-02-24 19:39:15 +01:00
|
|
|
$self->debug("---- preprocess\n");
|
|
|
|
$self->debug("$self->{nick} $self->{channel}: [$self->{arguments}] $self->{cmdline_options} $self->{code}\n", 0);
|
2022-02-07 04:32:34 +01:00
|
|
|
}
|
2015-04-05 11:24:01 +02:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
# replace \n outside of quotes with literal newline
|
|
|
|
my $new_code = "";
|
|
|
|
|
|
|
|
use constant {
|
|
|
|
NORMAL => 0,
|
|
|
|
DOUBLE_QUOTED => 1,
|
|
|
|
SINGLE_QUOTED => 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
my $state = NORMAL;
|
|
|
|
my $escaped = 0;
|
|
|
|
|
|
|
|
my @chars = split //, $self->{code};
|
|
|
|
foreach my $ch (@chars) {
|
|
|
|
given ($ch) {
|
|
|
|
when ('\\') {
|
2022-02-13 01:06:04 +01:00
|
|
|
if ($escaped == 0) {
|
2022-02-07 04:32:34 +01:00
|
|
|
$escaped = 1;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
if ($state == NORMAL) {
|
2022-02-07 04:32:34 +01:00
|
|
|
when ($_ eq '"' and not $escaped) {
|
|
|
|
$state = DOUBLE_QUOTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
when ($_ eq "'" and not $escaped) {
|
|
|
|
$state = SINGLE_QUOTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
when ($_ eq 'n' and $escaped == 1) {
|
|
|
|
$ch = "\n";
|
|
|
|
$escaped = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
if ($state == DOUBLE_QUOTED) {
|
2022-02-07 04:32:34 +01:00
|
|
|
when ($_ eq '"' and not $escaped) {
|
|
|
|
$state = NORMAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
if ($state == SINGLE_QUOTED) {
|
2022-02-07 04:32:34 +01:00
|
|
|
when ($_ eq "'" and not $escaped) {
|
|
|
|
$state = NORMAL;
|
|
|
|
}
|
|
|
|
}
|
2015-04-05 11:24:01 +02:00
|
|
|
}
|
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
$new_code .= '\\' and $escaped = 0 if $escaped;
|
|
|
|
$new_code .= $ch;
|
2015-04-05 11:24:01 +02:00
|
|
|
}
|
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
if (!$opts{omit_prelude} && exists $self->{prelude}) {
|
|
|
|
$self->{code} = "$self->{prelude}\n$self->{code}";
|
|
|
|
}
|
2021-09-17 22:30:46 +02:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
$self->{code} = $new_code;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub execute {
|
2022-02-07 04:32:34 +01:00
|
|
|
my ($self) = @_;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
my $input = $self->{'vm-input'};
|
|
|
|
my $output = $self->{'vm-output'};
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
my $date = time;
|
|
|
|
my $stdin = $self->{options}->{'-stdin'};
|
2019-06-13 06:35:04 +02:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
if (not length $stdin) {
|
2023-03-13 00:06:04 +01:00
|
|
|
$stdin = decode('UTF-8', `fortune -u -s`);
|
2022-02-07 04:32:34 +01:00
|
|
|
$stdin =~ s/[\n\r\t]/ /msg;
|
|
|
|
$stdin =~ s/:/ - /g;
|
|
|
|
$stdin =~ s/\s+/ /g;
|
|
|
|
$stdin =~ s/^\s+//;
|
|
|
|
$stdin =~ s/\s+$//;
|
|
|
|
}
|
2019-06-13 06:35:04 +02:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
$stdin =~ s/(?<!\\)\\n/\n/mg;
|
|
|
|
$stdin =~ s/(?<!\\)\\r/\r/mg;
|
|
|
|
$stdin =~ s/(?<!\\)\\t/\t/mg;
|
|
|
|
$stdin =~ s/(?<!\\)\\b/\b/mg;
|
|
|
|
$stdin =~ s/(?<!\\)\\x([a-f0-9]+)/chr hex $1/igme;
|
|
|
|
$stdin =~ s/(?<!\\)\\([0-7]+)/chr oct $1/gme;
|
2019-06-13 06:35:04 +02:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
my $pretty_code = $self->pretty_format($self->{code});
|
2019-06-13 06:35:04 +02:00
|
|
|
|
2015-01-16 07:00:20 +01:00
|
|
|
my $cmdline = $self->{cmdline};
|
2022-02-07 04:32:34 +01:00
|
|
|
|
2015-01-16 07:00:20 +01:00
|
|
|
$cmdline =~ s/\$sourcefile/$self->{sourcefile}/g;
|
|
|
|
$cmdline =~ s/\$execfile/$self->{execfile}/g;
|
2019-06-13 06:35:04 +02:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
my $options = length $self->{cmdline_options} ? $self->{cmdline_options} : $self->{default_options};
|
|
|
|
|
|
|
|
if ((not exists $self->{options}->{'-paste'}) and (not defined $self->{got_run} or $self->{got_run} ne 'paste')) {
|
|
|
|
if (exists $self->{options_nopaste}) {
|
|
|
|
$options .= ' ' if length $options;
|
|
|
|
$options .= $self->{options_nopaste};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (exists $self->{options_paste}) {
|
|
|
|
$options .= ' ' if length $options;
|
|
|
|
$options .= $self->{options_paste};
|
|
|
|
}
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
if (length $options) {
|
|
|
|
$cmdline =~ s/\$options/$options/;
|
|
|
|
} else {
|
|
|
|
$cmdline =~ s/\$options\s+//;
|
|
|
|
}
|
2019-06-25 23:41:54 +02:00
|
|
|
|
2022-02-24 19:39:15 +01:00
|
|
|
$self->debug("---- executing\n");
|
|
|
|
$self->debug("$cmdline\n$stdin\n$pretty_code\n", 0);
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
my $compile_in = {
|
|
|
|
lang => $self->{lang},
|
|
|
|
sourcefile => $self->{sourcefile},
|
|
|
|
execfile => $self->{execfile},
|
|
|
|
cmdline => $cmdline,
|
|
|
|
input => $stdin,
|
|
|
|
date => $date,
|
|
|
|
arguments => $self->{arguments},
|
|
|
|
code => $pretty_code
|
|
|
|
};
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
$compile_in->{'factoid'} = $self->{'factoid'} if length $self->{'factoid'};
|
|
|
|
$compile_in->{'persist-key'} = $self->{'persist-key'} if length $self->{'persist-key'};
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
my $compile_json = encode_json($compile_in);
|
2023-03-13 00:06:04 +01:00
|
|
|
$compile_json .= "\n:end:\n";
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
my $length = length $compile_json;
|
|
|
|
my $sent = 0;
|
2022-02-24 19:39:15 +01:00
|
|
|
my $chunk_max = 16384;
|
2022-02-07 04:32:34 +01:00
|
|
|
my $chunk_size = $length < $chunk_max ? $length : $chunk_max;
|
|
|
|
my $chunks_sent = 0;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-24 19:39:15 +01:00
|
|
|
# $self->debug("Sending $length bytes [$compile_json] to vm_server\n");
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
$chunk_size -= 1; # account for newline in syswrite
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
while ($chunks_sent < $length) {
|
|
|
|
my $chunk = substr $compile_json, $chunks_sent, $chunk_size;
|
2022-02-13 01:06:04 +01:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
$chunks_sent += length $chunk;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
my $ret = syswrite($input, $chunk);
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
if (not defined $ret) {
|
2022-02-24 19:39:15 +01:00
|
|
|
my $error = $!;
|
|
|
|
print STDERR "Error sending: $error\n";
|
|
|
|
$self->debug("Error sending: $error\n");
|
2022-02-07 04:32:34 +01:00
|
|
|
last;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
if ($ret == 0) {
|
2022-02-13 01:06:04 +01:00
|
|
|
print STDERR "Sent 0 bytes. Sleep 1 sec and try again\n";
|
2022-02-24 19:39:15 +01:00
|
|
|
$self->debug("Sent 0 bytes. Sleep 1 sec and try again\n");
|
2022-02-07 04:32:34 +01:00
|
|
|
sleep 1;
|
|
|
|
next;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
$sent += $ret;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
my $result = "";
|
|
|
|
my $got_result = 0;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
while (my $line = <$output>) {
|
2022-02-07 04:32:34 +01:00
|
|
|
utf8::decode($line);
|
|
|
|
$line =~ s/[\r\n]+$//;
|
|
|
|
last if $line =~ /^result:end$/;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
if ($line =~ /^result:/) {
|
2022-02-07 04:32:34 +01:00
|
|
|
$line =~ s/^result://;
|
2022-04-05 07:32:53 +02:00
|
|
|
|
2022-04-08 22:12:37 +02:00
|
|
|
my $octets = decode('UTF-8', $line, sub { sprintf '\\\\x%02X', shift });
|
2022-04-05 07:32:53 +02:00
|
|
|
$line = encode('UTF-8', $octets, Encode::FB_CROAK);
|
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
my $compile_out = decode_json($line);
|
|
|
|
$result .= "$compile_out->{result}\n";
|
|
|
|
$got_result = 1;
|
|
|
|
next;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
if ($got_result) {
|
2022-02-07 04:32:34 +01:00
|
|
|
$result .= "$line\n";
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
close $input;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
$self->{output} = $result;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
return $result;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
sub postprocess_output($self) {
|
|
|
|
unless($self->{got_run} and $self->{copy_code}) {
|
2022-02-24 19:39:15 +01:00
|
|
|
$self->debug("---- post-processing\n");
|
|
|
|
$self->debug("$self->{output}\n", 0);
|
2022-02-14 20:18:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# backspace
|
|
|
|
my $boutput = "";
|
|
|
|
my $active_position = 0;
|
|
|
|
$self->{output} =~ s/\n$//;
|
|
|
|
while ($self->{output} =~ /(.)/gms) {
|
|
|
|
my $c = $1;
|
|
|
|
if ($c eq "\b") {
|
|
|
|
if (--$active_position <= 0) {
|
|
|
|
$active_position = 0;
|
|
|
|
}
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
substr($boutput, $active_position++, 1) = $c;
|
|
|
|
}
|
|
|
|
$self->{output} = $boutput;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-04-08 03:24:57 +02:00
|
|
|
# bell
|
2022-02-14 20:18:49 +01:00
|
|
|
my @beeps = qw/*BEEP* *BING* *DING* *DONG* *CLUNK* *BONG* *PING* *BOOP* *BLIP* *BOP* *WHIRR*/;
|
|
|
|
$self->{output} =~ s/\007/$beeps[rand @beeps]/g;
|
2022-04-08 03:24:57 +02:00
|
|
|
|
|
|
|
# known control characters
|
|
|
|
my %escapes = (
|
|
|
|
"\e" => '<esc>',
|
|
|
|
"\f" => '<ff>',
|
|
|
|
|
|
|
|
# \r and \n are disabled so the bot itself processes them, e.g. when
|
|
|
|
# web-pasting, etc. feel free to uncomment them if you like (add them
|
|
|
|
# to the group in the substitution regex as well).
|
|
|
|
|
|
|
|
# "\r" => '<cr>',
|
|
|
|
# "\n" => '<nl>',
|
|
|
|
|
|
|
|
# \t is left alone
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->{output} =~ s/([\e\f])/$escapes{$1}/gs;
|
|
|
|
|
|
|
|
# other unprintables
|
2022-04-08 22:12:37 +02:00
|
|
|
my %disregard = ( "\n" => 1, "\r" => 1, "\t" => 1, "\x03" => 1 );
|
|
|
|
$self->{output} =~ s/([\x00-\x1f])/$disregard{$1} ? $1 : sprintf('\x%02X', ord $1)/gse;
|
2022-02-07 04:32:34 +01:00
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
sub show_output($self) {
|
|
|
|
my $output = $self->{output};
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
unless ($self->{got_run} and $self->{copy_code}) {
|
2022-02-24 19:39:15 +01:00
|
|
|
$self->debug("---- show output\n");
|
|
|
|
$self->debug("$output\n", 0);
|
|
|
|
$self->debug("=========================\n", 0);
|
2022-02-14 20:18:49 +01:00
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
if (exists $self->{options}->{'-paste'} or (defined $self->{got_run} and $self->{got_run} eq 'paste')) {
|
2023-03-13 00:12:58 +01:00
|
|
|
my $cmdline = "$self->{cmdline}\n";
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-07 04:32:34 +01:00
|
|
|
$cmdline =~ s/\$sourcefile/$self->{sourcefile}/g;
|
|
|
|
$cmdline =~ s/\$execfile/$self->{execfile}/g;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
my $options;
|
|
|
|
if (length $self->{cmdline_options}) {
|
|
|
|
$options = $self->{cmdline_options};
|
2022-02-07 04:32:34 +01:00
|
|
|
} else {
|
2022-02-14 20:18:49 +01:00
|
|
|
$options = $self->{default_options};
|
2022-02-07 04:32:34 +01:00
|
|
|
}
|
2015-06-17 06:51:09 +02:00
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
if (exists $self->{options_paste}) {
|
|
|
|
$options .= ' ' if length $options;
|
|
|
|
$options .= $self->{options_paste};
|
2022-02-07 04:32:34 +01:00
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
if (length $options) {
|
|
|
|
$cmdline =~ s/\$options/$options/;
|
2022-02-07 04:32:34 +01:00
|
|
|
} else {
|
2022-02-14 20:18:49 +01:00
|
|
|
$cmdline =~ s/\$options\s+//;
|
2022-02-07 04:32:34 +01:00
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
if (length $self->{arguments}) {
|
|
|
|
$cmdline .= "arguments: $self->{arguments}\n";
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
if ($self->{options}->{'-stdin'}) {
|
|
|
|
$cmdline .= "stdin: $self->{options}->{'-stdin'}\n";
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
my $pretty_code = $self->pretty_format($self->{code});
|
2022-02-07 04:32:34 +01:00
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
my $cmdline_opening_comment = $self->{cmdline_opening_comment} // "/************* CMDLINE *************\n";
|
|
|
|
my $cmdline_closing_comment = $self->{cmdline_closing_comment} // "************** CMDLINE *************/\n";
|
2022-02-07 04:32:34 +01:00
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
my $output_opening_comment = $self->{output_opening_comment} // "/************* OUTPUT *************\n";
|
|
|
|
my $output_closing_comment = $self->{output_closing_comment} // "************** OUTPUT *************/\n";
|
2022-02-07 04:32:34 +01:00
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
$pretty_code .= "\n\n";
|
|
|
|
$pretty_code .= $cmdline_opening_comment;
|
|
|
|
$pretty_code .= "$cmdline";
|
|
|
|
$pretty_code .= $cmdline_closing_comment;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
$output =~ s/\s+$//;
|
|
|
|
$pretty_code .= "\n";
|
|
|
|
$pretty_code .= $output_opening_comment;
|
|
|
|
$pretty_code .= "$output\n";
|
|
|
|
$pretty_code .= $output_closing_comment;
|
2022-02-07 04:32:34 +01:00
|
|
|
|
2022-02-24 19:39:15 +01:00
|
|
|
my $uri = paste_0x0(encode('UTF-8', $pretty_code));
|
2022-02-14 20:18:49 +01:00
|
|
|
print "$uri\n";
|
2022-02-24 19:39:15 +01:00
|
|
|
$self->done;
|
2022-02-14 20:18:49 +01:00
|
|
|
exit 0;
|
2022-02-07 04:32:34 +01:00
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2022-02-24 19:39:15 +01:00
|
|
|
if ($self->{channel} =~ m/^#/ and length $output > 22 and open my $fh, '<:encoding(UTF-8)', "$RealBin/../history/$self->{channel}-$self->{lang}.last-output") {
|
2022-02-14 20:18:49 +01:00
|
|
|
my $last_output;
|
2022-02-24 19:39:15 +01:00
|
|
|
my $time = <$fh>;
|
2022-02-07 04:32:34 +01:00
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
if (gettimeofday - $time > 60 * 4) {
|
2022-02-24 19:39:15 +01:00
|
|
|
close $fh;
|
2022-02-14 20:18:49 +01:00
|
|
|
} else {
|
2022-02-24 19:39:15 +01:00
|
|
|
while (my $line = <$fh>) {
|
2022-02-14 20:18:49 +01:00
|
|
|
$last_output .= $line;
|
2022-02-07 04:32:34 +01:00
|
|
|
}
|
2022-02-24 19:39:15 +01:00
|
|
|
close $fh;
|
2022-02-07 04:32:34 +01:00
|
|
|
|
2022-02-14 20:18:49 +01:00
|
|
|
if ((not $self->{factoid}) and defined $last_output and $last_output eq $output) {
|
|
|
|
print "Same output.\n";
|
2022-02-24 19:39:15 +01:00
|
|
|
$self->done;
|
2022-02-07 04:32:34 +01:00
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
2022-02-24 19:39:15 +01:00
|
|
|
print encode('UTF-8', "$output\n");
|
2019-06-13 06:35:04 +02:00
|
|
|
|
2022-02-24 19:39:15 +01:00
|
|
|
my $file = "$RealBin/../history/$self->{channel}-$self->{lang}.last-output";
|
|
|
|
open my $fh, '>:encoding(UTF-8)', $file or die "Couldn't open $file: $!";
|
2022-02-14 20:18:49 +01:00
|
|
|
my $now = gettimeofday;
|
2022-02-24 19:39:15 +01:00
|
|
|
print $fh "$now\n";
|
|
|
|
print $fh "$output";
|
|
|
|
close $fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub debug($self, $text, $timestamp = 1) {
|
|
|
|
if (not exists $self->{logh}) {
|
|
|
|
open $self->{logh}, '>>:encoding(UTF-8)', "$RealBin/../log.txt" or die "Could not open log file: $!";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($timestamp) {
|
|
|
|
my ($sec, $usec) = gettimeofday;
|
|
|
|
my $time = strftime "%a %b %e %Y %H:%M:%S", localtime $sec;
|
|
|
|
$time .= sprintf ".%03d", $usec / 1000;
|
|
|
|
print { $self->{logh} } "$time :: $text";
|
|
|
|
} else {
|
|
|
|
print { $self->{logh} } $text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub done($self) {
|
|
|
|
if ($self->{logh}) {
|
|
|
|
close $self->{logh};
|
|
|
|
delete $self->{logh};
|
|
|
|
}
|
2019-06-13 06:35:04 +02:00
|
|
|
}
|
|
|
|
|
2015-01-15 06:51:17 +01:00
|
|
|
1;
|