]> git.draconx.ca Git - mpdhacks.git/blobdiff - mpdmenu.pl
Fix quoting of single quotes in MPD protocol.
[mpdhacks.git] / mpdmenu.pl
index a12eb4c39350e6948ed8fd75fd37058d0aef854c..3a84f7855bcda3ddfb60c92d41f06657b2bb45f6 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/perl
 #
-# Copyright © 2008,2010,2012,2019 Nick Bowler
+# Copyright © 2008,2010,2012,2020 Nick Bowler
 #
 # Silly little script to generate an FVWM menu with various bits of MPD
 # status information and controls.
@@ -24,6 +24,9 @@ use Scalar::Util qw(reftype);
 use List::Util qw(any max);
 use FindBin;
 
+use lib "$FindBin::Bin";
+use MPDHacks;
+
 use constant {
        MPD_MJR_MIN => 0,
        MPD_MNR_MIN => 21,
@@ -42,29 +45,10 @@ my ($topmenu, $menu);
 my $mode = "top";
 my %artistids;
 
-# Quotes the argument so that it is presented as a single argument to MPD
-# at the protocol level.  This also works OK for most FVWM arguments.
-sub escape {
-       my $s = @_[0] // $_;
-
-       # No way to encode literal newlines in the protocol, so we
-       # convert any newlines in the arguments into a space, which
-       # can help with quoting.
-       $s =~ s/\n/ /g;
-
-       if (/[ \t\\"]/) {
-               $s =~ s/[\\"]/\\$&/g;
-               return "\"$s\"";
-       }
-
-       $s =~ s/^\s*$/"$&"/;
-       return $s;
-}
-
 # Submit a command to the MPD server; each argument to this function
 # is quoted and sent as a single argument to MPD.
 sub mpd_exec {
-       my $cmd = join(' ', map { escape } @_);
+       my $cmd = join(' ', map { MPD::escape } @_);
 
        print $sock "$cmd\n";
 }
@@ -74,7 +58,7 @@ sub fvwm_cmd_unquoted {
 }
 
 sub fvwm_cmd {
-       fvwm_cmd_unquoted(map { escape } @_);
+       fvwm_cmd_unquoted(map { MPD::escape } @_);
 }
 
 # Quotes the argument in such a way that it is passed unadulterated by
@@ -243,8 +227,8 @@ sub top_track_cover {
                my $cover = mpd_cover_filename($file);
 
                $cover = fvwm_shell_literal($cover // $file);
-               fvwm_cmd_unquoted("AddToMenu", escape($menu),
-                                 escape($entry->{thumb}),
+               fvwm_cmd_unquoted("AddToMenu", MPD::escape($menu),
+                                 MPD::escape($entry->{thumb}),
                                  "Exec", "exec", "geeqie", $cover);
        }
 }