diff options
-rw-r--r-- | CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/actionhelper.c | 68 | ||||
-rw-r--r-- | src/actionhelper.h | 27 | ||||
-rw-r--r-- | src/actionparser.c | 51 | ||||
-rw-r--r-- | src/actionparser.h | 5 | ||||
-rw-r--r-- | src/actions.c | 54 | ||||
-rw-r--r-- | src/ck.c | 25 | ||||
-rw-r--r-- | src/cklist.c | 35 | ||||
-rw-r--r-- | src/cklist.h | 6 | ||||
-rw-r--r-- | src/confparser.c | 25 | ||||
-rw-r--r-- | src/engine.c | 62 | ||||
-rw-r--r-- | src/engine.h | 34 |
12 files changed, 183 insertions, 212 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index fe0934c..088ce3b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,8 +38,8 @@ set(ckLib_src ${SRC_DIR}/dbhelper.c ${SRC_DIR}/ckutil.c ${SRC_DIR}/cklist.c - ${SRC_DIR}/engine.c ) + set(ckLib_hdr ${SRC_DIR}/actionparser.h ${SRC_DIR}/actions.h @@ -49,7 +49,6 @@ set(ckLib_hdr ${SRC_DIR}/dbhelper.h ${SRC_DIR}/ckutil.h ${SRC_DIR}/cklist.h - ${SRC_DIR}/engine.h ) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RES_DIR}/cmake) diff --git a/src/actionhelper.c b/src/actionhelper.c index 55f0326..7d6aa8a 100644 --- a/src/actionhelper.c +++ b/src/actionhelper.c @@ -1,11 +1,69 @@ +/* actionhelper.c - helper functions for ck actions --------------------*- C -*- + * + * This file is part of ck, the config keeper + * + * ----------------------------------------------------------------------------- + * + * Copyright (C) 2018 Anastasis Grammenos + * GPLv3 (see LICENCE for the full notice) + * + * ----------------------------------------------------------------------------- + * + * + * + * -------------------------------------------------------------------------- */ #include "actionhelper.h" -/* Holds the suggestions */ -char _sug[2][STR_L]; +char add_err[STR_M] = ""; + +int add_err_message(char *err) { + if (!str_is_empty(add_err)) { + if (err) { + strcpy(err, add_err); + } + return 1; + } + return 0; +} + +int copy_config(char *path); + + +void link_config(const AddOpt *opt, const char* newPath) { + printf("Linking %s -> %s\n", newPath, opt->confPath); + if (util_symlink_file(newPath, opt->confPath) != 0) { + strcpy(add_err, "Could not link file."); + } +} + +void move_config(const AddOpt *opt, const Conf *conf, char *ret) { + char newName[STR_M]; + str_make_ck_config_name(newName, opt->confPath, opt->progName); + char newPath[STR_L]; + str_join_dirname_with_basename(newPath, opt->secret ? conf->SCRT_dir : conf->VC_dir, newName); + printf("Moving %s -> %s\n", opt->confPath, newPath); + strcpy(ret, newPath); + if (util_move_file(opt->confPath, newPath) != 0) { + strcpy(add_err, "Could not move file."); + } +} + +void add_make_link(const AddOpt *opt, + const Conf *conf) { + char newPath[STR_L]; + move_config(opt, conf, newPath); + if (add_err_message(NULL)) { + return; + } + link_config(opt, newPath); + if (add_err_message(NULL)) { + return; + } +} edit_rc -edit_get_config_or_suggestions(const int argc, - char **argv, - char *ret) { +edit_get_config_or_suggestions(cklist *args, char *ret) { + UNUSED(args); + UNUSED(ret); return ERC_ERR; } diff --git a/src/actionhelper.h b/src/actionhelper.h index 75ad0a7..2a47e79 100644 --- a/src/actionhelper.h +++ b/src/actionhelper.h @@ -1,4 +1,20 @@ + +/* actionhelper.h - helper functions for ck actions --------------------*- C -*- + * + * This file is part of ck, the config keeper + * + * ----------------------------------------------------------------------------- + * + * Copyright (C) 2018 Anastasis Grammenos + * GPLv3 (see LICENCE for the full notice) + * + * ----------------------------------------------------------------------------- + * + * + * -------------------------------------------------------------------------- */ +#include "actions.h" #include "ckutil.h" +#include "cklist.h" typedef enum edit_opt_return_code edit_rc; enum edit_opt_return_code { @@ -7,4 +23,13 @@ enum edit_opt_return_code { ERC_ERR }; -extern edit_rc edit_get_config_or_suggestions(const int argc, char **argv, char *ret); +/* 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 void add_make_link(const AddOpt *opt, + const Conf *conf); + +extern edit_rc edit_get_config_or_suggestions(cklist* args, char *ret); diff --git a/src/actionparser.c b/src/actionparser.c index b3afbfc..6c3cb77 100644 --- a/src/actionparser.c +++ b/src/actionparser.c @@ -58,10 +58,13 @@ int next_token() { void get_opt(int position, UserOpt *opt) { /* get arg */ next_token(); + list_add(opt->args, token); +} - /* allocate memory */ - opt->argv[position] = (char *)malloc((strlen(token))*sizeof(char) + 1); - strcpy(opt->argv[position], token); +void fill_args_list(int arg_num, UserOpt *opt) { + for (int i = 0; i < arg_num; i++) { + get_opt(i, opt); + } } /* When starting to parse the action, @@ -71,15 +74,13 @@ void get_opt(int position, UserOpt *opt) { int parse_INIT(UserOpt *opt) { /* INIT expects 2 arguments * starting from 0 */ - opt->argc = 2; - if (optNum != pos /* already consumed */ + opt->argc) { + int arg_num = 2; + if (optNum != pos /* already consumed */ + arg_num) { opt->err = PERR_INIT_WRONG; return -1; } - for (int i = 0; i < opt->argc; i++) { - get_opt(i, opt); - } + fill_args_list(arg_num, opt); return 1; } @@ -91,10 +92,8 @@ int parse_ADD(UserOpt *opt) { return -1; } - opt->argc = optNum - pos; - for (int i = 0; i < opt->argc; i++) { - get_opt(i, opt); - } + int arg_num = optNum - pos; + fill_args_list(arg_num, opt); return 1; } @@ -107,14 +106,21 @@ int parse_EDIT(UserOpt *opt) { opt->err = PERR_EDIT_WRONG; return -1; } - opt->argc = optNum - pos; - for (int i = 0; i < opt->argc; i++) { - get_opt(i, opt); - } + + int arg_num = optNum - pos; + fill_args_list(arg_num, opt); return 1; } int parse_LIST(UserOpt *opt) { - return -1; + /* List expects a maximum of than 2 arguments */ + if (optNum > pos + 2) { + opt->err = PERR_EDIT_WRONG; + return -1; + } + + int arg_num = optNum - pos; + fill_args_list(arg_num, opt); + return 1; } int parse_SEARCH(UserOpt *opt) { return -1; @@ -162,11 +168,8 @@ UserOpt make_empty_user_opt() { UserOpt opt; opt.action = -1; opt.err = PERR_NOERR; - opt.argc = 0; opt.confDir = NULL; - for (int i = 0; i < 10; i++) { - opt.argv[i] = NULL; - } + opt.args = list_make_new(); return opt; } @@ -174,14 +177,10 @@ UserOpt make_empty_user_opt() { /* called to free the resources * UserOpt holds */ void free_user_opt(UserOpt *opt) { - for (int i = 0; i < 10; i++) { - if (opt->argv[i] != NULL) { - free(opt->argv[i]); - } - } if (opt->confDir != NULL) { free(opt->confDir); } + list_free(opt->args); } /* If the used has specified a config file other diff --git a/src/actionparser.h b/src/actionparser.h index 1c4e539..01f32a5 100644 --- a/src/actionparser.h +++ b/src/actionparser.h @@ -22,6 +22,8 @@ #ifndef ACTIONPARSER_H #define ACTIONPARSER_H +#include "cklist.h" + #define CK_ACTIONS \ X(INIT) \ X(ADD) \ @@ -62,8 +64,7 @@ struct UserOptions { ParseError err; CkAction action; char *confDir; - int argc; - char *argv[10]; // action's options + cklist *args; }; diff --git a/src/actions.c b/src/actions.c index e72e2b5..2dafd0b 100644 --- a/src/actions.c +++ b/src/actions.c @@ -12,7 +12,7 @@ #include "actionhelper.h" #include "dblayer.h" #include "ckutil.h" -#include "engine.h" +#include "cklist.h" int run_INIT(UserOpt * opt, Conf *conf) { UNUSED(conf); @@ -32,44 +32,35 @@ int run_INIT(UserOpt * opt, Conf *conf) { return 1; } -AddOpt make_add_options(const int argc, char **argv) { +AddOpt make_add_options(cklist* args) { + list_rewind(args); /* since we are here, the first two argumens must exist */ AddOpt addOpt = { - .progName = argv[0], + .progName = list_get(args), .confPath = NULL, .secret = 0, .prime = 0, .err = ADD_NO_ERR }; - - if (!util_is_file_rw(argv[1])) { + + list_next(args); + if (!util_is_file_rw(list_get(args))) { addOpt.err = ADD_ERR_WRONG_CONFIG; return addOpt; } - addOpt.confPath = argv[1]; + addOpt.confPath = list_get(args); - if (argc == 3) { - if (strcmp(argv[2], "-s") == 0) { + while (list_next(args)) { + if (strcmp(list_get(args), "-s") == 0 && addOpt.secret == 0) { addOpt.secret = 1; - } else if (strcmp(argv[2], "-p") == 0) { + } else if (strcmp(list_get(args), "-p") == 0 && addOpt.prime == 0) { addOpt.prime = 1; } else { addOpt.err = ADD_ERR_WRONG_FLAGS; return addOpt; } - } else if (argc == 4) { - int i; - for (i = 2; i < 4; i++) { - if (strcmp(argv[i], "-s") == 0) { - addOpt.secret = 1; - } else if (strcmp(argv[i], "-p") == 0) { - addOpt.prime = 1; - } else { - addOpt.err = ADD_ERR_WRONG_FLAGS; - return addOpt; - } - } } + list_rewind(args); return addOpt; } @@ -92,7 +83,7 @@ int run_ADD(UserOpt * opt, Conf *conf) { } return 0; } - AddOpt addOpt = make_add_options(opt->argc, opt->argv); + AddOpt addOpt = make_add_options(opt->args); switch (addOpt.err) { case ADD_NO_ERR: break; @@ -111,9 +102,9 @@ int run_ADD(UserOpt * opt, Conf *conf) { return 0; } close_DB(&db); - engine_add_make_link(&addOpt, conf); + add_make_link(&addOpt, conf); char err[STR_M]; - if (engine_err_message(err)) { + if (add_err_message(err)) { PRINT_ERR(err); return 0; } @@ -135,11 +126,12 @@ int run_EDIT(UserOpt *opt, Conf *conf) { return 0; } + list_rewind(opt->args); char confPath[STR_M]; - if (opt->argc == 1) { + if (list_size(opt->args) == 1) { char confName[STR_M]; int secret = 0; - if (edit_get_prime_config_from_program(&db, opt->argv[0], confName, &secret) == 1) { + if (edit_get_prime_config_from_program(&db, list_get(opt->args), confName, &secret) == 1) { str_join_dirname_with_basename(confPath, secret ? conf->SCRT_dir : conf->VC_dir, confName); printf("%s\n", confPath); } else { @@ -150,13 +142,13 @@ int run_EDIT(UserOpt *opt, Conf *conf) { } else { close_DB(&db); char confName[STR_L]; - switch (edit_get_config_or_suggestions(opt->argc, - opt->argv, - confName)) { + switch (edit_get_config_or_suggestions(opt->args, confName)) { case ERC_OK: return 0; case ERC_ERR: return 1; + case ERC_SUGGESTIONS: + return 1; } return 1; } @@ -180,9 +172,7 @@ int run_LIST(UserOpt *opt, Conf *conf) { } return 0; } - for (int i = 0; i < opt->argc; i++) { - printf("[%d]: %s\n", i, opt->argv[i]); - } + list_print_lisp(opt->args); close_DB(&db); return 0; } @@ -43,31 +43,6 @@ void free_res(UserOpt *opt, Conf *conf) { } int main(int argc, char *argv[]) { - - cklist *p; - p = list_make_and_add("0"); - list_add(p, "1"); - list_add(p, "2"); - list_add(p, "3"); - list_add(p, "4"); - list_add(p, "5"); - list_add(p, "6"); - list_add(p, "7"); - list_add(p, "8"); - list_print_lisp(p); - cklist *po = list_move(p); - cklist *po_l = list_copy_until(po, 4); - list_print_lisp(po_l); - cklist *po_r = list_copy_from(po, 5); - list_print_lisp(po_r); - cklist *po_part = list_copy_part(po, 2, 4); - list_print_lisp(po_part); - list_print(po); - list_free(po_part); - list_free(po); - list_free(po_l); - list_free(po_r); - UserOpt opt; /* get user opt */ switch(parse_action(argc, argv, &opt)) { diff --git a/src/cklist.c b/src/cklist.c index 32352df..b2a7803 100644 --- a/src/cklist.c +++ b/src/cklist.c @@ -127,25 +127,36 @@ cklist* list_copy_part(cklist *ckl, int from, int until) { } void list_print_lisp(cklist *ckl) { - list_rewind(ckl); - printf("( \"%s\"", list_get(ckl)); - while (list_next(ckl)) { - if (ckl->pos == ckl->size-1) { - printf(", \"%s\" )\n", list_get(ckl)); + if (ckl->size > 0) { + list_rewind(ckl); + printf("( \"%s\"", list_get(ckl)); + if (ckl->size == 1) { + printf(" )\n"); } - else { - printf(", \"%s\"", list_get(ckl)); + while (list_next(ckl)) { + if (ckl->pos == ckl->size-1) { + printf(", \"%s\" )\n", list_get(ckl)); + } + else { + printf(", \"%s\"", list_get(ckl)); + } } + list_rewind(ckl); } - list_rewind(ckl); } void list_print(cklist *ckl) { - list_rewind(ckl); - while (list_next(ckl)) { - printf("%s\n", list_get(ckl)); + if (ckl->size > 0) { + list_rewind(ckl); + while (list_next(ckl)) { + printf("%s\n", list_get(ckl)); + } + list_rewind(ckl); } - list_rewind(ckl); +} + +int list_size(cklist *ckl) { + return ckl->size; } void list_free(cklist *ckl) { diff --git a/src/cklist.h b/src/cklist.h index 9c883b5..e303ebc 100644 --- a/src/cklist.h +++ b/src/cklist.h @@ -13,7 +13,8 @@ * to edit when using ck edit and provides a backend to the ck list action. * * -------------------------------------------------------------------------- */ - +#ifndef CKLIST_H +#define CKLIST_H typedef struct cklist_st cklist; struct cklist_st { int size; @@ -29,7 +30,7 @@ extern void list_rewind(cklist *ckl); extern int list_next(cklist *ckl); extern char* list_get(cklist *ckl); -extern void list_size(cklist *ckl); +extern int list_size(cklist *ckl); /* rewinds */ extern cklist* list_duplicate(cklist *ckl); @@ -54,3 +55,4 @@ extern void list_print(cklist *ckl); /* Deallocate resources */ extern void list_free(cklist *ckl); +#endif /* CKLIST_H */ diff --git a/src/confparser.c b/src/confparser.c index 80120c8..2d17c81 100644 --- a/src/confparser.c +++ b/src/confparser.c @@ -13,6 +13,7 @@ #include <stdio.h> #include "ckutil.h" +#include "cklist.h" #include "confparser.h" @@ -138,14 +139,16 @@ void free_conf(Conf *conf) { } int init_create_config_file(UserOpt *opt) { - char tmp[200]; - if (!util_file_exists(opt->argv[0])) { - printf("Version control directory: %s\ndoes not exist.\n", opt->argv[0]); + list_rewind(opt->args); + + if (!util_file_exists(list_get(opt->args))) { + printf("Version control directory: %s\ndoes not exist.\n", list_get(opt->args)); return 1; } - if (!util_file_exists(opt->argv[1])) { - printf("Secret directory: %s\ndoes not exist.\n", opt->argv[1]); + list_next(opt->args); + if (!util_file_exists(list_get(opt->args))) { + printf("Secret directory: %s\ndoes not exist.\n", list_get(opt->args)); return 1; } @@ -159,17 +162,21 @@ int init_create_config_file(UserOpt *opt) { if ((f = fopen(confName, "w")) == NULL) { return 1; } - + + list_rewind(opt->args); + char tmp[200]; strcpy(tmp, "version_control_dir = "); - strcat(tmp, opt->argv[0]); + strcat(tmp, list_get(opt->args)); strcat(tmp, "\n"); fputs(tmp, f); + list_next(opt->args); strcpy(tmp, "secret_dir = "); - strcat(tmp, opt->argv[1]); + strcat(tmp, list_get(opt->args)); strcat(tmp, "\n"); fputs(tmp, f); - fclose(f); + fclose(f); + list_rewind(opt->args); return 0; } diff --git a/src/engine.c b/src/engine.c deleted file mode 100644 index 681cac6..0000000 --- a/src/engine.c +++ /dev/null @@ -1,62 +0,0 @@ -/* engine.c - manages the linking and copying of configs ---------------*- C -*- - * - * This file is part of ck, the config keeper - * - * ----------------------------------------------------------------------------- - * - * Copyright (C) 2018 Anastasis Grammenos - * GPLv3 (see LICENCE for the full notice) - * - * ----------------------------------------------------------------------------- - * - * - * - * -------------------------------------------------------------------------- */ -#include "engine.h" - -char engine_err[STR_M] = ""; - -int engine_err_message(char *err) { - if (!str_is_empty(engine_err)) { - if (err) { - strcpy(err, engine_err); - } - return 1; - } - return 0; -} - -int copy_config(char *path); - - -void link_config(const AddOpt *opt, const char* newPath) { - printf("Linking %s -> %s\n", newPath, opt->confPath); - if (util_symlink_file(newPath, opt->confPath) != 0) { - strcpy(engine_err, "Could not link file."); - } -} - -void move_config(const AddOpt *opt, const Conf *conf, char *ret) { - char newName[STR_M]; - str_make_ck_config_name(newName, opt->confPath, opt->progName); - char newPath[STR_L]; - str_join_dirname_with_basename(newPath, opt->secret ? conf->SCRT_dir : conf->VC_dir, newName); - printf("Moving %s -> %s\n", opt->confPath, newPath); - strcpy(ret, newPath); - if (util_move_file(opt->confPath, newPath) != 0) { - strcpy(engine_err, "Could not move file."); - } -} - -void engine_add_make_link(const AddOpt *opt, - const Conf *conf) { - char newPath[STR_L]; - move_config(opt, conf, newPath); - if (engine_err_message(NULL)) { - return; - } - link_config(opt, newPath); - if (engine_err_message(NULL)) { - return; - } -} diff --git a/src/engine.h b/src/engine.h deleted file mode 100644 index fc10323..0000000 --- a/src/engine.h +++ /dev/null @@ -1,34 +0,0 @@ -/* engine.h - manages the linking and copying of configs ---------------*- C -*- - * - * This file is part of ck, the config keeper - * - * ----------------------------------------------------------------------------- - * - * Copyright (C) 2018 Anastasis Grammenos - * GPLv3 (see LICENCE for the full notice) - * - * ----------------------------------------------------------------------------- - * - * The functions in here are responsible for manipulating - * the file structure of the system and creating the links. - * - * It makes use of some util functions that wrap the actual - * function calls to linux functions - * - * After a call to an engine_*() function one has to check - * engine_err_message() to check for errors - * - * -------------------------------------------------------------------------- */ -#include "actions.h" -#include "ckutil.h" - -/* if engine_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 engine_err_message(char *err); - - -extern void engine_add_make_link(const AddOpt *opt, - const Conf *conf); - - |