3 # Copyright © 2012,2019 Nick Bowler
5 # Simple program to send a command to MPD. Each command-line argument is
6 # quoted as necessary so it appears as a single argument at the protocol
7 # level. The result is printed to standard output.
9 # License GPLv3+: GNU General Public License version 3 or any later version.
10 # This is free software: you are free to change and redistribute it.
11 # There is NO WARRANTY, to the extent permitted by law.
17 use Encode::Locale qw(decode_argv);
18 decode_argv(Encode::FB_CROAK);
20 binmode(STDOUT, ":utf8");
21 binmode(STDIN, ":utf8");
22 use IO::Socket::INET6;
24 my $host = $ENV{MPD_HOST} // "localhost";
25 my $port = $ENV{MPD_PORT} // 6600;
27 my $sock = new IO::Socket::INET6(
31 ) or die "failed to connect to MPD: $!";
32 binmode($sock, ":utf8");
34 if (!(<$sock> =~ /^OK MPD ([0-9]+)\.([0-9]+)\.([0-9]+)$/)) {
35 die "MPD failed to announce version: $!";
41 # No way to encode literal newlines in the protocol, so we convert
42 # any newlines in the arguments into a space, which can help with
54 print $sock join(' ', @_), "\n";
63 mpd_exec(map { mpd_escape($_) } @ARGV)
71 print $sock "close\n";