From 4287cb9fa219bdda7106ce582c216ccf37b5e525 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Tue, 29 Oct 2024 10:19:46 -0700 Subject: [PATCH] Fix split_line() quoting to be more sh-like E.g.: show testcargs [global] testcargs: /code c11 printf("/say args: " ); while (*++argv) printf("[%s] " , *argv); testcargs 'hello''world' 'how are 'you today args: [helloworld] [how are you] [today] Previously, adjacent 'foo''bar' would end up as 2 args like [foo] [bar]. Now they are properly concatenated into one [foobar] argument. This also fixes the cases of whitespace being inserted after quoted arguments. --- applets/pbot-vm/guest/lib/Languages/_default.pm | 14 +++++--------- applets/pbot-vm/host/lib/SplitLine.pm | 14 +++++--------- lib/PBot/Core/Interpreter.pm | 14 +++++--------- lib/PBot/VERSION.pm | 4 ++-- 4 files changed, 17 insertions(+), 29 deletions(-) diff --git a/applets/pbot-vm/guest/lib/Languages/_default.pm b/applets/pbot-vm/guest/lib/Languages/_default.pm index f074af88..46d9eae8 100755 --- a/applets/pbot-vm/guest/lib/Languages/_default.pm +++ b/applets/pbot-vm/guest/lib/Languages/_default.pm @@ -101,11 +101,9 @@ sub execute { return ($exitval, $stdout, $stderr); } -# 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. +# splits line into arguments separated by unquoted whitespace. +# handles unbalanced quotes by treating them as part of the +# argument they were found within. sub split_line { my ($self, $line, %opts) = @_; @@ -170,12 +168,10 @@ sub split_line { } if (defined $quote) { - if ($ch eq $quote and (not defined $next_ch or $next_ch =~ /[\s,:;})\].+=]/)) { + if ($ch eq $quote) { # closing quote $token .= $ch unless $opts{strip_quotes}; - push @args, $token; $quote = undef; - $token = ''; } else { # still within quoted argument $token .= $ch; @@ -183,7 +179,7 @@ sub split_line { next; } - if (($last_ch =~ /[\s:{(\[.+=]/) and not defined $quote and ($ch eq "'" or $ch eq '"')) { + if (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/applets/pbot-vm/host/lib/SplitLine.pm b/applets/pbot-vm/host/lib/SplitLine.pm index b2ad2d63..f866da76 100644 --- a/applets/pbot-vm/host/lib/SplitLine.pm +++ b/applets/pbot-vm/host/lib/SplitLine.pm @@ -13,11 +13,9 @@ no warnings 'experimental::signatures'; use parent qw(Exporter); our @EXPORT = qw(split_line); -# 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. +# splits line into arguments separated by unquoted whitespace. +# handles unbalanced quotes by treating them as part of the +# argument they were found within. sub split_line ($line, %opts) { my %default_opts = ( strip_quotes => 0, @@ -82,12 +80,10 @@ sub split_line ($line, %opts) { } if (defined $quote) { - if ($ch eq $quote and (not defined $next_ch or $next_ch =~ /[\s,:;})\[\].+=]/)) { + if ($ch eq $quote) { # closing quote $token .= $ch unless $opts{strip_quotes}; - push @args, $token; $quote = undef; - $token = ''; } else { # still within quoted argument $token .= $ch; @@ -95,7 +91,7 @@ sub split_line ($line, %opts) { next; } - if (($last_ch =~ /[\s:{(\[.+=]/) and not defined $quote and ($ch eq "'" or $ch eq '"')) { + if (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/lib/PBot/Core/Interpreter.pm b/lib/PBot/Core/Interpreter.pm index d504831f..3298bca5 100644 --- a/lib/PBot/Core/Interpreter.pm +++ b/lib/PBot/Core/Interpreter.pm @@ -1180,11 +1180,9 @@ sub extract_bracketed($self, $string, $open_bracket = '{', $close_bracket = '}', return ($result, $rest); } -# 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. +# splits line into arguments separated by unquoted whitespace. +# handles unbalanced quotes by treating them as part of the +# argument they were found within. sub split_line($self, $line, %opts) { my %default_opts = ( strip_quotes => 0, @@ -1252,12 +1250,10 @@ sub split_line($self, $line, %opts) { } if (defined $quote) { - if ($ch eq $quote and (not defined $next_ch or $next_ch =~ /[\s,:;})\].+=]/)) { + if ($ch eq $quote) { # closing quote $token .= $ch unless $opts{strip_quotes}; - push @args, $token; $quote = undef; - $token = ''; } else { # still within quoted argument $token .= $ch; @@ -1265,7 +1261,7 @@ sub split_line($self, $line, %opts) { next; } - if (($last_ch =~ /[\s:{(\[.+=,]/) and not defined $quote and ($ch eq "'" or $ch eq '"')) { + if (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/lib/PBot/VERSION.pm b/lib/PBot/VERSION.pm index 22c25c2e..d9ff5356 100644 --- a/lib/PBot/VERSION.pm +++ b/lib/PBot/VERSION.pm @@ -25,8 +25,8 @@ use PBot::Imports; # These are set by the /misc/update_version script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 4814, - BUILD_DATE => "2024-10-27", + BUILD_REVISION => 4815, + BUILD_DATE => "2024-10-29", }; sub initialize {}