diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/actionhelper.c | 30 | ||||
-rw-r--r-- | src/actionhelper.h | 5 | ||||
-rw-r--r-- | src/actionparser.c | 10 | ||||
-rw-r--r-- | src/actions.c | 28 | ||||
-rw-r--r-- | src/actions.h | 15 |
5 files changed, 85 insertions, 3 deletions
diff --git a/src/actionhelper.c b/src/actionhelper.c index c3b1b81..596a7db 100644 --- a/src/actionhelper.c +++ b/src/actionhelper.c @@ -50,7 +50,7 @@ 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 */ + /* since we are here, the first two arguments must exist */ AddOpt addOpt = { .progName = list_get(args), .secret = 0, @@ -79,6 +79,34 @@ AddOpt add_make_options(cklist *args) { return addOpt; } +DelOpt del_make_options(cklist *args) { + list_rewind(args); + DelOpt delOpt = { + .prog = NULL, + .path = "", + .isConf = 0, + .err = DEL_NO_ERR + }; + + if (strcmp(list_get(args), "-c") == 0) { + delOpt.isConf = 1; + if (!list_next(args)) { + delOpt.err = DEL_ERR_WRONG_ARGS; + return delOpt; + } + realpath(list_get(args), delOpt.path); + if (!util_is_file_rw(delOpt.path)) { + delOpt.err = DEL_ERR_WRONG_PATH; + return delOpt; + } + } else { + delOpt.prog = list_get(args); + } + + list_rewind(args); + return delOpt; +} + void add_print_opts(AddOpt *opt) { printf("Program:\t%s\nConfig:\t\t%s\n", opt->progName, opt->confPath); if (opt->prime && opt->secret) { diff --git a/src/actionhelper.h b/src/actionhelper.h index 3dea765..f046ec9 100644 --- a/src/actionhelper.h +++ b/src/actionhelper.h @@ -30,6 +30,11 @@ 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); +/*******/ +/* DEL */ +/*******/ +extern DelOpt del_make_options(cklist *args); + /********/ /* EDIT */ /********/ diff --git a/src/actionparser.c b/src/actionparser.c index 0057e7f..82ae25e 100644 --- a/src/actionparser.c +++ b/src/actionparser.c @@ -100,7 +100,15 @@ int parse_ADD(UserOpt *opt) { } int parse_DEL(UserOpt *opt) { - return -1; + /* DEL expects 1 to 2 arguments */ + if (optNum <= pos || optNum > pos + 2) { + opt->err = PERR_DEL_WRONG; + return -1; + } + + int arg_num = optNum - pos; + fill_args_list(arg_num, opt); + return 1; } int parse_EDIT(UserOpt *opt) { diff --git a/src/actions.c b/src/actions.c index 54e781b..70705e4 100644 --- a/src/actions.c +++ b/src/actions.c @@ -72,7 +72,33 @@ int run_ADD(UserOpt * opt, Conf *conf) { } int run_DEL(UserOpt * opt, Conf *conf) { - printf("Running %s\n", "del"); + printf("Running del\n"); + DB db = open_DB(opt); + if (db.ptr == NULL) { + if (db.error == SQL_ERR_NO_TABLES) { + PRINT_ERR("The database file is currupted. Run ck init anew."); + } + goto error; + } + + DelOpt delOpt = del_make_options(opt->args); + switch(delOpt.err) { + case DEL_NO_ERR: + if (delOpt.isConf) { + printf("deleting conf %s\n", delOpt.path); + } else { + printf("Deleting program %s\n", delOpt.prog); + } + close_DB(&db); + return 0; + case DEL_ERR_WRONG_ARGS: + printf("wrong del args\n"); + break; + case DEL_ERR_WRONG_PATH: + printf("path %s doesnt exist\n", delOpt.path); + } + error: + close_DB(&db); return 1; } diff --git a/src/actions.h b/src/actions.h index 5e32164..0ca7838 100644 --- a/src/actions.h +++ b/src/actions.h @@ -40,6 +40,21 @@ struct AddOptions { AddOptErr err; }; +typedef enum DelErrors DelErr; +enum DelErrors { + DEL_NO_ERR = 0, + DEL_ERR_WRONG_PATH, + DEL_ERR_WRONG_ARGS +}; + +typedef struct DelOptions DelOpt; +struct DelOptions { + char *prog; + char path[STR_L]; + int isConf; + DelErr err; +}; + typedef enum ListTypes ListType; enum ListTypes { LT_PATH, |