Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
fix(cli): Append zeros in CLI option array
Browse files Browse the repository at this point in the history
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
  • Loading branch information
howjmay committed Jan 16, 2020
1 parent df53b0b commit 3af590a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 23 deletions.
3 changes: 2 additions & 1 deletion accelerator/cli_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
23 changes: 1 addition & 22 deletions accelerator/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3af590a

Please sign in to comment.