Fix compatibility with latest perl which removes the 'encoding' pragma.
And improve mpdexec.pl to be a bit more flexible: command-line arguments
will be correctly quoted for MPD, and also add a mode to accept commands
on standard input which helps performance with long command sequences.
-# Copyright © 2012 Nick Bowler
+# Copyright © 2012,2019 Nick Bowler
-# Simple program to send a command to MPD. The result is printed to standard
-# output.
+# Simple program to send a command to MPD. Each command-line argument is
+# quoted as necessary so it appears as a single argument at the protocol
+# level. The result is printed to standard output.
#
# License WTFPL2: Do What The Fuck You Want To Public License, version 2.
# This is free software: you are free to do what the fuck you want to.
#
# License WTFPL2: Do What The Fuck You Want To Public License, version 2.
# This is free software: you are free to do what the fuck you want to.
+
+use Encode::Locale qw(decode_argv);
+decode_argv(Encode::FB_CROAK);
+
+binmode(STDOUT, ":utf8");
+binmode(STDIN, ":utf8");
use IO::Socket::INET6;
my $host = $ENV{MPD_HOST} // "localhost";
use IO::Socket::INET6;
my $host = $ENV{MPD_HOST} // "localhost";
die "MPD failed to announce version: $!";
}
die "MPD failed to announce version: $!";
}
-print $sock join(' ', @ARGV), "\n";
-while (<$sock>) {
- last if (/^OK/);
- print;
- exit 1 if (/^ACK/);
+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 mpd_exec {
+ print $sock join(' ', @_), "\n";
+ while (<$sock>) {
+ last if (/^OK/);
+ print;
+ exit 1 if (/^ACK/);
+ }
+}
+
+if (@ARGV) {
+ mpd_exec(map { mpd_escape($_) } @ARGV)
+} else {
+ while (<>) {
+ chomp;
+ mpd_exec($_);
+ }
+#
+# Copyright © 2008,2010,2012,2019 Nick Bowler
+#
+# Silly little script to generate an FVWM menu with various bits of MPD
+# status information and controls.
+#
+# License WTFPL2: Do What The Fuck You Want To Public License, version 2.
+# This is free software: you are free to do what the fuck you want to.
+# There is NO WARRANTY, to the extent permitted by law.
+use open qw(:std :utf8);
+binmode(STDOUT, ":utf8");
my $sock = new IO::Socket::INET6(
PeerAddr => $host,
PeerPort => $port,
my $sock = new IO::Socket::INET6(
PeerAddr => $host,
PeerPort => $port,
+ Proto => 'tcp',
+ Timeout => 2
) or die("could not open socket: $!.\n");
binmode($sock, ":utf8");
) or die("could not open socket: $!.\n");
binmode($sock, ":utf8");