lib

morphux C utility library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/lib

commit 3af3ff2a94de24c1708ad696340937b0bed9b488
parent 201418b22e601d2d5e3e7b0a820c05a577b8cb47
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 23 Jan 2017 20:32:13 +0100

Fix(margs): Now properly handling EOL

Diffstat:
Minc/m_args.h | 2++
Msrc/m_args.c | 11+++++------
Mtests/test_args.c | 21+++++++++++++++++++++
3 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/inc/m_args.h b/inc/m_args.h @@ -53,6 +53,8 @@ typedef struct s_args { } margs_t; #define ARGS_EOL {0, NULL, NULL, false, NULL} +#define IS_EOL(lst) (lst.opt == 0 && lst.s_opt == NULL && lst.desc == NULL && \ + lst.take_arg == false && lst.callback == NULL) u32_t read_opt(const int ac, char **av, const margs_t *args); diff --git a/src/m_args.c b/src/m_args.c @@ -70,11 +70,11 @@ u32_t read_opt(const int ac, char **av, const margs_t *args) { /* Search the option in the args array */ for (u32_t z = 1; av[i][z] != '\0'; z++) { - for (it = 0; args[it].opt != 0 && args[it].opt != av[i][z]; it++) + for (it = 0; !IS_EOL(args[it]) && args[it].opt != av[i][z]; it++) ; /* Can't find the option */ - if (args[it].opt == 0) { + if (IS_EOL(args[it])) { m_error("Unknow option -%s\n", &(av[i][z])); opt_help(args, 1); } else { @@ -122,12 +122,12 @@ u32_t read_opt(const int ac, char **av, const margs_t *args) { } /* Search the option in the args array */ - for (it = 0; args[it].opt != 0 && (args[it].s_opt != NULL && - strncmp(args[it].s_opt, &(av[i][2]), k) != 0); it++) + for (it = 0; !IS_EOL(args[it]) && + (strncmp(args[it].s_opt, &(av[i][2]), k) != 0); it++) ; /* Can't find the option */ - if (args[it].opt == 0) { + if (IS_EOL(args[it])) { m_error("Unknown option %s\n", av[i]); opt_help(args, 1); } else { @@ -135,7 +135,6 @@ u32_t read_opt(const int ac, char **av, const margs_t *args) { m_error("Option %s must take an argument", args[it].s_opt); opt_help(args, 1); } - if (got_arg) args[it].callback(&(av[i][k + 3])); else diff --git a/tests/test_args.c b/tests/test_args.c @@ -299,6 +299,7 @@ TEST(args_missing_value_1) { int st, fd[2]; pid_t pid; + reset_args(); pipe(fd); if ((pid = fork()) == 0) { DUP_ALL_OUTPUTS(fd); @@ -317,6 +318,7 @@ TEST(args_missing_value_2) { int st, fd[2]; pid_t pid; + reset_args(); pipe(fd); if ((pid = fork()) == 0) { DUP_ALL_OUTPUTS(fd); @@ -333,6 +335,7 @@ TEST(args_value_1) { margs_t opt[] = OPT_DEF(true); char *av[] = {"./test", "-q", "toto"}; + reset_args(); TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 1, "Wrong return"); TEST_ASSERT(args.opt_q == true, "Argument not read"); TEST_ASSERT(strcmp(args.str_q, "toto") == 0, "Value not read"); @@ -343,6 +346,7 @@ TEST(args_value_2) { margs_t opt[] = OPT_DEF(true); char *av[] = {"./test", "--qwerty=toto"}; + reset_args(); TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 1, "Wrong return"); TEST_ASSERT(args.opt_q == true, "Argument not read"); TEST_ASSERT(strcmp(args.str_q, "toto") == 0, "Value not read"); @@ -353,6 +357,7 @@ TEST(args_value_3) { margs_t opt[] = OPT_DEF(true); char *av[] = {"./test", "--qwerty=toto", "-w", "tata"}; + reset_args(); TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 2, "Wrong return"); TEST_ASSERT(args.opt_q == true, "Argument not read"); TEST_ASSERT(strcmp(args.str_q, "toto") == 0, "Value not read"); @@ -365,6 +370,7 @@ TEST(args_value_4) { margs_t opt[] = OPT_DEF(true); char *av[] = {"./test", "--qwerty=toto", "--wertyu=tata"}; + reset_args(); TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 2, "Wrong return"); TEST_ASSERT(args.opt_q == true, "Argument not read"); TEST_ASSERT(strcmp(args.str_q, "toto") == 0, "Value not read"); @@ -373,6 +379,20 @@ TEST(args_value_4) { return TEST_SUCCESS; } +TEST(args_word_only_1) { + margs_t opt[] = { + {0, "qwerty", "qwerty", false, &callback_q}, + ARGS_EOL + }; + char *av[] = {"./test", "--qwerty"}; + + reset_args(); + TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 1, "Wrong return"); + TEST_ASSERT(args.opt_q == true, "Argument not read"); + TEST_ASSERT(args.opt_w == false, "Wrong argument"); + return TEST_SUCCESS; +} + void callback_q(const char *s) { args.opt_q = true; @@ -466,4 +486,5 @@ void register_args_tests(void) { reg_test("m_args", args_value_2); reg_test("m_args", args_value_3); reg_test("m_args", args_value_4); + reg_test("m_args", args_word_only_1); }