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, 31 insertions(+), 9 deletions(-)

diff --git a/src/command.c b/src/command.c
index 914f87b..9940035 100644
--- a/src/command.c
+++ b/src/command.c
@@ -369,20 +369,38 @@ cmd_outputs(mpd_unused int argc, mpd_unused char **argv, 
struct mpd_connection *
 }
 
 static unsigned
-count_outputs(struct mpd_connection *conn)
+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;
 }
 
@@ -391,6 +409,7 @@ 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))
 {
+       char **names = argv, **names_end = argv;
        unsigned *ids, *ids_end, max;
        bool only = false;
        int arg;
@@ -407,7 +426,10 @@ 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. */
@@ -415,12 +437,12 @@ enable_disable(int argc, char **argv, struct 
mpd_connection *conn,
                }
        }
 
-       if (ids == ids_end) {
-               goto done;
+       if (only || names != names_end) {
+               max = match_outputs(conn, names, names_end, &ids_end);
        }
 
-       if (only) {
-               max = count_outputs(conn);
+       if (ids == ids_end) {
+               goto done;
        }
 
        if (!mpd_command_list_begin(conn, false)) {
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