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 | |
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')
-rw-r--r-- | src/actions.c | 2 | ||||
-rw-r--r-- | src/actions.h | 4 | ||||
-rw-r--r-- | src/ck.c | 2 | ||||
-rw-r--r-- | src/clparser.c | 126 | ||||
-rw-r--r-- | src/clparser.h | 23 | ||||
-rw-r--r-- | src/restore.c | 1 |
6 files changed, 33 insertions, 125 deletions
diff --git a/src/actions.c b/src/actions.c index c977aee..3293e53 100644 --- a/src/actions.c +++ b/src/actions.c @@ -292,7 +292,7 @@ int run_HELP(UserOpt *opt, Conf *conf) { } switch(parser_get_action(list_get(opt->args), NULL)) { -#define X(ACTION) \ +#define X(ACTION, MIN, MAX) \ case CKA_##ACTION: \ HELP("%s", get_possible_action_strings(tmp, CKA_##ACTION)); \ print_##ACTION##_help(); \ diff --git a/src/actions.h b/src/actions.h index 2015538..4550d4c 100644 --- a/src/actions.h +++ b/src/actions.h @@ -20,7 +20,7 @@ #include "clparser.h" #include "confparser.h" -#define X(ACTION) \ +#define X(ACTION, MIN, MAX) \ int run_##ACTION(UserOpt *, Conf *); CK_ACTIONS #undef X @@ -84,7 +84,7 @@ int restore_make_links(cklist *from, cklist *to); /************************/ /* PRINT RESULTS & HELP */ /************************/ -#define X(ACTION) \ +#define X(ACTION, MIN, MAX) \ void print_##ACTION##_help(void); CK_ACTIONS #undef X @@ -65,7 +65,7 @@ int main(int argc, const char **argv) { /* Run action and print results */ switch(opt.action) { -#define X(ACTION) \ +#define X(ACTION, MIN, MAX) \ case CKA_##ACTION: \ rc = run_##ACTION(&opt, &conf); \ break; 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(); \ diff --git a/src/clparser.h b/src/clparser.h index 9b22b87..f6818fa 100644 --- a/src/clparser.h +++ b/src/clparser.h @@ -20,19 +20,20 @@ #include "cklist.h" -#define CK_ACTIONS \ - X(INIT) \ - X(ADD) \ - X(DEL) \ - X(EDIT) \ - X(LIST) \ - X(SEARCH) \ - X(RESTORE) \ - X(HELP) +/* NAME | MIN ACCEPTED ARGS | MAX ACCEPTED ARGS */ +#define CK_ACTIONS \ + X(INIT, 2 , 2) \ + X(ADD, 2, 4) \ + X(DEL, 1, 2) \ + X(EDIT, 1, 2) \ + X(LIST, 1, 6) \ + X(SEARCH, 1, 1) \ + X(RESTORE, 1, 2) \ + X(HELP, 1, 1) enum ParseErrors { PERR_NOERR = 0, -#define X(ACTION) \ +#define X(ACTION, MIN, MAX) \ PERR_##ACTION##_WRONG, CK_ACTIONS #undef X @@ -42,7 +43,7 @@ typedef enum ParseErrors ParseError; enum CkActions { CK_WRONG_ACTION, -#define X(ACTION) \ +#define X(ACTION, MIN, MAX) \ CKA_##ACTION, CK_ACTIONS #undef X diff --git a/src/restore.c b/src/restore.c index c90cb82..cb8e6c9 100644 --- a/src/restore.c +++ b/src/restore.c @@ -14,6 +14,7 @@ int restore_make_links(cklist *from, cklist *to) { && list_size(to) > 0 && list_size(from) == list_size(to)) { do { + HELP("If %d OR %d then file exists.", util_file_exists(list_get(to), NULL), !util_is_file_link(list_get(to))); if (util_file_exists(list_get(to), NULL) || !util_is_file_link(list_get(to))) { ERR("File %s already exists.", list_get(to)); |