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.
--- /dev/null
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+ Version 2, December 2004
+
+ Copyright (C) 2004 Sam Hocevar
+ 22 rue de Plaisance, 75014 Paris, France
+ Everyone is permitted to copy and distribute verbatim or modified
+ copies of this license document, and changing it is allowed as long
+ as the name is changed.
+
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
+
#!/usr/bin/env perl
#
-# 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.
use strict;
use utf8;
-use encoding 'utf8';
+
+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";
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($_);
+ }
}
print $sock "close\n";
#!/usr/bin/perl
+#
+# Copyright © 2018,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 strict;
};
use utf8;
-use encoding 'utf8';
+use open qw(:std :utf8);
+binmode(STDOUT, ":utf8");
use Encode;
sub cmd
my $sock = new IO::Socket::INET6(
PeerAddr => $host,
PeerPort => $port,
- Proto => 'tcp'
+ Proto => 'tcp',
+ Timeout => 2
) or die("could not open socket: $!.\n");
binmode($sock, ":utf8");