From: Nick Bowler Date: Sat, 20 Apr 2019 00:22:32 +0000 (-0400) Subject: MPD script updates. X-Git-Url: https://git.draconx.ca/gitweb/fvwmconf.git/commitdiff_plain/9e3e0b6e4941da4fd9329dc0d1567bbbbbf58988 MPD script updates. 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. --- diff --git a/COPYING.WTFPL-2 b/COPYING.WTFPL-2 new file mode 100644 index 0000000..cb33446 --- /dev/null +++ b/COPYING.WTFPL-2 @@ -0,0 +1,14 @@ + 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. + diff --git a/scripts/mpdexec.pl b/scripts/mpdexec.pl index 9c6e069..962656f 100755 --- a/scripts/mpdexec.pl +++ b/scripts/mpdexec.pl @@ -1,9 +1,10 @@ #!/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. @@ -12,7 +13,12 @@ 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"; @@ -29,11 +35,37 @@ if (!(<$sock> =~ /^OK MPD ([0-9]+)\.([0-9]+)\.([0-9]+)$/)) { 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"; diff --git a/scripts/mpdmenu.pl b/scripts/mpdmenu.pl index d477ca3..c36fad7 100755 --- a/scripts/mpdmenu.pl +++ b/scripts/mpdmenu.pl @@ -1,4 +1,13 @@ #!/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; @@ -12,7 +21,8 @@ use constant { }; use utf8; -use encoding 'utf8'; +use open qw(:std :utf8); +binmode(STDOUT, ":utf8"); use Encode; sub cmd @@ -49,7 +59,8 @@ $title = decode_utf8($title) if defined($title);; 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");