]> git.alsa-project.org Git - alsa-utils.git/commitdiff
axfer: use second argument in command line for transmission direction
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:23:03 +0000 (15:23 +0100)
In renewed command system, axfer uses first argument for subcommand. In
popular subcommand such as transfer, transmission direction is required.
At present, some options in aplay(1) are used for this purpose however
it's understandable to use second argument for this purpose.

This commit uses second argument as fixed position to indicate
direction for renewed command system.

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

index 655d1e046a689ef469f509fde6a37445481a726e..0da3eb738d6fef8d14fac8b7cde71ec67fa4ca30 100644 (file)
@@ -72,12 +72,6 @@ static void print_help(void)
 
 static void decide_subcmd(int argc, char *const *argv, enum subcmds *subcmd)
 {
-       static const char *const subcmds[] = {
-               [SUBCMD_TRANSFER] = "transfer",
-               [SUBCMD_LIST] = "list",
-               [SUBCMD_HELP] = "help",
-               [SUBCMD_VERSION] = "version",
-       };
        static const struct {
                const char *const name;
                enum subcmds subcmd;
@@ -103,14 +97,6 @@ static void decide_subcmd(int argc, char *const *argv, enum subcmds *subcmd)
                return;
        }
 
-       // sub-command system.
-       for (i = 0; i < ARRAY_SIZE(subcmds); ++i) {
-               if (!strcmp(argv[1], subcmds[i])) {
-                       *subcmd = i;
-                       return;
-               }
-       }
-
        // Original command system. For long options.
        for (i = 0; i < ARRAY_SIZE(long_opts); ++i) {
                for (j = 0; j < argc; ++j) {
@@ -203,16 +189,72 @@ static bool decide_direction(int argc, char *const *argv,
        return false;
 }
 
+static bool detect_subcmd(int argc, char *const *argv, enum subcmds *subcmd)
+{
+       static const char *const subcmds[] = {
+               [SUBCMD_TRANSFER] = "transfer",
+               [SUBCMD_LIST] = "list",
+               [SUBCMD_HELP] = "help",
+               [SUBCMD_VERSION] = "version",
+       };
+       int i;
+
+       if (argc < 2)
+               return false;
+
+       for (i = 0; i < ARRAY_SIZE(subcmds); ++i) {
+               if (!strcmp(argv[1], subcmds[i])) {
+                       *subcmd = i;
+                       return true;
+               }
+       }
+
+       return false;
+}
+
+static bool detect_direction(int argc, char *const *argv,
+                            snd_pcm_stream_t *direction)
+{
+       if (argc < 3)
+               return false;
+
+       if (!strcmp(argv[2], "capture")) {
+               *direction = SND_PCM_STREAM_CAPTURE;
+               return true;
+       }
+
+       if (!strcmp(argv[2], "playback")) {
+               *direction = SND_PCM_STREAM_PLAYBACK;
+               return true;
+       }
+
+       return false;
+}
+
 int main(int argc, char *const *argv)
 {
        snd_pcm_stream_t direction;
        enum subcmds subcmd;
        int err = 0;
 
-       if (!decide_direction(argc, argv, &direction))
-               subcmd = SUBCMD_HELP;
-       else
-               decide_subcmd(argc, argv, &subcmd);
+       // For compatibility to aplay(1) implementation.
+       if (strstr(argv[0], "arecord") == argv[0] + strlen(argv[0]) - 7 ||
+           strstr(argv[0], "aplay") == argv[0] + strlen(argv[0]) - 5) {
+               if (!decide_direction(argc, argv, &direction))
+                       subcmd = SUBCMD_HELP;
+               else
+                       decide_subcmd(argc, argv, &subcmd);
+       } else {
+               // The first option should be one of subcommands.
+               if (!detect_subcmd(argc, argv, &subcmd))
+                       subcmd = SUBCMD_HELP;
+               // The second option should be either 'capture' or 'direction'
+               // if subcommand is neither 'version' nor 'help'.
+               if (subcmd != SUBCMD_VERSION && subcmd != SUBCMD_HELP) {
+                       if (!detect_direction(argc, argv, &direction))
+                               subcmd = SUBCMD_HELP;
+               }
+       }
 
        if (subcmd == SUBCMD_TRANSFER)
                err = subcmd_transfer(argc, argv, direction);