OSDN Git Service

axfer: apply refactoring in list subcommand for new command system
authorTakashi Sakamoto <o-takashi@sakamocchi.jp>
Mon, 3 Dec 2018 21:33:42 +0000 (06:33 +0900)
committerTakashi Iwai <tiwai@suse.de>
Wed, 5 Dec 2018 14:22:15 +0000 (15:22 +0100)
This commit splits option parser for new command system into a function
for readability.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
axfer/subcmd-list.c

index 0876e07..f8f0952 100644 (file)
@@ -225,30 +225,37 @@ static bool decide_operation(int argc, char *const *argv, enum list_op *op)
        return false;
 }
 
-int subcmd_list(int argc, char *const *argv, snd_pcm_stream_t direction)
+static int detect_operation(int argc, char *const *argv, enum list_op *op)
 {
-       static const struct {
-               const char *const category;
-               int (*func)(snd_pcm_stream_t direction);
-       } ops[] = {
-               {"device",      list_devices},
-               {"pcm",         list_pcms},
+       static const char *const ops[] = {
+               [LIST_OP_DEVICE] = "device",
+               [LIST_OP_PCM] = "pcm",
        };
-       enum list_op op;
        int i;
-       int err;
 
-       // Renewed command system.
-       if (argc > 2 && !strcmp(argv[1], "list")) {
-               for (i = 0; i < ARRAY_SIZE(ops); ++i) {
-                       if (!strcmp(ops[i].category, argv[2]))
-                               return ops[i].func(direction);
+       if (strcmp(argv[1], "list") || argc < 3)
+               return false;
+
+       for (i = 0; i < ARRAY_SIZE(ops); ++i) {
+               if (!strcmp(argv[2], ops[i])) {
+                       *op = i;
+                       return true;
                }
        }
 
-       if (!decide_operation(argc, argv, &op)) {
-               err = -EINVAL;
-               op = LIST_OP_HELP;
+       return false;
+}
+
+int subcmd_list(int argc, char *const *argv, snd_pcm_stream_t direction)
+{
+       enum list_op op = LIST_OP_HELP;
+       int err = 0;
+
+       // Renewed command system.
+       if (argc > 1) {
+               if (!detect_operation(argc, argv, &op) &&
+                   !decide_operation(argc, argv, &op))
+                               err = -EINVAL;
        }
 
        if (op == LIST_OP_DEVICE)