]> git.draconx.ca Git - fvwmconf.git/commitdiff
MPD script updates.
authorNick Bowler <nbowler@draconx.ca>
Sat, 20 Apr 2019 00:22:32 +0000 (20:22 -0400)
committerNick Bowler <nbowler@draconx.ca>
Sun, 21 Apr 2019 04:48:13 +0000 (00:48 -0400)
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.

COPYING.WTFPL-2 [new file with mode: 0644]
scripts/mpdexec.pl
scripts/mpdmenu.pl

diff --git a/COPYING.WTFPL-2 b/COPYING.WTFPL-2
new file mode 100644 (file)
index 0000000..cb33446
--- /dev/null
@@ -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.
+
index 9c6e0693d765b0d6015a5631db825cdbe9a566cb..962656f2c957aa6920ed12cda42653f82a390927 100755 (executable)
@@ -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.
 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";
index d477ca3bdfb01eb69d2bbc807ea958ef9aa0a2b0..c36fad7ab64e7fa051a1104e4cc2183ede6058ae 100755 (executable)
@@ -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");