From: Michal Nazarewicz <min...@mina86.com>

This commit changes enable and disable commands so that they accept
not only output number but also its name.  This way, one does not
need to remember the number of the output but instead use its name.
It also makes user command work even if number and order of outputs
in configuration file changes (as long as names stay the same).
---
 src/command.c |   36 ++++++++++++++++++++++++++++++------
 src/main.c    |    4 ++--
 2 files changed, 32 insertions(+), 8 deletions(-)

diff --git a/src/command.c b/src/command.c
index 57bd613..06ce27e 100644
--- a/src/command.c
+++ b/src/command.c
@@ -368,20 +368,37 @@ cmd_outputs(mpd_unused int argc, mpd_unused char **argv, 
struct mpd_connection *
        return( 0 );
 }
 
-static unsigned count_outputs(struct mpd_connection *conn)
+static unsigned match_outputs(struct mpd_connection *conn,
+                             char **names, char **names_end, unsigned 
**ids_end)
 {
        struct mpd_output *output;
-       unsigned max = 0;
+       unsigned max = 0, *id = *ids_end;
 
        mpd_send_outputs(conn);
 
        while ((output = mpd_recv_output(conn)) != NULL) {
+               const char *name = mpd_output_get_name(output);
                max = mpd_output_get_id(output);
+
+               for (char **n = names; n != names_end; ++n) {
+                       if (!strcmp(*n, name)) {
+                               *id = max;
+                               ++id;
+                               *n = *names;
+                               ++names;
+                               break;
+                       }
+               }
+
                mpd_output_free(output);
        }
-
        mpd_response_finish(conn);
 
+       for (char **n = names; n != names_end; ++n) {
+               fprintf(stderr, "%s: no such output\n", *n);
+       }
+
+       *ids_end = id;
        return max;
 }
 
@@ -389,7 +406,8 @@ static int enable_disable(int argc, char **argv, struct 
mpd_connection *conn,
                          bool (*matched)(struct mpd_connection *conn, unsigned 
id),
                          bool (*not_matched)(struct mpd_connection *conn, 
unsigned id))
 {
-       unsigned *ids, *ids_end;
+       char **names = argv, **names_end = argv;
+       unsigned *ids, *ids_end, max;
        bool only = false;
        int arg;
 
@@ -405,7 +423,10 @@ static int enable_disable(int argc, char **argv, struct 
mpd_connection *conn,
        ids_end = ids;
 
        for (int i = argc; i; --i, ++argv) {
-               if (!parse_int(*argv, &arg) || arg <= 0) {
+               if (!parse_int(*argv, &arg)) {
+                       *names_end = *argv;
+                       ++names_end;
+               } else if (arg < 0) {
                        fprintf(stderr, "%s: not a positive integer\n", *argv);
                } else {
                        /* We decrement by 1 to make it natural to the user. */
@@ -413,10 +434,13 @@ static int enable_disable(int argc, char **argv, struct 
mpd_connection *conn,
                }
        }
 
+       if (only || names != names_end) {
+               max = match_outputs(conn, names, names_end, &ids_end);
+       }
+
        if (ids == ids_end) {
                /* nop */
        } else if (only) {
-               unsigned max = count_outputs(conn);
                for (unsigned i = 0; i <= max; ++i) {
                        bool found = false;
                        for (unsigned *id = ids;
diff --git a/src/main.c b/src/main.c
index f8d6d73..35f319f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -73,8 +73,8 @@ static struct command {
        {"seek",        1,   1,   0,    cmd_seek,        
"[+-][HH:MM:SS]|<0-100>%", "Seeks to the specified position"},
        {"clear",       0,   0,   0,    cmd_clear,       "", "Clear the current 
playlist"},
        {"outputs",     0,   0,   0,    cmd_outputs,     "", "Show the current 
outputs"},
-       {"enable",      1,   -1,  0,    cmd_enable,      "[only] <output #> 
[...]", "Enable output(s)"},
-       {"disable",     1,   -1,  0,    cmd_disable,     "[only] <output #> 
[...]", "Disable output(s)"},
+       {"enable",      1,   -1,  0,    cmd_enable,      "[only] <output # or 
name> [...]", "Enable output(s)"},
+       {"disable",     1,   -1,  0,    cmd_disable,     "[only] <output # or 
name> [...]", "Disable output(s)"},
        {"shuffle",     0,   0,   0,    cmd_shuffle,     "", "Shuffle the 
current playlist"},
        {"move",        2,   2,   0,    cmd_move,        "<from> <to>", "Move 
song in playlist"},
        {"playlist",    0,   0,   0,    cmd_playlist,    "", "Print the current 
playlist"},
-- 
1.7.7.3


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Musicpd-dev-team mailing list
Musicpd-dev-team@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/musicpd-dev-team

Reply via email to