]> git.proxmox.com Git - pve-common.git/blobdiff - data/PVE/Tools.pm
add split_args helper function
[pve-common.git] / data / PVE / Tools.pm
index 4c4e25958ad38336ae44b05cce672d9bbbc76f22..5cd8460db707bbef381a02611afa188ba982a6a0 100644 (file)
@@ -687,4 +687,52 @@ sub random_ether_addr {
     return $mac;
 }
 
+sub shellquote {
+    my $str = shift;
+
+    return "''" if !defined ($str) || ($str eq '');
+    
+    die "unable to quote string containing null (\\000) bytes"
+       if $str =~ m/\x00/;
+
+    # from String::ShellQuote
+    if ($str =~ m|[^\w!%+,\-./:@^]|) {
+
+       # ' -> '\''
+       $str =~ s/'/'\\''/g;
+
+       $str = "'$str'";
+       $str =~ s/^''//;
+       $str =~ s/''$//;
+    }
+
+    return $str;
+}
+
+# a clumsy way to split an shell argument string into an array,
+# we simply pass it to the cli (exec call)
+# fixme: use Text::ParseWords::shellwords() ?
+sub split_args {
+    my ($str) = @_;
+
+    my $args = [];
+
+    return $args if !$str;
+
+    my $cmd = 'perl -e \'foreach my $a (@ARGV) { print "$a\n"; } \' -- ' . $str;
+
+    eval {
+       run_command($cmd, outfunc => sub {
+           my $data = shift;
+           push @$args, $data;
+       });
+    };
+
+    my $err = $@;
+
+    die "unable to parse args: $str\n" if $err;
+
+    return $args;
+}
+
 1;