From dba9e5352a9ff792a71fbaede28f013b5d03daae Mon Sep 17 00:00:00 2001 From: Anastasis Grammenos Date: Fri, 21 Sep 2018 18:43:38 +0300 Subject: Small refactoring in actions --- src/actionhelper.c | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/actionhelper.h | 39 +++++++++++--- src/actions.c | 146 +---------------------------------------------------- src/actions.h | 5 -- src/ck.c | 2 +- 5 files changed, 179 insertions(+), 158 deletions(-) (limited to 'src') diff --git a/src/actionhelper.c b/src/actionhelper.c index c4c1f34..5007b62 100644 --- a/src/actionhelper.c +++ b/src/actionhelper.c @@ -48,6 +48,48 @@ void move_config(const AddOpt *opt, const Conf *conf, char *ret) { } } +AddOpt add_make_options(cklist *args) { + list_rewind(args); + /* since we are here, the first two argumens must exist */ + AddOpt addOpt = { + .progName = list_get(args), + .secret = 0, + .prime = 0, + .err = ADD_NO_ERR + }; + + list_next(args); + if (!util_is_file_rw(list_get(args))) { + addOpt.err = ADD_ERR_WRONG_CONFIG; + return addOpt; + } + realpath(list_get(args), addOpt.confPath); + + while (list_next(args)) { + if (strcmp(list_get(args), "-s") == 0 && addOpt.secret == 0) { + addOpt.secret = 1; + } else if (strcmp(list_get(args), "-p") == 0 && addOpt.prime == 0) { + addOpt.prime = 1; + } else { + addOpt.err = ADD_ERR_WRONG_FLAGS; + return addOpt; + } + } + list_rewind(args); + return addOpt; +} + +void add_print_opts(AddOpt *opt) { + printf("Program:\t%s\nConfig:\t\t%s\n", opt->progName, opt->confPath); + if (opt->prime && opt->secret) { + printf("Options:\tsecret, primary\n"); + } else if (opt->prime) { + printf("Options:\tprimary\n"); + } else if (opt->secret) { + printf("Options:\tsecret\n"); + } +} + void add_make_link(const AddOpt *opt, const Conf *conf) { char newPath[STR_L]; @@ -67,3 +109,106 @@ edit_get_config_or_suggestions(cklist *args, char *ret) { UNUSED(ret); return ERC_ERR; } + +ListOpt list_make_options(cklist *args) { + list_rewind(args); + ListOpt listOpt = { + ._lt = LT_NONE, + ._lst = LST_PLAIN, + .err = 0 + }; + + if (list_size(args)) { + do { + if (strcmp(list_get(args), "-t") == 0) { + if (!list_next(args)) { + listOpt.err = 1; + break; + } + if (strcmp(list_get(args), "plain") == 0) { + listOpt._lst = LST_PLAIN; + } + else if (strcmp(list_get(args), "lisp") == 0) { + listOpt._lst = LST_LISP; + } + else if (strcmp(list_get(args), "python") == 0) { + listOpt._lst = LST_PYTHON; + } + else { + listOpt.err = 1; + } + } + else if (strcmp(list_get(args), "paths") == 0) { + listOpt._lt = LT_PATH; + } + else if (strcmp(list_get(args), "programs") == 0) { + listOpt._lt = LT_PROGRAM; + } + else if (strcmp(list_get(args), "tree") == 0) { + listOpt._lt = LT_TREE; + } + else { + listOpt.err = 1; + } + } while(list_next(args)); + } + list_rewind(args); + return listOpt; +} + +/*****************/ +/* PRINT RESULTS */ +/*****************/ + +void print_INIT_result(int ok) { + if (ok) { + printf("Initialized empty ckdb.\n"); + } +} + +void print_ADD_result(int ok) { + if (ok) { + printf("ckdb updated succesfully.\n"); + return; + } + printf("Could not complete add transaction.\n"); +} + +void print_DEL_result(int ok) { + if (ok) { + printf("succes\n"); + return; + } + printf("Not Supported\n"); +} + +void print_EDIT_result(int ok) { + if (ok) { + printf("succes\n"); + return; + } + printf("failure\n"); +} + +void print_LIST_result(int ok) { + if (ok) { + return; + } + printf("Wrong list arguments\n"); +} + +void print_SEARCH_result(int ok) { + if (ok) { + printf("succes\n"); + return; + } + printf("Not Supported\n"); +} + +void print_HELP_result(int ok) { + if (ok) { + printf("succes\n"); + return; + } + printf("failure\n"); +} diff --git a/src/actionhelper.h b/src/actionhelper.h index 2a47e79..3393ec3 100644 --- a/src/actionhelper.h +++ b/src/actionhelper.h @@ -1,4 +1,3 @@ - /* actionhelper.h - helper functions for ck actions --------------------*- C -*- * * This file is part of ck, the config keeper @@ -12,10 +11,28 @@ * * * -------------------------------------------------------------------------- */ +#ifndef ACTIONHELPER_H +#define ACTIONHELPER_H + #include "actions.h" #include "ckutil.h" #include "cklist.h" +/*******/ +/* ADD */ +/*******/ + +/* if add_err is set, return true + * along with a copy of the error message. + * Pass NULL if you don't care about the message */ +extern int add_err_message(char *err); +extern AddOpt add_make_options(cklist *args); +extern void add_print_opts(AddOpt *opt); +extern void add_make_link(const AddOpt *opt, const Conf *conf); + +/********/ +/* EDIT */ +/********/ typedef enum edit_opt_return_code edit_rc; enum edit_opt_return_code { ERC_OK, @@ -23,13 +40,19 @@ enum edit_opt_return_code { ERC_ERR }; -/* if add_err is set, return true - * along with a copy of the error message. - * Pass NULL if you don't care about the message */ -extern int add_err_message(char *err); +extern edit_rc edit_get_config_or_suggestions(cklist* args, char *ret); +/********/ +/* LIST */ +/********/ +extern ListOpt list_make_options(cklist *args); -extern void add_make_link(const AddOpt *opt, - const Conf *conf); +/*****************/ +/* PRINT RESULTS */ +/*****************/ +#define X(ACTION) \ + extern void print_##ACTION##_result(int ok); +CK_ACTIONS +#undef X -extern edit_rc edit_get_config_or_suggestions(cklist* args, char *ret); +#endif /* ACTIONHELPER_H */ diff --git a/src/actions.c b/src/actions.c index c084e8f..8f89484 100644 --- a/src/actions.c +++ b/src/actions.c @@ -35,48 +35,6 @@ int run_INIT(UserOpt * opt, Conf *conf) { return 1; } -AddOpt make_add_options(cklist* args) { - list_rewind(args); - /* since we are here, the first two argumens must exist */ - AddOpt addOpt = { - .progName = list_get(args), - .secret = 0, - .prime = 0, - .err = ADD_NO_ERR - }; - - list_next(args); - if (!util_is_file_rw(list_get(args))) { - addOpt.err = ADD_ERR_WRONG_CONFIG; - return addOpt; - } - realpath(list_get(args), addOpt.confPath); - - while (list_next(args)) { - if (strcmp(list_get(args), "-s") == 0 && addOpt.secret == 0) { - addOpt.secret = 1; - } else if (strcmp(list_get(args), "-p") == 0 && addOpt.prime == 0) { - addOpt.prime = 1; - } else { - addOpt.err = ADD_ERR_WRONG_FLAGS; - return addOpt; - } - } - list_rewind(args); - return addOpt; -} - -void add_print_opts(AddOpt *opt) { - printf("Program:\t%s\nConfig:\t\t%s\n", opt->progName, opt->confPath); - if (opt->prime && opt->secret) { - printf("Options:\tsecret, primary\n"); - } else if (opt->prime) { - printf("Options:\tprimary\n"); - } else if (opt->secret) { - printf("Options:\tsecret\n"); - } -} - int run_ADD(UserOpt * opt, Conf *conf) { DB db = open_DB(opt); if (db.ptr == NULL) { @@ -85,7 +43,7 @@ int run_ADD(UserOpt * opt, Conf *conf) { } return 0; } - AddOpt addOpt = make_add_options(opt->args); + AddOpt addOpt = add_make_options(opt->args); switch (addOpt.err) { case ADD_NO_ERR: break; @@ -165,52 +123,6 @@ int run_EDIT(UserOpt *opt, Conf *conf) { return 1; } -ListOpt make_list_options(cklist *args) { - list_rewind(args); - ListOpt listOpt = { - ._lt = LT_NONE, - ._lst = LST_PLAIN, - .err = 0 - }; - - if (list_size(args)) { - do { - if (strcmp(list_get(args), "-t") == 0) { - if (!list_next(args)) { - listOpt.err = 1; - break; - } - if (strcmp(list_get(args), "plain") == 0) { - listOpt._lst = LST_PLAIN; - } - else if (strcmp(list_get(args), "lisp") == 0) { - listOpt._lst = LST_LISP; - } - else if (strcmp(list_get(args), "python") == 0) { - listOpt._lst = LST_PYTHON; - } - else { - listOpt.err = 1; - } - } - else if (strcmp(list_get(args), "paths") == 0) { - listOpt._lt = LT_PATH; - } - else if (strcmp(list_get(args), "programs") == 0) { - listOpt._lt = LT_PROGRAM; - } - else if (strcmp(list_get(args), "tree") == 0) { - listOpt._lt = LT_TREE; - } - else { - listOpt.err = 1; - } - } while(list_next(args)); - } - list_rewind(args); - return listOpt; -} - int run_LIST(UserOpt *opt, Conf *conf) { UNUSED(conf); DB db = open_DB(opt); @@ -223,7 +135,7 @@ int run_LIST(UserOpt *opt, Conf *conf) { cklist *list_type = list_make_new(); - ListOpt listOpt = make_list_options(opt->args); + ListOpt listOpt = list_make_options(opt->args); if (listOpt.err) { close_DB(&db); list_free(list_type); @@ -274,57 +186,3 @@ int run_HELP(UserOpt *opt, Conf *conf) { printf("Running %s\n", "help"); return 0; } - -void print_INIT_result(int ok) { - if (ok) { - printf("Initialized empty ckdb.\n"); - } -} - -void print_ADD_result(int ok) { - if (ok) { - printf("ckdb updated succesfully.\n"); - return; - } - printf("Could not complete add transaction.\n"); -} - -void print_DEL_result(int ok) { - if (ok) { - printf("succes\n"); - return; - } - printf("Not Supported\n"); -} - -void print_EDIT_result(int ok) { - if (ok) { - printf("succes\n"); - return; - } - printf("failure\n"); -} - -void print_LIST_result(int ok) { - if (ok) { - return; - } - printf("Wrong list arguments\n"); -} - -void print_SEARCH_result(int ok) { - if (ok) { - printf("succes\n"); - return; - } - printf("Not Supported\n"); -} - -void print_HELP_result(int ok) { - if (ok) { - printf("succes\n"); - return; - } - printf("failure\n"); -} - diff --git a/src/actions.h b/src/actions.h index e462e74..414c098 100644 --- a/src/actions.h +++ b/src/actions.h @@ -24,11 +24,6 @@ CK_ACTIONS #undef X -#define X(ACTION) \ - extern void print_##ACTION##_result(int ok); -CK_ACTIONS -#undef X - typedef enum AddOptErrors AddOptErr; enum AddOptErrors { ADD_NO_ERR = 0, diff --git a/src/ck.c b/src/ck.c index 96bc50a..0d093d3 100644 --- a/src/ck.c +++ b/src/ck.c @@ -28,7 +28,7 @@ * * -------------------------------------------------------------------------- */ -#include "actions.h" +#include "actionhelper.h" #include "dblayer.h" #include "cklist.h" #include "ckutil.h" -- cgit v1.2.3