From 1d5f3e61ed0a967b10e928318e0809c868f2a0df Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Tue, 4 Dec 2018 06:33:42 +0900 Subject: [PATCH] axfer: use second argument in command line for transmission direction 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 Signed-off-by: Takashi Iwai --- axfer/main.c | 78 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 60 insertions(+), 18 deletions(-) diff --git a/axfer/main.c b/axfer/main.c index 655d1e0..0da3eb7 100644 --- a/axfer/main.c +++ b/axfer/main.c @@ -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);