#!/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
binmode(STDOUT, ":utf8");
binmode(STDIN, ":utf8");
-use IO::Socket::INET6;
use Getopt::Long qw(:config gnu_getopt);
-my $host = $ENV{MPD_HOST} // "localhost";
-my $port = $ENV{MPD_PORT} // 6600;
+use FindBin;
+use lib "$FindBin::Bin";
+use MPDHacks;
+
my ($quiet, $binary, $ignore_errors, $download);
sub print_version {
}
sub print_usage {
- my $fh = $_[1] // *STDERR;
+ my ($fh) = (@_, *STDERR);
print $fh "Usage: $0 [options] [command ...]\n";
- print "Try $0 --help for more information.\n" unless (@_ > 0);
+ print $fh "Try $0 --help for more information.\n" unless (@_ > 0);
}
sub print_help {
}
GetOptions(
- 'host|h=s' => \$host,
- 'port|p=s' => \$port,
+ 'host|h=s' => \$MPD::host,
+ 'port|p=s' => \$MPD::port,
'quiet|q' => \$quiet,
'no-quiet' => sub { $quiet = 0; },
}
$quiet = 1 if (defined($binary) && $binary eq "");
-my $sock = new IO::Socket::INET6(
- PeerAddr => $host,
- PeerPort => $port,
- Proto => 'tcp',
-) or die "failed to connect to MPD: $!";
-#binmode($sock, ":utf8");
-binmode($sock);
-
-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 $_;
-}
+my $sock = MPD::connect(binmode => ":raw");
sub read_binary {
my ($count) = @_;
}
if (@ARGV) {
- mpd_exec(map { mpd_escape($_) } @ARGV)
+ mpd_exec(map { MPD::escape($_) } @ARGV)
} else {
while (<>) {
chomp;