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

Commit

Permalink
fix: Fix error as CLI argument without giving key
Browse files Browse the repository at this point in the history
If we give a CLI argument without key which would be two
dash (`--`) only, TA would open fail.

Therefore, we simply remove the dashes without assigning
keys.
  • Loading branch information
howjmay committed Jan 5, 2020
1 parent ccd7074 commit de3e972
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion accelerator/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#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 @@ -24,6 +26,19 @@ 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 @@ -190,6 +205,9 @@ 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 @@ -277,7 +295,10 @@ 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();

while ((key = getopt_long(argc, argv, "hv", long_options, NULL)) != -1) {
// remove `--` from argv
remove_nonvalue_dash(tmp_argc, argv);

while ((key = getopt_long(tmp_argc, argv, "hv", long_options, NULL)) != -1) {
switch (key) {
case ':':
ret = SC_CONF_MISSING_ARGUMENT;
Expand Down

0 comments on commit de3e972

Please sign in to comment.