From 11bcc43a2c9e2787c861debda6b94e0550065402 Mon Sep 17 00:00:00 2001 From: gramanas Date: Wed, 14 Nov 2018 20:39:19 +0200 Subject: Simplify cli parser --- src/actionparser.c | 73 ++++++++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 40 deletions(-) (limited to 'src/actionparser.c') diff --git a/src/actionparser.c b/src/actionparser.c index 9ca2867..398ba5e 100644 --- a/src/actionparser.c +++ b/src/actionparser.c @@ -175,7 +175,29 @@ int parse_RESTORE(UserOpt *opt) { return 0; } +void determine_action(UserOpt *opt) { + /* get action */ + if (!next_token()) { + opt->action = CK_WRONG_ACTION; + return; + } + + char actionName[STR_S] = ""; + opt->action = parser_get_action(token, actionName); + if (opt->action != CK_WRONG_ACTION) { + LOG("Action to perform: %s", actionName); + } + return; +} + int parse_vals(UserOpt *opt) { + /* find the action */ + determine_action(opt); + if (opt->action == CK_WRONG_ACTION) { + opt->err = PERR_UNKNOWN_ACTION; + return -1; + } + switch (opt->action) { #define X(ACTION) \ case CKA_##ACTION: \ @@ -203,21 +225,6 @@ CkAction parser_get_action(const char *name, char *actionName) { return CK_WRONG_ACTION; } -void determine_action(UserOpt *opt) { - /* get action */ - if (!next_token()) { - opt->action = CK_WRONG_ACTION; - return; - } - - char actionName[STR_S] = ""; - opt->action = parser_get_action(token, actionName); - if (opt->action != CK_WRONG_ACTION) { - LOG("Action to perform: %s", actionName); - } - return; -} - UserOpt make_empty_user_opt() { UserOpt opt; opt.action = CK_WRONG_ACTION; @@ -309,7 +316,7 @@ void verbose() { } } -void get_possible_action_strings(char *dest, CkAction ckAction) { +char * get_possible_action_strings(char *dest, CkAction ckAction) { char buf[STR_M] = ""; switch (ckAction) { #define X(ACTION) \ @@ -326,10 +333,11 @@ void get_possible_action_strings(char *dest, CkAction ckAction) { #undef X default: dest = NULL; - return; + return NULL; } strcpy(dest, buf); + return dest; } void print_parser_error(UserOpt *opt) { @@ -374,22 +382,14 @@ void print_parser_help() { char names[STR_M] = ""; ckhelp("ck - the config keeper"); ckhelp("Usage:"); - get_possible_action_strings(names, CKA_INIT); - ckhelp("Initialize: \t%s", names); - get_possible_action_strings(names, CKA_ADD); - ckhelp("Add config: \t%s", names); - get_possible_action_strings(names, CKA_DEL); - ckhelp("Delete config: \t%s", names); - get_possible_action_strings(names, CKA_EDIT); - ckhelp("Edit config: \t%s", names); - get_possible_action_strings(names, CKA_LIST); - ckhelp("List configs: \t%s", names); - get_possible_action_strings(names, CKA_SEARCH); - ckhelp("Search configs: \t%s", names); - get_possible_action_strings(names, CKA_RESTORE); - ckhelp("Restore links: \t%s", names); - get_possible_action_strings(names, CKA_HELP); - ckhelp("Help: \t%s", names); + ckhelp("Init\t%s", get_possible_action_strings(names, CKA_INIT)); + ckhelp("Add\t%s", get_possible_action_strings(names, CKA_ADD)); + ckhelp("Delete\t%s", get_possible_action_strings(names, CKA_DEL)); + ckhelp("Edit\t%s", get_possible_action_strings(names, CKA_EDIT)); + ckhelp("List\t%s", get_possible_action_strings(names, CKA_LIST)); + ckhelp("Search\t%s", get_possible_action_strings(names, CKA_SEARCH)); + ckhelp("Restore\t%s", get_possible_action_strings(names, CKA_RESTORE)); + ckhelp("Help\t%s", get_possible_action_strings(names, CKA_HELP)); report_help(); } @@ -416,13 +416,6 @@ int parse_action(int argc, const char **argv, UserOpt *opt) { print_parser_help(); return -1; } - /* find the action */ - determine_action(opt); - if (opt->action == CK_WRONG_ACTION) { - opt->err = PERR_UNKNOWN_ACTION; - print_parser_error(opt); - return -1; - } /* parse values */ if (parse_vals(opt)) { print_parser_error(opt); -- cgit v1.2.3