]> git.draconx.ca Git - mpdhacks.git/blobdiff - mpdexec.pl
Fix quoting of single quotes in MPD protocol.
[mpdhacks.git] / mpdexec.pl
index 0d477dae465d0923b297fea31d2af3919508d54a..8147f1d185c96fe4aec99a2399de5d63c6a1e279 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env perl
 #
-# Copyright © 2012,2019 Nick Bowler
+# Copyright © 2012,2019-2020 Nick Bowler
 #
 # Send commands to MPD.  Each command-line argument is quoted as necessary
 # so it appears as a single argument at the protocol level.  The result is
@@ -24,6 +24,10 @@ use IO::Socket::INET6;
 
 use Getopt::Long qw(:config gnu_getopt);
 
+use FindBin;
+use lib "$FindBin::Bin";
+use MPDHacks;
+
 my $host = $ENV{MPD_HOST} // "localhost";
 my $port = $ENV{MPD_PORT} // 6600;
 my ($quiet, $binary, $ignore_errors, $download);
@@ -112,21 +116,6 @@ if (!(<$sock> =~ /^OK MPD ([0-9]+)\.([0-9]+)\.([0-9]+)$/)) {
        die "MPD failed to announce version: $!";
 }
 
-sub mpd_escape {
-       ($_) = @_;
-
-       # No way to encode literal newlines in the protocol, so we convert
-       # any newlines in the arguments into a space, which can help with
-       # shell quoting.
-       s/\n/ /g;
-
-       if (/[ \t\\"]/) {
-               s/[\\"]/\\$&/g;
-               return "\"$_\"";
-       }
-       return $_;
-}
-
 sub read_binary {
        my ($count) = @_;
        my $buf;
@@ -185,7 +174,7 @@ sub mpd_exec {
 }
 
 if (@ARGV) {
-       mpd_exec(map { mpd_escape($_) } @ARGV)
+       mpd_exec(map { MPD::escape($_) } @ARGV)
 } else {
        while (<>) {
                chomp;