From 4176849e37c08892667b3d1d150a9bcbd6558241 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Mon, 24 Jun 2019 19:01:38 -0700 Subject: [PATCH] compiler_vm: improve escaping of quotes --- modules/compiler_vm/languages/_default.pm | 17 +++++++++++------ modules/compiler_vm/languages/server/_c_base.pm | 4 ++-- .../compiler_vm/languages/server/_default.pm | 15 ++++++++++----- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/modules/compiler_vm/languages/_default.pm b/modules/compiler_vm/languages/_default.pm index dc464b85..4c339ab8 100755 --- a/modules/compiler_vm/languages/_default.pm +++ b/modules/compiler_vm/languages/_default.pm @@ -443,10 +443,10 @@ sub add_option { sub process_standard_options { my $self = shift; - my @opt_args = $self->split_line($self->{code}); + my @opt_args = $self->split_line($self->{code}, preserve_escapes => 0); use Data::Dumper; - print STDERR Dumper \@opt_args; + print STDERR "opt_arg: ", Dumper \@opt_args; my $getopt_error; local $SIG{__WARN__} = sub { @@ -463,7 +463,7 @@ sub process_standard_options { print STDERR "getopt: ret: [$ret]: rest: [$rest], info: $info, input: $input, args: $arguments, paste: $paste\n"; - print STDERR Dumper @opt_args; + # print STDERR Dumper \@opt_args; if ($info) { my $cmdline = $self->{cmdline}; @@ -1026,7 +1026,8 @@ sub split_line { my %default_opts = ( strip_quotes => 0, - keep_spaces => 0 + keep_spaces => 0, + preserve_escapes => 1, ); %opts = (%default_opts, %opts); @@ -1067,7 +1068,11 @@ sub split_line { $spaces = 0 if $ch ne ' '; if ($escaped) { - $token .= "\\$ch"; + if ($opts{preserve_escapes}) { + $token .= "\\$ch"; + } else { + $token .= $ch; + } $escaped = 0; next; } @@ -1091,7 +1096,7 @@ sub split_line { next; } - if ($last_ch eq ' ' and not defined $quote and ($ch eq "'" or $ch eq '"')) { + if (($last_ch eq ' ' or $last_ch eq ':' or $last_ch eq '(') and not defined $quote and ($ch eq "'" or $ch eq '"')) { if ($ignore_quote) { # treat unbalanced quote as part of this argument $token .= $ch; diff --git a/modules/compiler_vm/languages/server/_c_base.pm b/modules/compiler_vm/languages/server/_c_base.pm index 74672005..6e84f3d9 100755 --- a/modules/compiler_vm/languages/server/_c_base.pm +++ b/modules/compiler_vm/languages/server/_c_base.pm @@ -19,7 +19,7 @@ sub preprocess { $self->execute(10, undef, 'date', '-s', "\@$self->{date}"); - my @cmd = $self->split_line($self->{cmdline}, strip_quotes => 1); + my @cmd = $self->split_line($self->{cmdline}, strip_quotes => 1, preserve_escapes => 0); if ($self->{code} =~ m/print_last_statement\(.*\);$/m) { # remove print_last_statement wrapper in order to get warnings/errors from last statement line @@ -75,7 +75,7 @@ sub postprocess { } print "Executing gdb\n"; - my @args = $self->split_line($self->{arguments}, strip_quotes => 1); + my @args = $self->split_line($self->{arguments}, strip_quotes => 1, preserve_escapes => 0); my ($exitval, $stdout, $stderr) = $self->execute(60, undef, 'compiler_watchdog.pl', @args); my $result = $stderr; diff --git a/modules/compiler_vm/languages/server/_default.pm b/modules/compiler_vm/languages/server/_default.pm index 4292190e..f57df31f 100755 --- a/modules/compiler_vm/languages/server/_default.pm +++ b/modules/compiler_vm/languages/server/_default.pm @@ -44,8 +44,8 @@ sub preprocess { $self->execute(10, undef, 'date', '-s', "\@$self->{date}"); print "Executing [$self->{cmdline}] with args [$self->{arguments}]\n"; - my @cmdline = $self->split_line($self->{cmdline}, strip_quotes => 1); - push @cmdline, $self->split_line($self->{arguments}, strip_quotes => 1); + 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); my ($retval, $stdout, $stderr) = $self->execute(60, $self->{input}, @cmdline); $self->{output} = $stderr; @@ -89,7 +89,8 @@ sub split_line { my %default_opts = ( strip_quotes => 0, - keep_spaces => 0 + keep_spaces => 0, + preserve_escapes => 1, ); %opts = (%default_opts, %opts); @@ -130,7 +131,11 @@ sub split_line { $spaces = 0 if $ch ne ' '; if ($escaped) { - $token .= "\\$ch"; + if ($opts{preserve_escapes}) { + $token .= "\\$ch"; + } else { + $token .= $ch; + } $escaped = 0; next; } @@ -154,7 +159,7 @@ sub split_line { next; } - if ($last_ch eq ' ' and not defined $quote and ($ch eq "'" or $ch eq '"')) { + if (($last_ch eq ' ' or $last_ch eq ':' or $last_ch eq '(') and not defined $quote and ($ch eq "'" or $ch eq '"')) { if ($ignore_quote) { # treat unbalanced quote as part of this argument $token .= $ch;