diff options
-rw-r--r-- | src/actionhelper.c | 35 | ||||
-rw-r--r-- | src/actions.c | 71 | ||||
-rw-r--r-- | src/ck.c | 10 |
3 files changed, 55 insertions, 61 deletions
diff --git a/src/actionhelper.c b/src/actionhelper.c index 5899024..d1891c9 100644 --- a/src/actionhelper.c +++ b/src/actionhelper.c @@ -307,13 +307,13 @@ int restore_make_links(cklist *from, cklist *to) { void print_INIT_result(int err) { if (!err) { - HELP("Initialized empty ckdb."); + hLOG("Initialized empty ckdb."); } } void print_ADD_result(int err) { if (!err) { - HELP("ckdb updated succesfully."); + hLOG("ckdb updated succesfully."); return; } sERR("Could not complete add transaction."); @@ -321,47 +321,30 @@ void print_ADD_result(int err) { void print_DEL_result(int err) { if (!err) { - HELP("ckdb updated succesfully."); - return; - } - if (err == -1) { - sERR("Could not complete delete transaction."); + hLOG("ckdb updated succesfully."); return; } + sERR("Could not complete delete transaction."); } void print_EDIT_result(int err) { - if (!err) { - return; - } + UNUSED(err); } void print_LIST_result(int err) { - if (!err) { - return; - } + UNUSED(err); } void print_SEARCH_result(int err) { - if (err == 2) { - ERR("No grep avaliable. Please make sure you have grep installed."); - return; - } - if (!err) { - return; - } - ERR("Wrong search."); + UNUSED(err); } void print_HELP_result(int err) { - if (!err) { - } + UNUSED(err); } void print_RESTORE_result(int err) { - if (err == -1) { - sERR("Restore failed.") - } + UNUSED(err); } void print_INIT_help() { diff --git a/src/actions.c b/src/actions.c index ddfe9b9..36e3a42 100644 --- a/src/actions.c +++ b/src/actions.c @@ -25,7 +25,7 @@ int run_INIT(UserOpt * opt, Conf *conf) { } if (init_create_config_file(opt)) { sERR("Cound not create config file."); - return -2; + return -1; } DB db = init_make_DB(opt); if (db.error == SQL_NO_ERR) { @@ -65,7 +65,7 @@ int run_ADD(UserOpt * opt, Conf *conf) { PRINT_ERR(err); error: close_DB(&db); - return 1; + return -1; } close_DB(&db); return 0; @@ -83,7 +83,7 @@ int run_DEL(UserOpt * opt, Conf *conf) { if (db.error == SQL_ERR_NO_TABLES) { ERR("The database file is currupted. Run ck init anew."); } - return -2; + return -1; } int rc = del_transaction_try(&db, delOpt.arg, delOpt.isConf); close_DB(&db); @@ -94,7 +94,7 @@ int run_DEL(UserOpt * opt, Conf *conf) { case DEL_ERR_WRONG_PATH: ERR("Path %s doesnt exist.", delOpt.arg); } - return -2; + return -1; } int run_EDIT(UserOpt *opt, Conf *conf) { @@ -103,7 +103,7 @@ int run_EDIT(UserOpt *opt, Conf *conf) { if (db.error == SQL_ERR_NO_TABLES) { ERR("The database file is currupted. Run ck init anew."); } - return 1; + return -1; } list_rewind(opt->args); @@ -146,18 +146,29 @@ int run_EDIT(UserOpt *opt, Conf *conf) { } close_DB(&db); str_join_dirname_with_basename(confPath, secret ? conf->scrt_dir : conf->vc_dir, confName); - HELP("Editing %s", confPath); char *editor = getenv("EDITOR"); char command[STR_L]; - strcpy(command, str_is_empty(editor) ? "nano" : editor); + if (str_is_empty(editor)) { + if (system("which nano > /dev/null 2>&1") != 0) { + ERR("Nano not found. Please set $EDITOR to your desired editor."); + return -1; + } + strcpy(command, "nano"); + } else { + strcpy(command, editor); + } + + strcat(command, " "); strcat(command, confPath); + + HELP("editing...\n%s", command); system(command); return 0; error: close_DB(&db); - return 1; + return -1; } int run_LIST(UserOpt *opt, Conf *conf) { @@ -166,7 +177,7 @@ int run_LIST(UserOpt *opt, Conf *conf) { if (db.error == SQL_ERR_NO_TABLES) { ERR("The database file is currupted. Run ck init anew."); } - return 1; + return -1; } cklist *the_list = list_make_new(); @@ -227,15 +238,23 @@ int run_LIST(UserOpt *opt, Conf *conf) { error: close_DB(&db); list_free(the_list); - return 1; + return -1; } int run_SEARCH(UserOpt *opt, Conf *conf) { UNUSED(conf); - if (system("which grep > /dev/null") != 0) { - return 2; + if (system("which grep > /dev/null 2>&1") != 0) { + ERR("No grep avaliable. Please make sure you have grep installed."); + return -1; } 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."); + } + return -1; + } + cklist *paths = list_make_new(); list_get_paths(&db, paths, 0 /*basename*/, 0/*attributes*/); close_DB(&db); @@ -250,7 +269,7 @@ int run_SEARCH(UserOpt *opt, Conf *conf) { cmd = popen(command, "r"); if (cmd == NULL) { list_free(paths); - return 1; + return -1; } while (fgets(result, sizeof(result), cmd)) { printf("%s", result); @@ -265,17 +284,6 @@ int run_SEARCH(UserOpt *opt, Conf *conf) { int run_HELP(UserOpt *opt, Conf *conf) { UNUSED(conf); char tmp[STR_M]; - if (strcmp(list_get(opt->args), "man") == 0) { -#define X(ACTION) \ - get_possible_action_strings(tmp, CKA_##ACTION); \ - HELP("%s:\n%s", #ACTION, tmp); \ - print_##ACTION##_help(); \ - HELP(""); - CK_ACTIONS -#undef X - return 0; - } - if (strcmp(list_get(opt->args), "config") == 0) { print_conf_help(); return 0; @@ -298,7 +306,7 @@ int run_HELP(UserOpt *opt, Conf *conf) { default: ERR("Unknown action: %s", list_get(opt->args)); } - return 1; + return -1; } int run_RESTORE(UserOpt *opt, Conf *conf) { @@ -308,7 +316,7 @@ int run_RESTORE(UserOpt *opt, Conf *conf) { if (db.error == SQL_ERR_NO_TABLES) { ERR("The database file is currupted. Run ck init anew."); } - return 1; + return -1; } cklist *from = list_make_new(); cklist *to = list_make_new(); @@ -352,13 +360,14 @@ int run_RESTORE(UserOpt *opt, Conf *conf) { err_flag = 1; } close_DB(&db); + int rc = -1; if (!err_flag) { - int rc = restore_make_links(from, to); - list_free(from); - list_free(to); - return rc; + rc = restore_make_links(from, to); + if (rc) { + sERR("Restore failed."); + } } list_free(from); list_free(to); - return -2; + return rc; } @@ -39,6 +39,7 @@ ERRLOG(main); int main(int argc, const char **argv) { UserOpt opt; Conf conf; + int rc = -1; initialize_errlog(argc, argv); initialize_conf(&conf); @@ -64,9 +65,10 @@ int main(int argc, const char **argv) { /* Run action and print results */ switch(opt.action) { -#define X(ACTION) \ - case CKA_##ACTION: \ - print_##ACTION##_result(run_##ACTION(&opt, &conf)); \ +#define X(ACTION) \ + case CKA_##ACTION: \ + rc = run_##ACTION(&opt, &conf); \ + print_##ACTION##_result(rc); \ break; CK_ACTIONS #undef X @@ -77,5 +79,5 @@ int main(int argc, const char **argv) { free_user_opt(&opt); free_conf(&conf); report_errlog(); - return 0; + return rc; } |