From 3af590a4e994f25f37cd6f44e6b1fb358d6c3674 Mon Sep 17 00:00:00 2001 From: YangHau Date: Thu, 16 Jan 2020 15:36:15 +0800 Subject: [PATCH] fix(cli): Append zeros in CLI option array the 4th argument of `getopt_long()` is an array of `struct option`. And this array should be ended with a `struct option` filled with zeros. Fixes #436 --- accelerator/cli_info.h | 3 ++- accelerator/config.c | 23 +---------------------- 2 files changed, 3 insertions(+), 23 deletions(-) diff --git a/accelerator/cli_info.h b/accelerator/cli_info.h index 314030c1..7a115bd0 100644 --- a/accelerator/cli_info.h +++ b/accelerator/cli_info.h @@ -72,7 +72,8 @@ static struct ta_cli_argument_s { {"cache", CACHE, "Enable cache server with Y", REQUIRED_ARG}, {"config", CONF_CLI, "Read configuration file", REQUIRED_ARG}, {"proxy_passthrough", PROXY_API, "Pass proxy API directly to IRI without processing", NO_ARG}, - {"verbose", VERBOSE, "Enable logger", NO_ARG}}; + {"verbose", VERBOSE, "Enable logger", NO_ARG}, + {NULL, 0, NULL, (ta_cli_arg_requirement_t)0}}; static const int cli_cmd_num = sizeof(ta_cli_arguments_g) / sizeof(struct ta_cli_argument_s); diff --git a/accelerator/config.c b/accelerator/config.c index b00ea7ee..f6e9631e 100644 --- a/accelerator/config.c +++ b/accelerator/config.c @@ -13,8 +13,6 @@ #define CONFIG_LOGGER "config" static logger_id_t logger_id; -// FIXME: We need further improvements without depending on temporary variables -static int tmp_argc; int get_conf_key(char const* const key) { for (int i = 0; i < cli_cmd_num; ++i) { @@ -26,19 +24,6 @@ int get_conf_key(char const* const key) { return 0; } -static void remove_nonvalue_dash(int argc, char** argv) { - for (int i = 1; i < argc; i++) { - if ((strlen(argv[i]) == 2) && (!strncmp(argv[i], "--", 2))) { - argc--; - if (argc == (i + 1)) { - break; - } - memmove(argv + i, argv + (i + 1), (argc - i) * sizeof(char*)); - } - } - tmp_argc = argc; -} - struct option* cli_build_options() { struct option* long_options = (struct option*)malloc(cli_cmd_num * sizeof(struct option)); for (int i = 0; i < cli_cmd_num; ++i) { @@ -205,9 +190,6 @@ status_t ta_core_file_init(ta_core_t* const core, int argc, char** argv) { goto done; } - // remove `--` from argv - remove_nonvalue_dash(argc, argv); - // Loop through the CLI arguments for first time to find the configuration file path while ((key = getopt_long(argc, argv, "hv", long_options, NULL)) != -1) { switch (key) { @@ -295,10 +277,7 @@ status_t ta_core_cli_init(ta_core_t* const core, int argc, char** argv) { status_t ret = SC_OK; struct option* long_options = cli_build_options(); - // remove `--` from argv - remove_nonvalue_dash(tmp_argc, argv); - - while ((key = getopt_long(tmp_argc, argv, "hv", long_options, NULL)) != -1) { + while ((key = getopt_long(argc, argv, "hv", long_options, NULL)) != -1) { switch (key) { case ':': ret = SC_CONF_MISSING_ARGUMENT;