diff options
author | gramanas <anastasis.gramm2@gmail.com> | 2018-11-16 18:05:57 +0200 |
---|---|---|
committer | gramanas <anastasis.gramm2@gmail.com> | 2018-11-16 18:05:57 +0200 |
commit | 8eb4df24eb30353bea82268440396e50ed8b1bbc (patch) | |
tree | c885ab94952315f64bd596512cd23d0ef5509b3e /src/clparser.c | |
parent | 2919a5e2c1d33d170bffc93dfeaa6292b9fa3dca (diff) | |
download | ck-8eb4df24eb30353bea82268440396e50ed8b1bbc.tar.gz ck-8eb4df24eb30353bea82268440396e50ed8b1bbc.tar.bz2 ck-8eb4df24eb30353bea82268440396e50ed8b1bbc.zip |
Fix bugs in test-ck and simplify clparser
Diffstat (limited to 'src/clparser.c')
-rw-r--r-- | src/clparser.c | 126 |
1 files changed, 16 insertions, 110 deletions
diff --git a/src/clparser.c b/src/clparser.c index ba73db0..081c981 100644 --- a/src/clparser.c +++ b/src/clparser.c @@ -68,113 +68,19 @@ static void fill_args_list(int arg_num, UserOpt *opt) { } } -/* When starting to parse the action, - * `pos` should be at 2 - * like so "ck ACTION ..." - * ^ */ -static int parse_INIT(UserOpt *opt) { - /* INIT expects 2 arguments - * starting from 0 */ - int arg_num = 2; - if (optNum != pos /* already consumed */ + arg_num) { - opt->err = PERR_INIT_WRONG; - return -1; - } - - fill_args_list(arg_num, opt); - return 0; -} - -static int parse_ADD(UserOpt *opt) { - /* ADD expects 2 to 4 arguments */ - if (optNum < pos + 2 - || optNum > pos + 4) { - opt->err = PERR_ADD_WRONG; - return -1; - } - - int arg_num = optNum - pos; - fill_args_list(arg_num, opt); - return 0; -} - -static int parse_DEL(UserOpt *opt) { - /* DEL expects 1 to 2 arguments */ - if (optNum < pos + 1 - || optNum > pos + 2) { - opt->err = PERR_DEL_WRONG; - return -1; - } - - int arg_num = optNum - pos; - fill_args_list(arg_num, opt); - return 0; -} - -static int parse_EDIT(UserOpt *opt) { - /* EDIT expects 1 to 2 arguments */ - if (optNum < pos + 1 - || optNum > pos + 2) { - opt->err = PERR_EDIT_WRONG; - return -1; +#define X(ACTION, MIN, MAX) \ + static int parse_ ##ACTION(UserOpt *opt) { \ + if (optNum < pos + MIN \ + || optNum > pos + MAX) { \ + opt->err = PERR_ ##ACTION## _WRONG; \ + return -1; \ + } \ + int arg_num = optNum - pos; \ + fill_args_list(arg_num, opt); \ + return 0; \ } - - int arg_num = optNum - pos; - fill_args_list(arg_num, opt); - return 0; -} - -static int parse_LIST(UserOpt *opt) { - /* List expects 1 to 6 arguments */ - if (optNum < pos + 1 - || optNum > pos + 6) { - opt->err = PERR_LIST_WRONG; - return -1; - } - - int arg_num = optNum - pos; - fill_args_list(arg_num, opt); - return 0; -} - -static int parse_SEARCH(UserOpt *opt) { - /* Search expects a maximum of 1 argument */ - if (optNum < pos + 1 - || optNum > pos + 1) { - opt->err = PERR_SEARCH_WRONG; - return -1; - } - - int arg_num = optNum - pos; - fill_args_list(arg_num, opt); - return 0; -} - -static int parse_HELP(UserOpt *opt) { - /* Help expects a maximum of 1 argument */ - if (optNum < pos + 1 - || optNum > pos + 1) { - opt->err = PERR_HELP_WRONG; - return -1; - } - - int arg_num = optNum - pos; - fill_args_list(arg_num, opt); - return 0; -} - -static int parse_RESTORE(UserOpt *opt) { - /* Restore expects 1 to 2 arguments */ - if (optNum < pos + 1 - || optNum > pos + 2) { - opt->err = PERR_RESTORE_WRONG; - return -1; - } - - int arg_num = optNum - pos; - fill_args_list(arg_num, opt); - return 0; -} +CK_ACTIONS +#undef X static void determine_action(UserOpt *opt) { /* get action */ @@ -200,7 +106,7 @@ static int parse_vals(UserOpt *opt) { } switch (opt->action) { -#define X(ACTION) \ +#define X(ACTION, MIN, MAX) \ case CKA_##ACTION: \ return parse_##ACTION(opt); CK_ACTIONS @@ -212,7 +118,7 @@ static int parse_vals(UserOpt *opt) { CkAction parser_get_action(const char *name, char *actionName) { int i; -#define X(ACTION) \ +#define X(ACTION, MIN, MAX) \ for (i = 1; i < atoi(str##ACTION[0]) + 1; i++) { \ if (strcmp(name, str##ACTION[i]) == 0) { \ if (actionName) { \ @@ -320,7 +226,7 @@ static void verbose() { char * get_possible_action_strings(char *dest, CkAction ckAction) { char buf[STR_M] = ""; switch (ckAction) { -#define X(ACTION) \ +#define X(ACTION, MIN, MAX) \ case CKA_##ACTION: \ strcpy(buf, "{ "); \ for (int i = 1; i < atoi(str##ACTION[0]); i++) { \ @@ -351,7 +257,7 @@ static void print_parser_error(UserOpt *opt) { case PERR_UNKNOWN_ACTION: ERR("Unknown action: %s", token); return; -#define X(ACTION) \ +#define X(ACTION, MIN, MAX) \ case PERR_ ##ACTION## _WRONG: \ HELP("Usage:\n%s", names); \ print_##ACTION##_help(); \ |