2015-01-15 06:51:17 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
use feature "switch";
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
2015-01-15 06:51:17 +01:00
|
|
|
|
|
|
|
no if $] >= 5.018, warnings => "experimental::smartmatch";
|
|
|
|
|
|
|
|
package _default;
|
|
|
|
|
|
|
|
use IPC::Open2;
|
|
|
|
use IO::Socket;
|
|
|
|
use LWP::UserAgent;
|
|
|
|
use Time::HiRes qw/gettimeofday/;
|
|
|
|
use Text::Balanced qw/extract_delimited/;
|
2017-09-15 04:10:21 +02:00
|
|
|
use JSON;
|
2019-06-25 23:41:54 +02:00
|
|
|
use Getopt::Long qw/GetOptionsFromArray :config pass_through no_ignore_case no_auto_abbrev/;
|
2019-07-11 03:40:53 +02:00
|
|
|
use Encode;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-01-16 07:00:20 +01:00
|
|
|
my $EXECUTE_PORT = '3333';
|
2015-01-15 06:51:17 +01:00
|
|
|
|
|
|
|
sub new {
|
|
|
|
my ($class, %conf) = @_;
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
|
|
|
|
$self->{debug} = $conf{debug} // 0;
|
|
|
|
$self->{nick} = $conf{nick};
|
|
|
|
$self->{channel} = $conf{channel};
|
|
|
|
$self->{lang} = $conf{lang};
|
|
|
|
$self->{code} = $conf{code};
|
|
|
|
$self->{max_history} = $conf{max_history} // 10000;
|
2017-09-12 14:50:49 +02:00
|
|
|
$self->{arguments} = $conf{arguments};
|
2017-09-16 01:41:36 +02:00
|
|
|
$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->{default_options} = '';
|
|
|
|
$self->{cmdline} = 'echo Hello, world!';
|
|
|
|
|
2015-01-18 14:42:28 +01:00
|
|
|
# remove leading and trailing whitespace
|
2015-04-05 11:24:01 +02:00
|
|
|
$self->{nick} =~ s/^\s+|\s+$//g if defined $self->{nick};
|
|
|
|
$self->{channel} =~ s/^\s+|\s+$//g if defined $self->{channel};
|
|
|
|
$self->{lang} =~ s/^\s+|\s+$//g if defined $self->{lang};
|
2015-01-18 14:42:28 +01:00
|
|
|
|
2015-01-15 06:51:17 +01:00
|
|
|
$self->initialize(%conf);
|
|
|
|
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub pretty_format {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{code};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub preprocess_code {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
if ($self->{only_show}) {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "$self->{code}\n";
|
2015-01-15 06:51:17 +01:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
unless($self->{got_run} and $self->{copy_code}) {
|
|
|
|
open FILE, ">> log.txt";
|
|
|
|
print FILE localtime() . "\n";
|
2019-06-13 06:35:04 +02:00
|
|
|
print FILE "$self->{nick} $self->{channel}: [" . $self->{arguments} . "] " . $self->{cmdline_options} . "$self->{code}\n";
|
2015-01-15 06:51:17 +01:00
|
|
|
close FILE;
|
|
|
|
}
|
2015-04-05 11:24:01 +02:00
|
|
|
|
2019-06-13 06:35:04 +02:00
|
|
|
if (exists $self->{prelude}) {
|
|
|
|
$self->{code} = "$self->{prelude}\n$self->{code}";
|
|
|
|
}
|
|
|
|
|
2015-04-05 11:24:01 +02: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;
|
|
|
|
|
|
|
|
while($self->{code} =~ m/(.)/gs) {
|
|
|
|
my $ch = $1;
|
|
|
|
|
|
|
|
given ($ch) {
|
|
|
|
when ('\\') {
|
|
|
|
if($escaped == 0) {
|
|
|
|
$escaped = 1;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($state == NORMAL) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($state == DOUBLE_QUOTED) {
|
|
|
|
when ($_ eq '"' and not $escaped) {
|
|
|
|
$state = NORMAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($state == SINGLE_QUOTED) {
|
|
|
|
when ($_ eq "'" and not $escaped) {
|
|
|
|
$state = NORMAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$new_code .= '\\' and $escaped = 0 if $escaped;
|
|
|
|
$new_code .= $ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->{code} = $new_code;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub postprocess_output {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
unless($self->{got_run} and $self->{copy_code}) {
|
|
|
|
open FILE, ">> log.txt";
|
|
|
|
print FILE "------------------------------------------------------------------------\n";
|
|
|
|
print FILE localtime() . "\n";
|
|
|
|
print FILE "$self->{output}\n";
|
|
|
|
close FILE;
|
|
|
|
}
|
2015-04-16 12:12:07 +02: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;
|
2016-06-28 21:04:54 +02:00
|
|
|
|
2017-02-27 13:46:55 +01:00
|
|
|
my @beeps = qw/*BEEP* *BING* *DING* *DONG* *CLUNK* *BONG* *PING* *BOOP* *BLIP* *BOP* *WHIRR*/;
|
|
|
|
|
|
|
|
$self->{output} =~ s/\007/$beeps[rand @beeps]/g;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub show_output {
|
|
|
|
my $self = shift;
|
|
|
|
my $output = $self->{output};
|
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
unless ($self->{got_run} and $self->{copy_code}) {
|
2015-01-15 06:51:17 +01:00
|
|
|
open FILE, ">> log.txt";
|
|
|
|
print FILE "------------------------------------------------------------------------\n";
|
|
|
|
print FILE localtime() . "\n";
|
|
|
|
print FILE "$output\n";
|
|
|
|
print FILE "========================================================================\n";
|
|
|
|
close FILE;
|
|
|
|
}
|
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if (exists $self->{options}->{'-paste'} or (defined $self->{got_run} and $self->{got_run} eq 'paste')) {
|
2019-06-25 23:41:54 +02:00
|
|
|
my $cmdline = "command: $self->{cmdline}\n";
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-01-17 07:23:28 +01:00
|
|
|
$cmdline =~ s/\$sourcefile/$self->{sourcefile}/g;
|
|
|
|
$cmdline =~ s/\$execfile/$self->{execfile}/g;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-01-17 13:41:50 +01:00
|
|
|
my $options;
|
2015-01-17 07:23:28 +01:00
|
|
|
if (length $self->{cmdline_options}) {
|
2015-01-17 13:41:50 +01:00
|
|
|
$options = $self->{cmdline_options};
|
2015-01-15 06:51:17 +01:00
|
|
|
} else {
|
2015-01-17 13:41:50 +01:00
|
|
|
$options = $self->{default_options};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exists $self->{options_paste}) {
|
|
|
|
$options .= ' ' if length $options;
|
|
|
|
$options .= $self->{options_paste};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length $options) {
|
|
|
|
$cmdline =~ s/\$options/$options/;
|
|
|
|
} else {
|
|
|
|
$cmdline =~ s/\$options\s+//;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
2019-06-25 23:41:54 +02:00
|
|
|
if (length $self->{arguments}) {
|
|
|
|
$cmdline .= "arguments: $self->{arguments}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($self->{options}->{'-stdin'}) {
|
|
|
|
$cmdline .= "stdin: $self->{options}->{'-stdin'}\n";
|
|
|
|
}
|
|
|
|
|
2015-01-15 06:51:17 +01:00
|
|
|
my $pretty_code = $self->pretty_format($self->{code});
|
|
|
|
|
2015-01-24 04:23:33 +01:00
|
|
|
my $cmdline_opening_comment = $self->{cmdline_opening_comment} // "/************* CMDLINE *************\n";
|
|
|
|
my $cmdline_closing_comment = $self->{cmdline_closing_comment} // "************** CMDLINE *************/\n";
|
|
|
|
|
|
|
|
my $output_opening_comment = $self->{output_opening_comment} // "/************* OUTPUT *************\n";
|
|
|
|
my $output_closing_comment = $self->{output_closing_comment} // "************** OUTPUT *************/\n";
|
|
|
|
|
|
|
|
$pretty_code .= "\n\n";
|
|
|
|
$pretty_code .= $cmdline_opening_comment;
|
2019-06-25 23:41:54 +02:00
|
|
|
$pretty_code .= "$cmdline";
|
2015-01-24 04:23:33 +01:00
|
|
|
$pretty_code .= $cmdline_closing_comment;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
|
|
|
$output =~ s/\s+$//;
|
2015-01-24 04:23:33 +01:00
|
|
|
$pretty_code .= "\n";
|
|
|
|
$pretty_code .= $output_opening_comment;
|
|
|
|
$pretty_code .= "$output\n";
|
|
|
|
$pretty_code .= $output_closing_comment;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2018-03-12 21:33:14 +01:00
|
|
|
my $uri = $self->paste_ixio($pretty_code);
|
2017-09-11 04:53:29 +02:00
|
|
|
print "$uri\n";
|
2015-01-15 06:51:17 +01:00
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
2015-01-24 04:23:33 +01:00
|
|
|
if($self->{channel} =~ m/^#/ and length $output > 22 and open FILE, "< history/$self->{channel}-$self->{lang}.last-output") {
|
2015-01-15 06:51:17 +01:00
|
|
|
my $last_output;
|
|
|
|
my $time = <FILE>;
|
|
|
|
|
2015-01-24 04:23:33 +01:00
|
|
|
if(gettimeofday - $time > 60 * 4) {
|
2015-01-15 06:51:17 +01:00
|
|
|
close FILE;
|
|
|
|
} else {
|
|
|
|
while(my $line = <FILE>) {
|
|
|
|
$last_output .= $line;
|
|
|
|
}
|
|
|
|
close FILE;
|
|
|
|
|
2017-09-16 01:41:36 +02:00
|
|
|
if((not $self->{factoid}) and defined $last_output and $last_output eq $output) {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "Same output.\n";
|
2015-01-15 06:51:17 +01:00
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-11 04:53:29 +02:00
|
|
|
print "$output\n";
|
2015-01-15 06:51:17 +01:00
|
|
|
|
|
|
|
open FILE, "> history/$self->{channel}-$self->{lang}.last-output" or die "Couldn't open $self->{channel}-$self->{lang}.last-output: $!";
|
|
|
|
my $now = gettimeofday;
|
|
|
|
print FILE "$now\n";
|
|
|
|
print FILE "$output";
|
|
|
|
close FILE;
|
|
|
|
}
|
|
|
|
|
2018-03-12 21:33:14 +01:00
|
|
|
sub paste_ixio {
|
2015-01-15 06:51:17 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $text = join(' ', @_);
|
|
|
|
|
|
|
|
$text =~ s/(.{120})\s/$1\n/g;
|
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new();
|
|
|
|
$ua->agent("Mozilla/5.0");
|
|
|
|
push @{ $ua->requests_redirectable }, 'POST';
|
2018-03-12 21:33:14 +01:00
|
|
|
$ua->timeout(10);
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2018-03-12 21:33:14 +01:00
|
|
|
my %post = ('f:1' => $text);
|
|
|
|
my $response = $ua->post("http://ix.io", \%post);
|
2015-01-15 06:51:17 +01:00
|
|
|
|
|
|
|
if(not $response->is_success) {
|
2018-03-12 21:33:14 +01:00
|
|
|
return "error pasting: " . $response->status_line;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 03:40:53 +02:00
|
|
|
my $result = $response->decoded_content;
|
2018-03-12 21:33:14 +01:00
|
|
|
$result =~ s/^\s+//;
|
|
|
|
$result =~ s/\s+$//;
|
|
|
|
return $result;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub execute {
|
2015-07-12 11:54:08 +02:00
|
|
|
my ($self) = @_;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
|
|
|
my ($compiler, $compiler_output, $pid);
|
|
|
|
|
2015-07-12 11:54:08 +02:00
|
|
|
delete $self->{local};
|
|
|
|
if(exists $self->{local} and $self->{local} != 0) {
|
2015-01-15 06:51:17 +01:00
|
|
|
print "Using local compiler instead of virtual machine\n";
|
|
|
|
$pid = open2($compiler_output, $compiler, './compiler_vm_server.pl') || die "repl failed: $@\n";
|
|
|
|
print "Started compiler, pid: $pid\n";
|
|
|
|
} else {
|
|
|
|
$compiler = IO::Socket::INET->new(PeerAddr => '127.0.0.1', PeerPort => $EXECUTE_PORT, Proto => 'tcp', Type => SOCK_STREAM);
|
|
|
|
die "Could not create socket: $!" unless $compiler;
|
|
|
|
$compiler_output = $compiler;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $date = time;
|
2019-06-25 23:41:54 +02:00
|
|
|
my $stdin = $self->{options}->{'-stdin'};
|
|
|
|
|
|
|
|
if (not length $stdin) {
|
|
|
|
$stdin = `fortune -u -s`;
|
|
|
|
$stdin =~ s/[\n\r\t]/ /msg;
|
|
|
|
$stdin =~ s/:/ - /g;
|
|
|
|
$stdin =~ s/\s+/ /g;
|
|
|
|
$stdin =~ s/^\s+//;
|
|
|
|
$stdin =~ s/\s+$//;
|
2015-07-18 17:12:59 +02:00
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2019-06-25 23:41:54 +02:00
|
|
|
$stdin =~ s/(?<!\\)\\n/\n/mg;
|
|
|
|
$stdin =~ s/(?<!\\)\\r/\r/mg;
|
|
|
|
$stdin =~ s/(?<!\\)\\t/\t/mg;
|
|
|
|
$stdin =~ s/(?<!\\)\\b/\b/mg;
|
2019-06-26 08:21:15 +02:00
|
|
|
$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
|
|
|
|
2015-01-15 06:51:17 +01:00
|
|
|
my $pretty_code = $self->pretty_format($self->{code});
|
|
|
|
|
|
|
|
my $cmdline = $self->{cmdline};
|
|
|
|
|
|
|
|
$cmdline =~ s/\$sourcefile/$self->{sourcefile}/g;
|
|
|
|
$cmdline =~ s/\$execfile/$self->{execfile}/g;
|
|
|
|
|
2015-01-17 13:41:50 +01:00
|
|
|
my $options;
|
2015-01-15 06:51:17 +01:00
|
|
|
if (length $self->{cmdline_options}) {
|
2015-01-17 13:41:50 +01:00
|
|
|
$options = $self->{cmdline_options};
|
|
|
|
} else {
|
|
|
|
$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};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length $options) {
|
|
|
|
$cmdline =~ s/\$options/$options/;
|
2015-01-15 06:51:17 +01:00
|
|
|
} else {
|
2015-01-17 13:41:50 +01:00
|
|
|
$cmdline =~ s/\$options\s+//;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
open FILE, ">> log.txt";
|
|
|
|
print FILE "------------------------------------------------------------------------\n";
|
|
|
|
print FILE localtime() . "\n";
|
2019-06-25 23:41:54 +02:00
|
|
|
print FILE "$cmdline\n$stdin\n$pretty_code\n";
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2019-06-13 06:35:04 +02:00
|
|
|
my $compile_in = {
|
|
|
|
lang => $self->{lang},
|
|
|
|
sourcefile => $self->{sourcefile},
|
|
|
|
execfile => $self->{execfile},
|
|
|
|
cmdline => $cmdline,
|
2019-06-25 23:41:54 +02:00
|
|
|
input => $stdin,
|
2019-06-13 06:35:04 +02:00
|
|
|
date => $date,
|
|
|
|
arguments => $self->{arguments},
|
|
|
|
code => $pretty_code
|
|
|
|
};
|
2017-09-19 06:36:40 +02:00
|
|
|
|
|
|
|
$compile_in->{'factoid'} = $self->{'factoid'} if length $self->{'factoid'};
|
|
|
|
$compile_in->{'persist-key'} = $self->{'persist-key'} if length $self->{'persist-key'};
|
|
|
|
|
2017-09-15 04:10:21 +02:00
|
|
|
my $compile_json = encode_json($compile_in);
|
2019-07-11 03:40:53 +02:00
|
|
|
$compile_json .= encode('UTF-8', "\n:end:\n");
|
2017-12-02 20:37:51 +01:00
|
|
|
|
|
|
|
my $length = length $compile_json;
|
|
|
|
my $sent = 0;
|
|
|
|
my $chunk_max = 4096;
|
|
|
|
my $chunk_size = $length < $chunk_max ? $length : $chunk_max;
|
2019-06-13 06:35:04 +02:00
|
|
|
my $chunks_sent = 0;
|
2017-12-02 20:37:51 +01:00
|
|
|
|
|
|
|
#print FILE "Sending $length bytes [$compile_json] to vm_server\n";
|
|
|
|
|
|
|
|
$chunk_size -= 1; # account for newline in syswrite
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2017-12-02 20:37:51 +01:00
|
|
|
while ($chunks_sent < $length) {
|
|
|
|
my $chunk = substr $compile_json, $chunks_sent, $chunk_size;
|
|
|
|
#print FILE "Sending chunk [$chunk]\n";
|
|
|
|
$chunks_sent += length $chunk;
|
|
|
|
|
2019-07-11 03:40:53 +02:00
|
|
|
my $ret = syswrite($compiler, $chunk);
|
2017-09-15 04:10:21 +02:00
|
|
|
|
2017-12-02 20:37:51 +01:00
|
|
|
if (not defined $ret) {
|
|
|
|
print FILE "Error sending: $!\n";
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($ret == 0) {
|
|
|
|
print FILE "Sent 0 bytes. Sleep 1 sec and try again\n";
|
|
|
|
sleep 1;
|
|
|
|
next;
|
|
|
|
}
|
2017-09-15 04:10:21 +02:00
|
|
|
|
2017-12-02 20:37:51 +01:00
|
|
|
$sent += $ret;
|
|
|
|
print FILE "Sent $ret bytes, so far $sent ...\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
#print FILE "Done sending!\n";
|
|
|
|
close FILE;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
|
|
|
my $result = "";
|
|
|
|
my $got_result = 0;
|
|
|
|
|
|
|
|
while(my $line = <$compiler_output>) {
|
2019-07-11 03:40:53 +02:00
|
|
|
utf8::decode($line);
|
|
|
|
print STDERR "Read from vm [$line]\n";
|
2017-09-15 04:10:21 +02:00
|
|
|
|
|
|
|
$line =~ s/[\r\n]+$//;
|
2015-01-15 06:51:17 +01:00
|
|
|
last if $line =~ /^result:end$/;
|
|
|
|
|
|
|
|
if($line =~ /^result:/) {
|
|
|
|
$line =~ s/^result://;
|
2017-09-15 04:10:21 +02:00
|
|
|
my $compile_out = decode_json($line);
|
|
|
|
$result .= "$compile_out->{result}\n";
|
2015-01-15 06:51:17 +01:00
|
|
|
$got_result = 1;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($got_result) {
|
|
|
|
$result .= "$line\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close $compiler;
|
|
|
|
waitpid($pid, 0) if defined $pid;
|
|
|
|
|
|
|
|
$self->{output} = $result;
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub add_option {
|
|
|
|
my $self = shift;
|
|
|
|
my ($option, $value) = @_;
|
|
|
|
|
|
|
|
$self->{options_order} = [] if not exists $self->{options_order};
|
|
|
|
|
|
|
|
$self->{options}->{$option} = $value;
|
2017-09-11 04:53:29 +02:00
|
|
|
push @{$self->{options_order}}, $option;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub process_standard_options {
|
|
|
|
my $self = shift;
|
|
|
|
|
2019-07-07 00:53:34 +02:00
|
|
|
my @opt_args = $self->split_line($self->{code}, preserve_escapes => 1, keep_spaces => 1);
|
2019-06-13 06:35:04 +02:00
|
|
|
|
|
|
|
use Data::Dumper;
|
2019-06-25 04:01:38 +02:00
|
|
|
print STDERR "opt_arg: ", Dumper \@opt_args;
|
2019-06-13 06:35:04 +02:00
|
|
|
|
|
|
|
my $getopt_error;
|
|
|
|
local $SIG{__WARN__} = sub {
|
|
|
|
$getopt_error = shift;
|
|
|
|
chomp $getopt_error;
|
|
|
|
};
|
|
|
|
|
2019-06-25 23:41:54 +02:00
|
|
|
my ($info, $arguments, $paste);
|
2020-10-23 09:46:23 +02:00
|
|
|
GetOptionsFromArray(\@opt_args,
|
2019-06-13 06:35:04 +02:00
|
|
|
'info!' => \$info,
|
|
|
|
'args|arguments=s' => \$arguments,
|
|
|
|
'paste!' => \$paste);
|
|
|
|
|
|
|
|
if ($info) {
|
2015-01-16 07:00:20 +01:00
|
|
|
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;
|
2015-04-09 20:03:04 +02:00
|
|
|
my $name = exists $self->{name} ? $self->{name} : $self->{lang};
|
2017-09-11 04:53:29 +02:00
|
|
|
print "$name cmdline: $cmdline\n";
|
2015-01-16 07:00:20 +01:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2019-06-13 06:35:04 +02:00
|
|
|
if (defined $arguments) {
|
|
|
|
if (not $arguments =~ s/^"(.*)"$/$1/) {
|
|
|
|
$arguments =~ s/^'(.*)'$/$1/;
|
|
|
|
}
|
|
|
|
$self->{arguments} = $arguments;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($paste) {
|
2015-01-15 06:51:17 +01:00
|
|
|
$self->add_option("-paste");
|
|
|
|
}
|
|
|
|
|
2019-06-13 06:35:04 +02:00
|
|
|
$self->{code} = join ' ', @opt_args;
|
2019-06-25 23:41:54 +02:00
|
|
|
|
|
|
|
if ($self->{code} =~ s/-stdin[ =]?(.*)$//) {
|
|
|
|
$self->add_option("-stdin", $1);
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub process_custom_options {
|
|
|
|
}
|
|
|
|
|
|
|
|
sub process_cmdline_options {
|
|
|
|
my $self = shift;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub process_interactive_edit {
|
|
|
|
my $self = shift;
|
|
|
|
my $code = $self->{code};
|
2015-06-17 06:51:09 +02:00
|
|
|
my (@last_code, $unshift_last_code);
|
2015-01-15 06:51:17 +01:00
|
|
|
|
|
|
|
print " code: [$code]\n" if $self->{debug};
|
|
|
|
|
|
|
|
my $subcode = $code;
|
|
|
|
while ($subcode =~ s/^\s*(-[^ ]+)\s*//) {}
|
|
|
|
|
|
|
|
my $copy_code;
|
|
|
|
if($subcode =~ s/^\s*copy\s+(\S+)\s*//) {
|
|
|
|
my $copy = $1;
|
|
|
|
|
|
|
|
if(open FILE, "< history/$copy-$self->{lang}.hist") {
|
|
|
|
$copy_code = <FILE>;
|
|
|
|
close FILE;
|
|
|
|
goto COPY_ERROR if not $copy_code;;
|
|
|
|
chomp $copy_code;
|
|
|
|
} else {
|
|
|
|
goto COPY_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto COPY_SUCCESS;
|
|
|
|
|
|
|
|
COPY_ERROR:
|
2017-09-11 04:53:29 +02:00
|
|
|
print "No history for $copy.\n";
|
2015-01-15 06:51:17 +01:00
|
|
|
exit 0;
|
|
|
|
|
|
|
|
COPY_SUCCESS:
|
|
|
|
$code = $copy_code;
|
|
|
|
$self->{only_show} = 1;
|
|
|
|
$self->{copy_code} = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($subcode =~ m/^\s*(?:and\s+)?(?:diff|show)\s+(\S+)\s*$/) {
|
|
|
|
$self->{channel} = $1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(open FILE, "< history/$self->{channel}-$self->{lang}.hist") {
|
|
|
|
while(my $line = <FILE>) {
|
|
|
|
chomp $line;
|
|
|
|
push @last_code, $line;
|
|
|
|
}
|
|
|
|
close FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
unshift @last_code, $copy_code if defined $copy_code;
|
|
|
|
|
|
|
|
if($subcode =~ m/^\s*(?:and\s+)?show(?:\s+\S+)?\s*$/i) {
|
|
|
|
if(defined $last_code[0]) {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "$last_code[0]\n";
|
2015-01-15 06:51:17 +01:00
|
|
|
} else {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "No recent code to show.\n"
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
my $prevchange = $last_code[0];
|
|
|
|
my @replacements;
|
|
|
|
my $got_changes = 0;
|
|
|
|
my $got_sub = 0;
|
|
|
|
my $got_diff = 0;
|
|
|
|
my $got_undo = 0;
|
|
|
|
my $last_keyword;
|
|
|
|
|
|
|
|
while($subcode =~ s/^\s*(and)?\s*undo//) {
|
|
|
|
splice @last_code, 0, 1;
|
|
|
|
if(not defined $last_code[0]) {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "No more undos remaining.\n";
|
2015-06-17 06:51:09 +02:00
|
|
|
exit 0;
|
2015-01-15 06:51:17 +01:00
|
|
|
} else {
|
2015-06-17 06:51:09 +02:00
|
|
|
$code = $last_code[0];
|
2015-09-04 06:32:44 +02:00
|
|
|
$prevchange = $last_code[0];
|
2015-06-17 06:51:09 +02:00
|
|
|
$got_undo = 1;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
while(1) {
|
|
|
|
$got_sub = 0;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
$subcode =~ s/^\s*and\s+'/and $last_keyword '/ if defined $last_keyword;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if($subcode =~ m/^\s*(?:and\s+)?diff\b/i) {
|
|
|
|
$got_diff = 1;
|
|
|
|
last;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2016-06-28 21:04:54 +02:00
|
|
|
if($subcode =~ m/^\s*(?:and\s+)?(again|run|paste)\b/i) {
|
2015-06-17 06:51:09 +02:00
|
|
|
$self->{got_run} = lc $1;
|
|
|
|
$self->{only_show} = 0;
|
|
|
|
if ($prevchange) {
|
|
|
|
$code = $prevchange;
|
|
|
|
} else {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "No recent code to $self->{got_run}.\n";
|
2015-06-17 06:51:09 +02:00
|
|
|
exit 0;
|
|
|
|
}
|
2019-06-26 18:34:19 +02:00
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if($subcode =~ m/^\s*(and)?\s*remove \s*([^']+)?\s*'/) {
|
|
|
|
$last_keyword = 'remove';
|
|
|
|
my $modifier = 'first';
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
$subcode =~ s/^\s*(and)?\s*//;
|
|
|
|
$subcode =~ s/remove\s*([^']+)?\s*//i;
|
|
|
|
$modifier = $1 if defined $1;
|
|
|
|
$modifier =~ s/\s+$//;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
my ($e, $r) = extract_delimited($subcode, "'");
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
my $text;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if(defined $e) {
|
|
|
|
$text = $e;
|
|
|
|
$text =~ s/^'//;
|
|
|
|
$text =~ s/'$//;
|
|
|
|
$subcode = "replace $modifier '$text' with ''$r";
|
|
|
|
} else {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "Unbalanced single quotes. Usage: cc remove [all, first, .., tenth, last] 'text' [and ...]\n";
|
2015-06-17 06:51:09 +02:00
|
|
|
exit 0;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
2015-06-17 06:51:09 +02:00
|
|
|
next;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if($subcode =~ s/^\s*(and)?\s*prepend '//) {
|
|
|
|
$last_keyword = 'prepend';
|
|
|
|
$subcode = "'$subcode";
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
my ($e, $r) = extract_delimited($subcode, "'");
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
my $text;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if(defined $e) {
|
|
|
|
$text = $e;
|
|
|
|
$text =~ s/^'//;
|
|
|
|
$text =~ s/'$//;
|
|
|
|
$subcode = $r;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
$got_sub = 1;
|
|
|
|
$got_changes = 1;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if(not defined $prevchange) {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "No recent code to prepend to.\n";
|
2015-01-15 06:51:17 +01:00
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
$code = $prevchange;
|
|
|
|
$code =~ s/^/$text /;
|
|
|
|
$prevchange = $code;
|
|
|
|
} else {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "Unbalanced single quotes. Usage: cc prepend 'text' [and ...]\n";
|
2015-06-17 06:51:09 +02:00
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
next;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if($subcode =~ s/^\s*(and)?\s*append '//) {
|
|
|
|
$last_keyword = 'append';
|
|
|
|
$subcode = "'$subcode";
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
my ($e, $r) = extract_delimited($subcode, "'");
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
my $text;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if(defined $e) {
|
|
|
|
$text = $e;
|
|
|
|
$text =~ s/^'//;
|
|
|
|
$text =~ s/'$//;
|
|
|
|
$subcode = $r;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
|
|
|
$got_sub = 1;
|
2015-06-17 06:51:09 +02:00
|
|
|
$got_changes = 1;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if(not defined $prevchange) {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "No recent code to append to.\n";
|
2015-01-15 06:51:17 +01:00
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
$code = $prevchange;
|
|
|
|
$code =~ s/$/ $text/;
|
|
|
|
$prevchange = $code;
|
|
|
|
} else {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "Unbalanced single quotes. Usage: cc append 'text' [and ...]\n";
|
2015-06-17 06:51:09 +02:00
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
next;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if($subcode =~ m/^\s*(and)?\s*replace\s*([^']+)?\s*'.*'\s*with\s*'.*?'/i) {
|
|
|
|
$last_keyword = 'replace';
|
|
|
|
$got_sub = 1;
|
|
|
|
my $modifier = 'first';
|
|
|
|
|
|
|
|
$subcode =~ s/^\s*(and)?\s*//;
|
|
|
|
$subcode =~ s/replace\s*([^']+)?\s*//i;
|
|
|
|
$modifier = $1 if defined $1;
|
|
|
|
$modifier =~ s/\s+$//;
|
|
|
|
|
|
|
|
my ($from, $to);
|
|
|
|
my ($e, $r) = extract_delimited($subcode, "'");
|
|
|
|
|
|
|
|
if(defined $e) {
|
|
|
|
$from = $e;
|
|
|
|
$from =~ s/^'//;
|
|
|
|
$from =~ s/'$//;
|
|
|
|
$from = quotemeta $from;
|
|
|
|
$from =~ s/\\ / /g;
|
|
|
|
$subcode = $r;
|
|
|
|
$subcode =~ s/\s*with\s*//i;
|
|
|
|
} else {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "Unbalanced single quotes. Usage: cc replace 'from' with 'to' [and ...]\n";
|
2015-06-17 06:51:09 +02:00
|
|
|
exit 0;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
($e, $r) = extract_delimited($subcode, "'");
|
|
|
|
|
|
|
|
if(defined $e) {
|
|
|
|
$to = $e;
|
|
|
|
$to =~ s/^'//;
|
|
|
|
$to =~ s/'$//;
|
|
|
|
$subcode = $r;
|
|
|
|
} else {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "Unbalanced single quotes. Usage: cc replace 'from' with 'to' [and replace ... with ... [and ...]]\n";
|
2015-06-17 06:51:09 +02:00
|
|
|
exit 0;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
given($modifier) {
|
|
|
|
when($_ eq 'all' ) {}
|
|
|
|
when($_ eq 'last' ) {}
|
|
|
|
when($_ eq 'first' ) { $modifier = 1; }
|
|
|
|
when($_ eq 'second' ) { $modifier = 2; }
|
|
|
|
when($_ eq 'third' ) { $modifier = 3; }
|
|
|
|
when($_ eq 'fourth' ) { $modifier = 4; }
|
|
|
|
when($_ eq 'fifth' ) { $modifier = 5; }
|
|
|
|
when($_ eq 'sixth' ) { $modifier = 6; }
|
|
|
|
when($_ eq 'seventh') { $modifier = 7; }
|
|
|
|
when($_ eq 'eighth' ) { $modifier = 8; }
|
|
|
|
when($_ eq 'nineth' ) { $modifier = 9; }
|
|
|
|
when($_ eq 'tenth' ) { $modifier = 10; }
|
2017-09-11 04:53:29 +02:00
|
|
|
default { print "Bad replacement modifier '$modifier'; valid modifiers are 'all', 'first', 'second', ..., 'tenth', 'last'\n"; exit 0; }
|
2015-06-17 06:51:09 +02:00
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
my $replacement = {};
|
|
|
|
$replacement->{'from'} = $from;
|
|
|
|
$replacement->{'to'} = $to;
|
|
|
|
$replacement->{'modifier'} = $modifier;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
push @replacements, $replacement;
|
|
|
|
next;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if($subcode =~ m/^\s*(and)?\s*s\/.*\//) {
|
|
|
|
$last_keyword = undef;
|
|
|
|
$got_sub = 1;
|
|
|
|
$subcode =~ s/^\s*(and)?\s*s//;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
my ($regex, $to);
|
|
|
|
my ($e, $r) = extract_delimited($subcode, '/');
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if(defined $e) {
|
|
|
|
$regex = $e;
|
|
|
|
$regex =~ s/^\///;
|
|
|
|
$regex =~ s/\/$//;
|
|
|
|
$subcode = "/$r";
|
|
|
|
} else {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "Unbalanced slashes. Usage: cc s/regex/substitution/[gi] [and s/.../.../ [and ...]]\n";
|
2015-06-17 06:51:09 +02:00
|
|
|
exit 0;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
($e, $r) = extract_delimited($subcode, '/');
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if(defined $e) {
|
|
|
|
$to = $e;
|
|
|
|
$to =~ s/^\///;
|
|
|
|
$to =~ s/\/$//;
|
|
|
|
$subcode = $r;
|
|
|
|
} else {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "Unbalanced slashes. Usage: cc s/regex/substitution/[gi] [and s/.../.../ [and ...]]\n";
|
2015-06-17 06:51:09 +02:00
|
|
|
exit 0;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
my $suffix;
|
|
|
|
$suffix = $1 if $subcode =~ s/^([^ ]+)//;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if(length $suffix and $suffix =~ m/[^gi]/) {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "Bad regex modifier '$suffix'. Only 'i' and 'g' are allowed.\n";
|
2015-06-17 06:51:09 +02:00
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
if(defined $prevchange) {
|
|
|
|
$code = $prevchange;
|
|
|
|
} else {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "No recent code to change.\n";
|
2015-06-17 06:51:09 +02:00
|
|
|
exit 0;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
my $ret = eval {
|
|
|
|
my ($ret, $a, $b, $c, $d, $e, $f, $g, $h, $i, $before, $after);
|
|
|
|
|
|
|
|
if(not length $suffix) {
|
|
|
|
$ret = $code =~ s|$regex|$to|;
|
|
|
|
($a, $b, $c, $d, $e, $f, $g, $h, $i) = ($1, $2, $3, $4, $5, $6, $7, $8, $9);
|
|
|
|
$before = $`;
|
|
|
|
$after = $';
|
|
|
|
} elsif($suffix =~ /^i$/) {
|
2019-06-26 18:34:19 +02:00
|
|
|
$ret = $code =~ s|$regex|$to|i;
|
2015-06-17 06:51:09 +02:00
|
|
|
($a, $b, $c, $d, $e, $f, $g, $h, $i) = ($1, $2, $3, $4, $5, $6, $7, $8, $9);
|
|
|
|
$before = $`;
|
|
|
|
$after = $';
|
|
|
|
} elsif($suffix =~ /^g$/) {
|
|
|
|
$ret = $code =~ s|$regex|$to|g;
|
|
|
|
($a, $b, $c, $d, $e, $f, $g, $h, $i) = ($1, $2, $3, $4, $5, $6, $7, $8, $9);
|
|
|
|
$before = $`;
|
|
|
|
$after = $';
|
|
|
|
} elsif($suffix =~ /^ig$/ or $suffix =~ /^gi$/) {
|
|
|
|
$ret = $code =~ s|$regex|$to|gi;
|
|
|
|
($a, $b, $c, $d, $e, $f, $g, $h, $i) = ($1, $2, $3, $4, $5, $6, $7, $8, $9);
|
|
|
|
$before = $`;
|
|
|
|
$after = $';
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if($ret) {
|
2015-06-17 06:51:09 +02:00
|
|
|
$code =~ s/\$1/$a/g;
|
|
|
|
$code =~ s/\$2/$b/g;
|
|
|
|
$code =~ s/\$3/$c/g;
|
|
|
|
$code =~ s/\$4/$d/g;
|
|
|
|
$code =~ s/\$5/$e/g;
|
|
|
|
$code =~ s/\$6/$f/g;
|
|
|
|
$code =~ s/\$7/$g/g;
|
|
|
|
$code =~ s/\$8/$h/g;
|
|
|
|
$code =~ s/\$9/$i/g;
|
|
|
|
$code =~ s/\$`/$before/g;
|
|
|
|
$code =~ s/\$'/$after/g;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
return $ret;
|
|
|
|
};
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if($@) {
|
|
|
|
my $error = $@;
|
|
|
|
$error =~ s/ at .* line \d+\.\s*$//;
|
2017-09-11 04:53:29 +02:00
|
|
|
print "$error\n";
|
2015-01-15 06:51:17 +01:00
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if ($ret) {
|
|
|
|
$got_changes = 1;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
$prevchange = $code;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if ($got_sub and not $got_changes) {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "No substitutions made.\n";
|
2015-06-17 06:51:09 +02:00
|
|
|
exit 0;
|
|
|
|
} elsif ($got_sub and $got_changes) {
|
|
|
|
next;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
last;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if (@replacements) {
|
|
|
|
use re::engine::RE2 -strict => 1;
|
|
|
|
@replacements = sort { $a->{'from'} cmp $b->{'from'} or $a->{'modifier'} <=> $b->{'modifier'} } @replacements;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
my ($previous_from, $previous_modifier);
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
foreach my $replacement (@replacements) {
|
|
|
|
my $from = $replacement->{'from'};
|
|
|
|
my $to = $replacement->{'to'};
|
|
|
|
my $modifier = $replacement->{'modifier'};
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if(defined $previous_from) {
|
|
|
|
if($previous_from eq $from and $previous_modifier =~ /^\d+$/) {
|
|
|
|
$modifier -= $modifier - $previous_modifier;
|
|
|
|
}
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if(defined $prevchange) {
|
|
|
|
$code = $prevchange;
|
|
|
|
} else {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "No recent code to change.\n";
|
2015-06-17 06:51:09 +02:00
|
|
|
exit 0;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
my $ret = eval {
|
|
|
|
my $got_change;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
my ($first_char, $last_char, $first_bound, $last_bound);
|
|
|
|
$first_char = $1 if $from =~ m/^(.)/;
|
|
|
|
$last_char = $1 if $from =~ m/(.)$/;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if($first_char =~ /\W/) {
|
|
|
|
$first_bound = '.?';
|
|
|
|
} else {
|
|
|
|
$first_bound = '\b';
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if($last_char =~ /\W/) {
|
|
|
|
$last_bound = '.?';
|
|
|
|
} else {
|
|
|
|
$last_bound = '\b';
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if($modifier eq 'all') {
|
|
|
|
if($code =~ s/($first_bound)$from($last_bound)/$1$to$2/g) {
|
|
|
|
$got_change = 1;
|
|
|
|
}
|
|
|
|
} elsif($modifier eq 'last') {
|
|
|
|
if($code =~ s/(.*)($first_bound)$from($last_bound)/$1$2$to$3/) {
|
|
|
|
$got_change = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
my $count = 0;
|
|
|
|
my $unescaped = $from;
|
|
|
|
$unescaped =~ s/\\//g;
|
|
|
|
if($code =~ s/($first_bound)$from($last_bound)/if(++$count == $modifier) { "$1$to$2"; } else { "$1$unescaped$2"; }/ge) {
|
|
|
|
$got_change = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $got_change;
|
|
|
|
};
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if($@) {
|
|
|
|
my $error = $@;
|
|
|
|
$error =~ s/ at .* line \d+\.\s*$//;
|
2017-09-11 04:53:29 +02:00
|
|
|
print "$error\n";
|
2015-01-15 06:51:17 +01:00
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if($ret) {
|
|
|
|
$got_sub = 1;
|
|
|
|
$got_changes = 1;
|
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
$prevchange = $code;
|
|
|
|
$previous_from = $from;
|
|
|
|
$previous_modifier = $modifier;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if(not $got_changes) {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "No replacements made.\n";
|
2015-06-17 06:51:09 +02:00
|
|
|
exit 0;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
2015-06-17 06:51:09 +02:00
|
|
|
}
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
unless($got_undo and not $got_changes) {
|
|
|
|
$unshift_last_code = 1 unless $copy_code and not $got_changes;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($copy_code and $got_changes) {
|
|
|
|
$self->{only_show} = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($got_undo and not $got_changes) {
|
|
|
|
$self->{only_show} = 1;
|
2015-01-15 06:51:17 +01:00
|
|
|
}
|
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
unless (($self->{got_run} or $got_diff) and not $got_changes) {
|
2015-01-15 06:51:17 +01:00
|
|
|
if($unshift_last_code) {
|
|
|
|
unshift @last_code, $code;
|
|
|
|
}
|
|
|
|
|
|
|
|
open FILE, "> history/$self->{channel}-$self->{lang}.hist";
|
|
|
|
|
|
|
|
my $i = 0;
|
|
|
|
foreach my $line (@last_code) {
|
|
|
|
last if(++$i > $self->{max_history});
|
|
|
|
print FILE "$line\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
close FILE;
|
|
|
|
}
|
|
|
|
|
2015-06-17 06:51:09 +02:00
|
|
|
if ($got_diff) {
|
|
|
|
if($#last_code < 1) {
|
2017-09-11 04:53:29 +02:00
|
|
|
print "Not enough recent code to diff.\n"
|
2015-06-17 06:51:09 +02:00
|
|
|
} else {
|
|
|
|
use Text::WordDiff;
|
|
|
|
my $diff = word_diff(\$last_code[1], \$last_code[0], { STYLE => 'Diff' });
|
|
|
|
|
|
|
|
if($diff !~ /(?:<del>|<ins>)/) {
|
|
|
|
$diff = "No difference.";
|
|
|
|
} else {
|
|
|
|
$diff =~ s/<del>(.*?)(\s+)<\/del>/<del>$1<\/del>$2/g;
|
|
|
|
$diff =~ s/<ins>(.*?)(\s+)<\/ins>/<ins>$1<\/ins>$2/g;
|
|
|
|
$diff =~ s/<del>((?:(?!<del>).)*)<\/del>\s*<ins>((?:(?!<ins>).)*)<\/ins>/`replaced $1 with $2`/g;
|
|
|
|
$diff =~ s/<del>(.*?)<\/del>/`removed $1`/g;
|
|
|
|
$diff =~ s/<ins>(.*?)<\/ins>/`inserted $1`/g;
|
|
|
|
}
|
|
|
|
|
2017-09-11 04:53:29 +02:00
|
|
|
print "$diff\n";
|
2015-06-17 06:51:09 +02:00
|
|
|
}
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
2015-01-15 06:51:17 +01:00
|
|
|
$self->{code} = $code;
|
|
|
|
}
|
|
|
|
|
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,
|
2019-06-25 04:01:38 +02:00
|
|
|
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 = '';
|
2019-07-09 00:51:55 +02:00
|
|
|
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;
|
2020-10-23 09:46:23 +02:00
|
|
|
my $pos = 0;
|
2019-06-13 06:35:04 +02:00
|
|
|
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 = ' ';
|
2019-07-09 00:51:55 +02:00
|
|
|
$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 ' ';
|
|
|
|
|
|
|
|
if ($escaped) {
|
2019-06-25 04:01:38 +02:00
|
|
|
if ($opts{preserve_escapes}) {
|
|
|
|
$token .= "\\$ch";
|
|
|
|
} else {
|
|
|
|
$token .= $ch;
|
|
|
|
}
|
2019-06-13 06:35:04 +02:00
|
|
|
$escaped = 0;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-07-09 00:51:55 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$token .= $ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
return @args;
|
|
|
|
}
|
|
|
|
|
2015-01-15 06:51:17 +01:00
|
|
|
1;
|