diff options
author | gramanas <anastasis.gramm2@gmail.com> | 2018-09-22 12:58:22 +0300 |
---|---|---|
committer | gramanas <anastasis.gramm2@gmail.com> | 2018-09-22 12:58:22 +0300 |
commit | 405afc36e0929d887188be09a5886f79ff0ea1c4 (patch) | |
tree | f31d36ad4229f207e7e7bfbfe679516295e82952 | |
parent | fe7c216a75ad1a5890c329df73f1d5ab461a1257 (diff) | |
download | ck-405afc36e0929d887188be09a5886f79ff0ea1c4.tar.gz ck-405afc36e0929d887188be09a5886f79ff0ea1c4.tar.bz2 ck-405afc36e0929d887188be09a5886f79ff0ea1c4.zip |
Standarize error codes
-rw-r--r-- | src/actionhelper.c | 28 | ||||
-rw-r--r-- | src/actionhelper.h | 2 | ||||
-rw-r--r-- | src/actionparser.c | 2 | ||||
-rw-r--r-- | src/actions.c | 44 | ||||
-rw-r--r-- | src/ck.c | 27 | ||||
-rw-r--r-- | src/dblayer.c | 14 | ||||
-rw-r--r-- | src/dblayer.h | 3 |
7 files changed, 55 insertions, 65 deletions
diff --git a/src/actionhelper.c b/src/actionhelper.c index 5007b62..9710ef8 100644 --- a/src/actionhelper.c +++ b/src/actionhelper.c @@ -160,53 +160,53 @@ ListOpt list_make_options(cklist *args) { /* PRINT RESULTS */ /*****************/ -void print_INIT_result(int ok) { - if (ok) { +void print_INIT_result(int err) { + if (!err) { printf("Initialized empty ckdb.\n"); } } -void print_ADD_result(int ok) { - if (ok) { +void print_ADD_result(int err) { + if (!err) { printf("ckdb updated succesfully.\n"); return; } printf("Could not complete add transaction.\n"); } -void print_DEL_result(int ok) { - if (ok) { +void print_DEL_result(int err) { + if (!err) { printf("succes\n"); return; } printf("Not Supported\n"); } -void print_EDIT_result(int ok) { - if (ok) { +void print_EDIT_result(int err) { + if (!err) { printf("succes\n"); return; } printf("failure\n"); } -void print_LIST_result(int ok) { - if (ok) { +void print_LIST_result(int err) { + if (!err) { return; } printf("Wrong list arguments\n"); } -void print_SEARCH_result(int ok) { - if (ok) { +void print_SEARCH_result(int err) { + if (!err) { printf("succes\n"); return; } printf("Not Supported\n"); } -void print_HELP_result(int ok) { - if (ok) { +void print_HELP_result(int err) { + if (!err) { printf("succes\n"); return; } diff --git a/src/actionhelper.h b/src/actionhelper.h index 3393ec3..3dea765 100644 --- a/src/actionhelper.h +++ b/src/actionhelper.h @@ -51,7 +51,7 @@ extern ListOpt list_make_options(cklist *args); /* PRINT RESULTS */ /*****************/ #define X(ACTION) \ - extern void print_##ACTION##_result(int ok); + extern void print_##ACTION##_result(int err); CK_ACTIONS #undef X diff --git a/src/actionparser.c b/src/actionparser.c index de162c2..a1fd1ef 100644 --- a/src/actionparser.c +++ b/src/actionparser.c @@ -269,7 +269,7 @@ void print_parser_error(UserOpt *opt) { sprintf(errStr, "Edit config with $EDITOR\nUsage: %s ProgramName or configBasename (or both)", names); break; case PERR_LIST_WRONG: - sprintf(errStr, "List programs, configs and more\nUsage: %s [list-type] value-to-list", names); + sprintf(errStr, "List programs, configs and more\nUsage: %s value-to-list (or tree) [-t list-type]", names); break; case PERR_SEARCH_WRONG: sprintf(errStr, "Usage: ............."); diff --git a/src/actions.c b/src/actions.c index 8f89484..e75c885 100644 --- a/src/actions.c +++ b/src/actions.c @@ -22,17 +22,17 @@ int run_INIT(UserOpt * opt, Conf *conf) { if (db_exists(opt)) { printf("Current configuration file location: %s\n", opt->confDir); PRINT_ERR("ck is already initialized."); - return 0; + return 1; } if (init_create_config_file(opt)) { - return 0; + return 1; } DB db = init_make_DB(opt); if (db.error == SQL_NO_ERR) { init_make_tables(&db); } sqlite3_close(db.ptr); - return 1; + return 0; } int run_ADD(UserOpt * opt, Conf *conf) { @@ -41,7 +41,7 @@ int run_ADD(UserOpt * opt, Conf *conf) { if (db.error == SQL_ERR_NO_TABLES) { PRINT_ERR("The database file is currupted. Run ck init anew."); } - return 0; + return 1; } AddOpt addOpt = add_make_options(opt->args); switch (addOpt.err) { @@ -50,30 +50,31 @@ int run_ADD(UserOpt * opt, Conf *conf) { case ADD_ERR_WRONG_CONFIG: PRINT_ERR("The config file specified doesn't exist."); close_DB(&db); - return 0; + return 1; case ADD_ERR_WRONG_FLAGS: PRINT_ERR("Flags are: -s for secret and -p for primary."); close_DB(&db); - return 0; + return 1; } add_print_opts(&addOpt); - if (add_transaction_begin(&db, &addOpt) == 0) { + /* Try adding the new config to the DB */ + if (add_transaction_try(&db, &addOpt)) { close_DB(&db); - return 0; + return 1; } close_DB(&db); add_make_link(&addOpt, conf); char err[STR_M]; if (add_err_message(err)) { PRINT_ERR(err); - return 0; + return 1; } - return 1; + return 0; } int run_DEL(UserOpt * opt, Conf *conf) { printf("Running %s\n", "del"); - return 0; + return 1; } int run_EDIT(UserOpt *opt, Conf *conf) { @@ -83,7 +84,7 @@ int run_EDIT(UserOpt *opt, Conf *conf) { if (db.error == SQL_ERR_NO_TABLES) { PRINT_ERR("The database file is currupted. Run ck init anew."); } - return 0; + return 1; } list_rewind(opt->args); @@ -97,7 +98,7 @@ int run_EDIT(UserOpt *opt, Conf *conf) { } else { PRINT_ERR("No primary config"); close_DB(&db); - return 0; + return 1; } } else { close_DB(&db); @@ -110,7 +111,6 @@ int run_EDIT(UserOpt *opt, Conf *conf) { case ERC_SUGGESTIONS: return 1; } - return 1; } close_DB(&db); @@ -120,7 +120,7 @@ int run_EDIT(UserOpt *opt, Conf *conf) { strcat(command, " "); strcat(command, confPath); system(command); - return 1; + return 0; } int run_LIST(UserOpt *opt, Conf *conf) { @@ -130,7 +130,7 @@ int run_LIST(UserOpt *opt, Conf *conf) { if (db.error == SQL_ERR_NO_TABLES) { printf("no tables\n"); } - return 0; + return 1; } cklist *list_type = list_make_new(); @@ -139,7 +139,7 @@ int run_LIST(UserOpt *opt, Conf *conf) { if (listOpt.err) { close_DB(&db); list_free(list_type); - return 0; + return 1; } switch(listOpt._lt) { case LT_PATH: @@ -153,12 +153,12 @@ int run_LIST(UserOpt *opt, Conf *conf) { list_print(list_type); close_DB(&db); list_free(list_type); - return 1; + return 0; case LT_NONE: printf("What should I list? (paths, configs)\n"); close_DB(&db); list_free(list_type); - return 0; + return 1; } switch(listOpt._lst) { case LST_PLAIN: @@ -172,17 +172,17 @@ int run_LIST(UserOpt *opt, Conf *conf) { } close_DB(&db); list_free(list_type); - return 1; + return 0; } int run_SEARCH(UserOpt *opt, Conf *conf) { printf("Running %s\n", "search"); DB db = open_DB(opt); close_DB(&db); - return 0; + return 1; } int run_HELP(UserOpt *opt, Conf *conf) { printf("Running %s\n", "help"); - return 0; + return 1; } @@ -33,45 +33,32 @@ #include "cklist.h" #include "ckutil.h" -void free_res(UserOpt *opt, Conf *conf) { - if (opt) { - free_user_opt(opt); - } - if (conf) { - free_conf(conf); - } -} - int main(int argc, char *argv[]) { UserOpt opt; + Conf conf = {.VC_dir = NULL, .SCRT_dir = NULL}; /* get user opt */ switch(parse_action(argc, argv, &opt)) { case APR_HELP: - free_res(&opt, NULL); print_parser_help(); - return 0; + goto error; case APR_ERR: print_parser_error(&opt); - free_res(&opt, NULL); - return 1; + goto error; case APR_OK: break; } - Conf conf = {.VC_dir = NULL, .SCRT_dir = NULL}; /* If the action is init don't load the config, skip to running init*/ if (opt.action != CKA_INIT) { /* If the db doesn't exist ck is not initialized in the config * location specified in opt */ if (!db_exists(&opt)) { printf("ck is not initialized in %s.\nRun ck init first.\n", opt.confDir); - free_res(&opt, NULL); - return 1; + goto error; } /* Finally parse the config file and exit on error */ if (!config_file_parse(&conf, &opt)) { - free_res(&opt, &conf); - return 1; + goto error; } } @@ -86,6 +73,8 @@ int main(int argc, char *argv[]) { default: break; } - free_res(&opt, &conf); + error: + free_user_opt(&opt); + free_conf(&conf); return 0; } diff --git a/src/dblayer.c b/src/dblayer.c index 190cb05..dbd772e 100644 --- a/src/dblayer.c +++ b/src/dblayer.c @@ -356,34 +356,34 @@ int add_get_or_insert_program_to_db(DB *db, const char *name) { return pid; } -int add_transaction_begin(DB *db, const AddOpt * const opt) { +int add_transaction_try(DB *db, const AddOpt * const opt) { __BEGIN_TRANSACTION__ int pid = add_get_or_insert_program_to_db(db, opt->progName); if (db->error == SQL_ERR_SQLITE) { PRINT_ERR("Could not insert program to db.\n"); - return 0; + return 1; } int cid = add_get_or_insert_config_to_db(db, pid, opt->confPath, opt->secret, opt->prime); if (db->error == SQL_ERR_SQLITE) { PRINT_ERR("Could not insert config to db.\n"); - return 0; + return 1; } else if (db->error == SQL_CONFIG_PATH_EXISTS) { PRINT_ERR("This config already exists in the database.\n"); - return 0; + return 1; } else if (db->error == SQL_ERR_PRIMARY_REDEFINITION) { PRINT_ERR("This program already has a primary config.\n"); - return 0; + return 1; } add_insert_relationship(db, pid, cid); if (db->error == SQL_ERR_SQLITE) { PRINT_ERR("rel update failed\n"); - return 0; + return 1; } __END_TRANSACTION__ - return 1; + return 0; } int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secret) { diff --git a/src/dblayer.h b/src/dblayer.h index 620d3e6..da54632 100644 --- a/src/dblayer.h +++ b/src/dblayer.h @@ -55,7 +55,8 @@ extern DB init_make_DB(const UserOpt *opt); /* add */ /*******/ -extern int add_transaction_begin(DB *db, const AddOpt * const opt); +/* Returns 1 in error, 0 otherwise */ +extern int add_transaction_try(DB *db, const AddOpt * const opt); /********/ /* edit */ |