diff options
author | gramanas <anastasis.gramm2@gmail.com> | 2018-10-26 13:52:32 +0300 |
---|---|---|
committer | gramanas <anastasis.gramm2@gmail.com> | 2018-10-26 13:52:32 +0300 |
commit | a6b2b4b2a4095d2f95419df986378e54432c2d71 (patch) | |
tree | ea94ac255bab5bf3665092c42207e8f3229411a2 | |
parent | c3267a360860de225fa0af666d99f60d03266249 (diff) | |
download | ck-a6b2b4b2a4095d2f95419df986378e54432c2d71.tar.gz ck-a6b2b4b2a4095d2f95419df986378e54432c2d71.tar.bz2 ck-a6b2b4b2a4095d2f95419df986378e54432c2d71.zip |
Small corrections
-rw-r--r-- | src/actionhelper.c | 11 | ||||
-rw-r--r-- | src/actions.c | 25 | ||||
-rw-r--r-- | src/ckerrlog.c | 2 | ||||
-rw-r--r-- | src/ckerrlog.h | 8 | ||||
-rw-r--r-- | src/confparser.c | 75 | ||||
-rw-r--r-- | src/confparser.h | 12 | ||||
-rw-r--r-- | src/dblayer.c | 2 |
7 files changed, 50 insertions, 85 deletions
diff --git a/src/actionhelper.c b/src/actionhelper.c index 6e76b2b..ad259a2 100644 --- a/src/actionhelper.c +++ b/src/actionhelper.c @@ -102,6 +102,10 @@ DelOpt del_make_options(cklist *args) { delOpt.arg = list_get(args); } else { delOpt.arg = list_get(args); + if (list_next(args)) { + delOpt.err = DEL_ERR_WRONG_ARGS; + return delOpt; + } } list_rewind(args); @@ -271,7 +275,7 @@ void print_ADD_result(int err) { HELP("ckdb updated succesfully."); return; } - ERR("Could not complete add transaction."); + sERR("Could not complete add transaction."); } void print_DEL_result(int err) { @@ -279,7 +283,10 @@ void print_DEL_result(int err) { HELP("ckdb updated succesfully."); return; } - ERR("Could not complete delete transaction."); + if (err == -1) { + sERR("Could not complete delete transaction."); + return; + } } void print_EDIT_result(int err) { diff --git a/src/actions.c b/src/actions.c index 4fc033e..b887399 100644 --- a/src/actions.c +++ b/src/actions.c @@ -24,7 +24,7 @@ int run_INIT(UserOpt * opt, Conf *conf) { return -1; } if (init_create_config_file(opt)) { - HELP("Cound not create config file."); + sERR("Cound not create config file."); return -2; } DB db = init_make_DB(opt); @@ -73,29 +73,28 @@ int run_ADD(UserOpt * opt, Conf *conf) { int run_DEL(UserOpt * opt, Conf *conf) { UNUSED(conf); - DB db = open_DB(opt); - if (db.ptr == NULL) { - if (db.error == SQL_ERR_NO_TABLES) { - ERR("The database file is currupted. Run ck init anew."); - } - goto error; - } + DB db; DelOpt delOpt = del_make_options(opt->args); switch(delOpt.err) { case DEL_NO_ERR: - del_transaction_try(&db, delOpt.arg, delOpt.isConf); + db = open_DB(opt); + if (db.ptr == NULL) { + if (db.error == SQL_ERR_NO_TABLES) { + ERR("The database file is currupted. Run ck init anew."); + } + return -2; + } + int rc = del_transaction_try(&db, delOpt.arg, delOpt.isConf); close_DB(&db); - return 0; + return rc; case DEL_ERR_WRONG_ARGS: ERR("Wrong delete arguments."); break; case DEL_ERR_WRONG_PATH: ERR("Path %s doesnt exist.", delOpt.arg); } - error: - close_DB(&db); - return 1; + return -2; } int run_EDIT(UserOpt *opt, Conf *conf) { diff --git a/src/ckerrlog.c b/src/ckerrlog.c index 0d0c30d..a023521 100644 --- a/src/ckerrlog.c +++ b/src/ckerrlog.c @@ -22,7 +22,7 @@ static char buf[STR_M]; CK_STREAMS #undef X -char * get_time() { +char *get_time() { time_t rawtime; struct tm * timeinfo; time (&rawtime); diff --git a/src/ckerrlog.h b/src/ckerrlog.h index 66923cb..aa5840b 100644 --- a/src/ckerrlog.h +++ b/src/ckerrlog.h @@ -74,7 +74,13 @@ CK_STREAMS #define ERR(...) \ ckerr_with_delim("\n", "Error in [%s]:", COMPONENT); \ ckerr(__VA_ARGS__); \ - cklog_with_delim(" ", "ERROR: [%s]", COMPONENT); \ + cklog_with_delim(" ", "ERROR: [%s]", COMPONENT); \ + cklog(__VA_ARGS__); \ + report_err(); + +#define sERR(...) \ + ckerr(__VA_ARGS__); \ + cklog_with_delim(" ", "ERROR: [%s]", COMPONENT); \ cklog(__VA_ARGS__); \ report_err(); diff --git a/src/confparser.c b/src/confparser.c index 4599a84..6dea001 100644 --- a/src/confparser.c +++ b/src/confparser.c @@ -23,16 +23,6 @@ void conf_values_initialize(Conf *c) { #undef X } -int remove_newline(char buff[]) { - int initLength = strlen(buff); - for (int i = 0; i < initLength; i++) { - if (buff[i] == '\n') { - buff[i] = ' '; - } - } - return strlen(buff); -} - int read_next_line(char *line, FILE *f) { char nextLine[STR_L]; if (fgets(nextLine, STR_L, f) == NULL) { @@ -63,70 +53,45 @@ void make_config_name(char * ret, const char *confPath) { strcpy(ret, tmp); } -ConfigParserResult parse(Conf *conf, UserOpt *opt) { +int config_file_parse(Conf *conf, UserOpt *opt) { + LOG("Using '%s' for ck configuration directory", opt->confDir); conf_values_initialize(conf); FILE *confPtr; char confName[STR_L]; + char line[STR_L]; + char matched[STR_L]; + make_config_name(confName, opt->confDir); if ((confPtr = fopen(confName, "r")) == NULL) { ERR("%s is not readable, check the permissions.", confName) - return CPR_NO_CONFIG_FILE; + return -1; } - int flag = 1; - char line[STR_L]; - char matched[STR_L]; while (read_next_line(line, confPtr) == 0) { if (strlen(line) > STR_L) { - return CPR_WRONG_CONFIG; + return -1; } switch(match_variables(line, matched)) { -#define X(var, str, name) \ - case CV_##var: \ - conf->var = malloc(strlen(matched)+1); \ - strcpy(conf->var, matched); \ - if (!util_is_dir(matched)) { \ - return CPR_WRONG_##var; \ - } \ - LOG("Found %s: %s", name, conf->var); \ +#define X(var, str, name) \ + case CV_##var: \ + conf->var = malloc(strlen(matched)+1); \ + strcpy(conf->var, matched); \ + if (!util_is_dir(matched)) { \ + ERR("%s %s defined in config doesn't exist", \ + name, conf->var); \ + return -1; \ + } \ + LOG("Found %s: %s", name, conf->var); \ break; CONFIG_VARIABLES_TABLE #undef X case CV_NO_VAL_OR_COMMENT: break; default: - if (line[strlen(line) - 1] == '\n') { - ERR("Config error in line: %.*s", strlen(line) - 1, line); - } - else { - ERR("Config error in line: %s", line); - } - flag = 0; + ERR("In line: %s", line); + return -1; } } - - if (flag) { - return CPR_OK; - } - return CPR_WRONG_CONFIG; -} - -int config_file_parse(Conf *conf, UserOpt *opt) { - LOG("Using '%s' for ck configuration directory", opt->confDir); - switch (parse(conf, opt)) { -#define X(var,str,name) \ - case CPR_WRONG_##var: \ - ERR("%s %s defined in config doesn't exist", name, conf->var); \ - return -1; \ - break; - CONFIG_VARIABLES_TABLE -#undef X - case CPR_OK: - return 0; - case CPR_NO_CONFIG_FILE: - case CPR_WRONG_CONFIG: - break; - } - return -1; + return 0; } void free_conf(Conf *conf) { diff --git a/src/confparser.h b/src/confparser.h index 261dc72..94541c6 100644 --- a/src/confparser.h +++ b/src/confparser.h @@ -30,20 +30,8 @@ enum ConfingVariables { }; typedef enum ConfingVariables ConfVar; -enum ConfigParserResults { - CPR_OK, - CPR_NO_CONFIG_FILE, - CPR_WRONG_CONFIG, -#define X(var, str, name) \ - CPR_WRONG_##var, - CONFIG_VARIABLES_TABLE -#undef X -}; -typedef enum ConfigParserResults ConfigParserResult; - typedef struct ConfigValues Conf; struct ConfigValues { - ConfigParserResult result; #define X(var, str, name) char* var; CONFIG_VARIABLES_TABLE #undef X diff --git a/src/dblayer.c b/src/dblayer.c index 0a59938..8703c1e 100644 --- a/src/dblayer.c +++ b/src/dblayer.c @@ -828,7 +828,7 @@ int del_transaction_try(DB *db, char *arg, int conf) { ERR("Program not found in the db."); return -1; } - /* If we are deleting a proram we should delete everything that + /* If we are deleting a program we should delete everything that * refferences it (configs and relationships) */ if (!conf) { remove_all_configs(db, pid); |