aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/actions.c416
-rw-r--r--src/actions.h25
-rw-r--r--src/add.c57
-rw-r--r--src/ck.c3
-rw-r--r--src/ckutil.c1
-rw-r--r--src/clparser.c6
-rw-r--r--src/confparser.c1
-rw-r--r--src/dblayer.c2
-rw-r--r--src/dblayer.h20
-rw-r--r--src/delete.c56
-rw-r--r--src/edit.c94
-rw-r--r--src/help.c53
-rw-r--r--src/init.c40
-rw-r--r--src/list.c94
-rw-r--r--src/queries.c1
-rw-r--r--src/queries.h3
-rw-r--r--src/restore.c83
-rw-r--r--src/search.c54
18 files changed, 512 insertions, 497 deletions
diff --git a/src/actions.c b/src/actions.c
deleted file mode 100644
index 3293e53..0000000
--- a/src/actions.c
+++ /dev/null
@@ -1,416 +0,0 @@
-/* actions.c - 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 "dblayer.h"
-#include "cklist.h"
-#include "ckerrlog.h"
-
-ERRLOG(action);
-
-int run_INIT(UserOpt * opt, Conf *conf) {
- UNUSED(conf);
- if (db_exists(opt)) {
- ERR("ck is already initialized in %s", opt->confDir);
- return -1;
- }
- if (init_create_config_file(opt)) {
- sERR("Cound not create config file.");
- return -1;
- }
- DB db;
- if (open_DB(&db, opt)) {
- return -1;
- }
- init_make_tables(&db);
- sqlite3_close(db.ptr);
- hLOG("Initialized empty ckdb.");
- return 0;
-}
-
-int run_ADD(UserOpt * opt, Conf *conf) {
- DB db;
- if (open_DB(&db, opt)) {
- return -1;
- }
- AddOpt addOpt = add_make_options(opt->args);
- switch (addOpt.err) {
- case ADD_NO_ERR:
- break;
- case ADD_ERR_WRONG_CONFIG:
- ERR("The config file specified doesn't exist or is a link.");
- goto error;
- case ADD_ERR_WRONG_FLAGS:
- ERR("Flags are: -s for secret and -p for primary.");
- goto error;
- }
- add_print_opts(&addOpt);
- /* Try adding the new config to the DB */
- if (add_transaction_try(&db, &addOpt, conf->home_dir)) {
- goto error;
- }
- if (add_make_link(&addOpt, conf)) {
- error:
- close_DB(&db);
- sERR("Could not complete add transaction.");
- return -1;
- }
- close_DB(&db);
- hLOG("ckdb updated succesfully.");
- return 0;
-}
-
-int run_DEL(UserOpt * opt, Conf *conf) {
- UNUSED(conf);
- DB db;
- if (open_DB(&db, opt)) {
- return -1;
- }
-
- int rc = -1;
- /* Since we are here, args have to be 1 or 2 */
- char *pName = list_get(opt->args);
- if (!program_exists(&db, pName)) {
- ERR("Program %s doesn't exist in the database.", pName);
- goto error;
- }
-
- /* If there is no next argument */
- if (!list_next(opt->args)) {
- rc = del_transaction_try(&db, pName, NULL);
- }
- /* If there are more arguments */
- else {
- char *cName = list_get(opt->args);
- rc = del_transaction_try(&db, pName, cName);
- if (rc) {
- HELP("Program %s doesn't have a config named %s", pName, cName);
- print_suggested_configs(&db, pName);
- }
- }
- error:
- close_DB(&db);
- if (!rc) {
- hLOG("ckdb updated succesfully.");
- } else {
- sERR("Could not complete delete transaction.");
- }
- return rc;
-}
-
-int run_EDIT(UserOpt *opt, Conf *conf) {
- DB db;
- if (open_DB(&db, opt)) {
- return -1;
- }
-
- list_rewind(opt->args);
- char confPath[STR_L] = "";
- char confName[STR_M] = "";
- int secret = 0;
- /* Since we are here, args have to be 1 or 2 */
- char *pName = list_get(opt->args);
- if (!program_exists(&db, pName)) {
- ERR("Program %s doesn't exist in the database.", pName);
- goto error;
- }
- /* If there is no next argument */
- if (!list_next(opt->args)) {
- /* If there is no primary config*/
- if (edit_get_prime_config_from_program(&db, pName, confName, &secret) == -1) {
- /* If the program has only one config */
- if (get_config_number(&db, pName) == 1) {
- if (edit_get_config(&db, pName, confName, NULL, &secret)) {
- ERR("Coudln't find config file for %s", pName);
- goto error;
- }
- }
- /* If the program has many configs */
- else {
- HELP("Ambiguous config. Please type the config name after the program.");
- print_suggested_configs(&db, pName);
- goto error;
- }
- }
- }
- /* If there are more arguments */
- else {
- char *cName = list_get(opt->args);
- if (edit_get_config(&db, pName, confName, cName, &secret)) {
- ERR("Program %s doesn't have a config named %s", pName, cName);
- print_suggested_configs(&db, pName);
- goto error;
- }
- }
- close_DB(&db);
- str_join_dirname_with_basename(confPath, secret ? conf->scrt_dir : conf->vc_dir, confName);
-
- char *editor = getenv("EDITOR");
- char command[STR_L] = "";
- 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;
-}
-
-int run_LIST(UserOpt *opt, Conf *conf) {
- DB db;
- if (open_DB(&db, opt)) {
- return -1;
- }
-
- cklist *the_list = list_make_new();
- ListOpt listOpt = list_make_options(opt->args);
-
- if (listOpt.err) {
- ERR("Wrong list options.");
- goto error;
- }
- char tmp[STR_L] = "";
- switch(listOpt._lt) {
- case LT_PATH:
- list_get_paths(&db, the_list, listOpt.bName, listOpt.attr, conf->home_dir);
- break;
- case LT_PROGRAM:
- list_get_programs(&db, the_list);
- break;
- case LT_TREE:
- list_get_path_program_tree(&db, the_list, listOpt.bName, listOpt.attr, conf->home_dir);
- list_print(the_list);
- goto close;
- case LT_CKCONF:
- strcat(tmp, "ck configuration directory path: ");
- strcat(tmp, opt->confDir);
- list_add(the_list, tmp);
-#define X(var, str, name) \
- strcpy(tmp, ""); \
- strcat(tmp, name); \
- strcat(tmp, ": "); \
- strcat(tmp, conf->var); \
- list_add(the_list, tmp);
- CONFIG_VARIABLES_TABLE;
-#undef X
- list_print(the_list);
- goto close;
- case LT_PROG_CONFS:
- if (!program_exists(&db, listOpt.pName)) {
- ERR("Program %s doesn't exist in the database.", listOpt.pName);
- goto error;
- }
- get_program_paths(&db, the_list, listOpt.pName, listOpt.bName, listOpt.attr, conf->home_dir);
- break;
- }
- switch(listOpt._lst) {
- case LST_PLAIN:
- list_print(the_list);
- break;
- case LST_LISP:
- list_print_lisp(the_list);
- break;
- case LST_PYTHON:
- list_print_python(the_list);
- }
- close:
- close_DB(&db);
- list_free(the_list);
- return 0;
- error:
- close_DB(&db);
- list_free(the_list);
- return -1;
-}
-
-int run_SEARCH(UserOpt *opt, Conf *conf) {
- if (system("which grep > /dev/null 2>&1") != 0) {
- ERR("No grep avaliable. Please make sure you have grep installed.");
- return -1;
- }
- DB db;
- if (open_DB(&db, opt)) {
- return -1;
- }
-
- cklist *paths = list_make_new();
- list_get_paths(&db, paths, 0 /*basename*/, 0/*attributes*/, conf->home_dir);
- close_DB(&db);
- if (list_size(paths) && list_size(opt->args)) {
- do {
- FILE *cmd;
- char result[STR_L] = "";
- char command[STR_L] = "grep -H -n \"";
- strcat(command, list_get(opt->args));
- strcat(command, "\" ");
- strcat(command, list_get(paths));
- cmd = popen(command, "r");
- if (cmd == NULL) {
- list_free(paths);
- return -1;
- }
- while (fgets(result, sizeof(result), cmd)) {
- printf("%s", result);
- }
- pclose(cmd);
- } while(list_next(paths));
- }
- list_free(paths);
- return 0;
-}
-
-int run_HELP(UserOpt *opt, Conf *conf) {
- UNUSED(conf);
- char tmp[STR_M] = "";
- if (strcmp(list_get(opt->args), "config") == 0) {
- print_conf_help();
- return 0;
- }
-
- if (strcmp(list_get(opt->args), "verbose") == 0) {
- print_verbose_help();
- return 0;
- }
-
- switch(parser_get_action(list_get(opt->args), NULL)) {
-#define X(ACTION, MIN, MAX) \
- case CKA_##ACTION: \
- HELP("%s", get_possible_action_strings(tmp, CKA_##ACTION)); \
- print_##ACTION##_help(); \
- return 0;
- CK_ACTIONS
-#undef X
- default:
- ERR("Unknown action: %s", list_get(opt->args));
- }
- return -1;
-}
-
-int run_RESTORE(UserOpt *opt, Conf *conf) {
- DB db;
- if (open_DB(&db, opt)) {
- return -1;
- }
- cklist *from = list_make_new();
- cklist *to = list_make_new();
- int err_flag = 0;
- if (strcmp(list_get(opt->args), "-p") == 0) {
- if (list_next(opt->args)) {
- if (program_exists(&db, list_get(opt->args))) {
- if (restore_configs_exists(&db, conf, list_get(opt->args), from, to)) {
- hLOG("Restoring links for %s...", list_get(opt->args));
- }
- else {
- err_flag = 1;
- }
- }
- else {
- ERR("Program %s does not exist", list_get(opt->args));
- err_flag = 1;
- }
- }
- else {
- sERR("-p needs a program name");
- err_flag = 1;
- }
- }
- else if (strcmp(list_get(opt->args), "all") == 0) {
- if (!list_next(opt->args)) {
- if (restore_all_exist(&db, conf, from, to)) {
- hLOG("Restoring all links...");
- }
- else {
- err_flag = 1;
- }
- }
- else {
- sERR("Wrong argument");
- err_flag = 1;
- }
- }
- else {
- sERR("Wrong argument");
- err_flag = 1;
- }
- close_DB(&db);
- int rc = -1;
- if (!err_flag) {
- rc = restore_make_links(from, to);
- if (rc) {
- sERR("Restore failed.");
- }
- }
- list_free(from);
- list_free(to);
- return rc;
-}
-
-/**************/
-/* PRINT HELP */
-/**************/
-
-void print_INIT_help() {
- HELP("ck init VERSION_CONTROL_DIR SECRET_DIR");
-}
-
-void print_ADD_help() {
- HELP("ck add PROGRAM_NAME CONFIG_PATH [-p] [-s]");
-}
-
-void print_DEL_help() {
- HELP("ck delete PROGRAM_NAME [CONFIG_BASENAME]");
-}
-
-void print_EDIT_help() {
- HELP("ck edit PROGRAM_NAME [CONFIG_BASENAME]");
-}
-
-void print_LIST_help() {
- ckhelp("ck list tree [-a] [-b]");
- ckhelp("ck list -p PROGRAM_NAME [-t list-type] [-a] [-b]");
- ckhelp("ck list programs [-t list-type] [-a] [-b]");
- ckhelp("ck list paths [-t list-type] [-a] [-b]");
- ckhelp("ck list ckconf");
- report_help();
-}
-
-void print_SEARCH_help() {
- HELP("ck search SEARCH_TERM");
-}
-
-void print_HELP_help() {
- HELP("ck help action");
-}
-
-void print_RESTORE_help() {
- ckhelp("ck restore -p PROGRAM_NAME");
- ckhelp("ck restore all");
- report_help();
-}
-
-void print_conf_help(void) {
- HELP("ck [-v|--verbose] [-c|--config DIR] action [...]");
-}
-
-void print_verbose_help(void) {
- HELP("ck [-v|--verbose] [-c|--config DIR] action [...]");
-}
diff --git a/src/actions.h b/src/actions.h
index 4550d4c..881fe9d 100644
--- a/src/actions.h
+++ b/src/actions.h
@@ -17,7 +17,6 @@
#define ACTIONS_H
#include "ckutil.h"
-#include "clparser.h"
#include "confparser.h"
#define X(ACTION, MIN, MAX) \
@@ -67,29 +66,11 @@ struct ListOptions {
int err;
};
-/* init.c */
-int init_create_config_file(UserOpt *opt);
-
-/* add.c */
-AddOpt add_make_options(cklist *args);
-void add_print_opts(AddOpt *opt);
-int add_make_link(const AddOpt *opt, const Conf *conf);
-
-/* list.c */
-ListOpt list_make_options(cklist *args);
-
-/* restore.c */
-int restore_make_links(cklist *from, cklist *to);
-
-/************************/
-/* PRINT RESULTS & HELP */
-/************************/
+/**************/
+/* PRINT HELP */
+/**************/
#define X(ACTION, MIN, MAX) \
void print_##ACTION##_help(void);
CK_ACTIONS
#undef X
-
-void print_conf_help(void);
-void print_verbose_help(void);
-
#endif /* ACTIONS_H */
diff --git a/src/add.c b/src/add.c
index f7a49bf..9959204 100644
--- a/src/add.c
+++ b/src/add.c
@@ -1,8 +1,16 @@
+/* add.c - the add action ----------------------------------------------*- C -*-
+ *
+ * This file is part of ck, the config keeper
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Copyright (C) 2018 Anastasis Grammenos
+ * GPLv3 (see LICENCE for the full notice)
+ *
+ * -------------------------------------------------------------------------- */
#include <libgen.h>
-#include "actions.h"
#include "dblayer.h"
-#include "queries.h"
#include "ckerrlog.h"
ERRLOG(add);
@@ -160,7 +168,8 @@ static int add_insert_relationship(DB *db, const int pid, const int cid) {
return 1;
}
-int add_transaction_try(DB *db, const AddOpt * const opt, const char *home) {
+/* Returns 1 in error, 0 otherwise */
+static int add_transaction_try(DB *db, const AddOpt * const opt, const char *home) {
__BEGIN_TRANSACTION__
int pid = add_get_or_insert_program_to_db(db, opt->progName);
if (db->error == SQL_ERR_SQLITE) {
@@ -221,7 +230,7 @@ static int move_config(const AddOpt *opt, char *progDir, char *ret) {
return 0;
}
-AddOpt add_make_options(cklist *args) {
+static AddOpt add_make_options(cklist *args) {
list_rewind(args);
/* since we are here, the first two arguments must exist */
AddOpt addOpt = {
@@ -253,7 +262,7 @@ AddOpt add_make_options(cklist *args) {
return addOpt;
}
-void add_print_opts(AddOpt *opt) {
+static void add_print_opts(AddOpt *opt) {
printf("Program:\t%s\nConfig:\t\t%s\n", opt->progName, opt->confPath);
if (opt->prime && opt->secret) {
printf("Options:\tsecret, primary\n");
@@ -273,7 +282,7 @@ static void get_or_make_program_dir(const AddOpt *opt, const Conf *conf, char *r
strcpy(ret, tmp);
}
-int add_make_link(const AddOpt *opt, const Conf *conf) {
+static int add_make_link(const AddOpt *opt, const Conf *conf) {
char progDir[STR_L] = "";
get_or_make_program_dir(opt, conf, progDir);
char newPath[STR_L] = "";
@@ -285,3 +294,39 @@ int add_make_link(const AddOpt *opt, const Conf *conf) {
}
return 0;
}
+
+int run_ADD(UserOpt * opt, Conf *conf) {
+ DB db;
+ if (open_DB(&db, opt)) {
+ return -1;
+ }
+ AddOpt addOpt = add_make_options(opt->args);
+ switch (addOpt.err) {
+ case ADD_NO_ERR:
+ break;
+ case ADD_ERR_WRONG_CONFIG:
+ ERR("The config file specified doesn't exist or is a link.");
+ goto error;
+ case ADD_ERR_WRONG_FLAGS:
+ ERR("Flags are: -s for secret and -p for primary.");
+ goto error;
+ }
+ add_print_opts(&addOpt);
+ /* Try adding the new config to the DB */
+ if (add_transaction_try(&db, &addOpt, conf->home_dir)) {
+ goto error;
+ }
+ if (add_make_link(&addOpt, conf)) {
+ error:
+ close_DB(&db);
+ sERR("Could not complete add transaction.");
+ return -1;
+ }
+ close_DB(&db);
+ hLOG("ckdb updated succesfully.");
+ return 0;
+}
+
+void print_ADD_help() {
+ HELP("ck add PROGRAM_NAME CONFIG_PATH [-p] [-s]");
+}
diff --git a/src/ck.c b/src/ck.c
index 4126e6d..361d9f0 100644
--- a/src/ck.c
+++ b/src/ck.c
@@ -28,10 +28,7 @@
*
* -------------------------------------------------------------------------- */
-#include "actions.h"
#include "dblayer.h"
-#include "cklist.h"
-#include "ckutil.h"
#include "ckerrlog.h"
ERRLOG(main);
diff --git a/src/ckutil.c b/src/ckutil.c
index 2b807de..bc86c02 100644
--- a/src/ckutil.c
+++ b/src/ckutil.c
@@ -16,7 +16,6 @@
#include <sys/stat.h>
#include <unistd.h>
-#include "ckutil.h"
#include "ckerrlog.h"
ERRLOG(utility);
diff --git a/src/clparser.c b/src/clparser.c
index 081c981..5b0852b 100644
--- a/src/clparser.c
+++ b/src/clparser.c
@@ -8,8 +8,6 @@
* GPLv3 (see LICENCE for the full notice)
*
* -------------------------------------------------------------------------- */
-#include "ckutil.h"
-#include "clparser.h"
#include "confparser.h"
#include "ckinfo.h"
#include "ckerrlog.h"
@@ -106,7 +104,7 @@ static int parse_vals(UserOpt *opt) {
}
switch (opt->action) {
-#define X(ACTION, MIN, MAX) \
+#define X(ACTION, MIN, MAX) \
case CKA_##ACTION: \
return parse_##ACTION(opt);
CK_ACTIONS
@@ -118,7 +116,7 @@ static int parse_vals(UserOpt *opt) {
CkAction parser_get_action(const char *name, char *actionName) {
int i;
-#define X(ACTION, MIN, MAX) \
+#define X(ACTION, MIN, MAX) \
for (i = 1; i < atoi(str##ACTION[0]) + 1; i++) { \
if (strcmp(name, str##ACTION[i]) == 0) { \
if (actionName) { \
diff --git a/src/confparser.c b/src/confparser.c
index 046bee7..6c01e6d 100644
--- a/src/confparser.c
+++ b/src/confparser.c
@@ -8,7 +8,6 @@
* GPLv3 (see LICENCE for the full notice)
*
* -------------------------------------------------------------------------- */
-#include "ckutil.h"
#include "confparser.h"
#include "ckerrlog.h"
diff --git a/src/dblayer.c b/src/dblayer.c
index 12be997..f13236c 100644
--- a/src/dblayer.c
+++ b/src/dblayer.c
@@ -11,8 +11,6 @@
#include <libgen.h>
#include "dblayer.h"
-#include "queries.h"
-#include "ckutil.h"
#include "ckerrlog.h"
ERRLOG(ckdb);
diff --git a/src/dblayer.h b/src/dblayer.h
index c830a94..08299f1 100644
--- a/src/dblayer.h
+++ b/src/dblayer.h
@@ -18,6 +18,7 @@
#include <sqlite3.h>
#include "actions.h"
+#include "queries.h"
enum SqlErrors {
SQL_NO_ERR = 0,
@@ -62,27 +63,8 @@ int get_pid_from_cid(DB *db, int cid);
void print_suggested_configs(DB *db, const char *pName);
-/* init.c */
-void init_make_tables(DB *db);
-
-/* add.c */
-/* Returns 1 in error, 0 otherwise */
-int add_transaction_try(DB *db, const AddOpt * const opt, const char *home);
-
-/* edit.c */
-int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secret);
-int edit_get_config(DB *db, const char *pName, char *ret, const char *cName, int *sec);
-
/* list.c */
int list_get_paths(DB *db, cklist *ckl, int bName, int attr, const char *home);
int list_get_programs(DB *db, cklist *ckl);
-int list_get_path_program_tree(DB *db, cklist *ckl, int bName, int attr, const char *home);
-
-/* delete.c */
-int del_transaction_try(DB *db, const char *pName, const char *cBaseName);
-
-/* restore.c */
-int restore_configs_exists(DB *db, Conf *conf, const char *pName, cklist *from, cklist *to);
-int restore_all_exist(DB *db, Conf *conf, cklist *from, cklist *to);
#endif /* DBLAYER_H */
diff --git a/src/delete.c b/src/delete.c
index ce7017f..00f51f8 100644
--- a/src/delete.c
+++ b/src/delete.c
@@ -1,8 +1,16 @@
+/* delete.c - the delete action ----------------------------------------*- C -*-
+ *
+ * This file is part of ck, the config keeper
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Copyright (C) 2018 Anastasis Grammenos
+ * GPLv3 (see LICENCE for the full notice)
+ *
+ * -------------------------------------------------------------------------- */
#include <libgen.h>
-#include "actions.h"
#include "dblayer.h"
-#include "queries.h"
#include "ckerrlog.h"
ERRLOG(delete);
@@ -132,7 +140,7 @@ static int get_cid_from_pname_and_basename(DB *db, const char *pName, const char
return _cid;
}
-int del_transaction_try(DB *db, const char *pName, const char *cBaseName) {
+static int del_transaction_try(DB *db, const char *pName, const char *cBaseName) {
__BEGIN_TRANSACTION__
int pid = -1;
if (cBaseName) {
@@ -174,3 +182,45 @@ int del_transaction_try(DB *db, const char *pName, const char *cBaseName) {
__END_TRANSACTION__
return 0;
}
+
+int run_DEL(UserOpt * opt, Conf *conf) {
+ UNUSED(conf);
+ DB db;
+ if (open_DB(&db, opt)) {
+ return -1;
+ }
+
+ int rc = -1;
+ /* Since we are here, args have to be 1 or 2 */
+ char *pName = list_get(opt->args);
+ if (!program_exists(&db, pName)) {
+ ERR("Program %s doesn't exist in the database.", pName);
+ goto error;
+ }
+
+ /* If there is no next argument */
+ if (!list_next(opt->args)) {
+ rc = del_transaction_try(&db, pName, NULL);
+ }
+ /* If there are more arguments */
+ else {
+ char *cName = list_get(opt->args);
+ rc = del_transaction_try(&db, pName, cName);
+ if (rc) {
+ HELP("Program %s doesn't have a config named %s", pName, cName);
+ print_suggested_configs(&db, pName);
+ }
+ }
+ error:
+ close_DB(&db);
+ if (!rc) {
+ hLOG("ckdb updated succesfully.");
+ } else {
+ sERR("Could not complete delete transaction.");
+ }
+ return rc;
+}
+
+void print_DEL_help() {
+ HELP("ck delete PROGRAM_NAME [CONFIG_BASENAME]");
+}
diff --git a/src/edit.c b/src/edit.c
index 1058d45..5179a54 100644
--- a/src/edit.c
+++ b/src/edit.c
@@ -1,11 +1,21 @@
+/* edit.c - the edit action --------------------------------------------*- C -*-
+ *
+ * This file is part of ck, the config keeper
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Copyright (C) 2018 Anastasis Grammenos
+ * GPLv3 (see LICENCE for the full notice)
+ *
+ * -------------------------------------------------------------------------- */
#include <libgen.h>
-#include "actions.h"
#include "dblayer.h"
-#include "queries.h"
#include "ckerrlog.h"
-int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secret) {
+ERRLOG(edit);
+
+static int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secret) {
int pid = get_program_id(db, pName);
/* error */
if (pid == -2) {
@@ -29,7 +39,7 @@ int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secr
return -1;
}
-int edit_get_config(DB *db, const char *pName, char *ret, const char *cName, int *sec) {
+static int edit_get_config(DB *db, const char *pName, char *ret, const char *cName, int *sec) {
int pid = get_program_id(db, pName);
/* error */
if (pid == -2) {
@@ -89,3 +99,79 @@ int edit_get_config(DB *db, const char *pName, char *ret, const char *cName, int
}
return -1;
}
+
+int run_EDIT(UserOpt *opt, Conf *conf) {
+ DB db;
+ if (open_DB(&db, opt)) {
+ return -1;
+ }
+
+ list_rewind(opt->args);
+ char confPath[STR_L] = "";
+ char confName[STR_M] = "";
+ int secret = 0;
+ /* Since we are here, args have to be 1 or 2 */
+ char *pName = list_get(opt->args);
+ if (!program_exists(&db, pName)) {
+ ERR("Program %s doesn't exist in the database.", pName);
+ goto error;
+ }
+ /* If there is no next argument */
+ if (!list_next(opt->args)) {
+ /* If there is no primary config*/
+ if (edit_get_prime_config_from_program(&db, pName, confName, &secret) == -1) {
+ /* If the program has only one config */
+ if (get_config_number(&db, pName) == 1) {
+ if (edit_get_config(&db, pName, confName, NULL, &secret)) {
+ ERR("Coudln't find config file for %s", pName);
+ goto error;
+ }
+ }
+ /* If the program has many configs */
+ else {
+ HELP("Ambiguous config. Please type the config name after the program.");
+ print_suggested_configs(&db, pName);
+ goto error;
+ }
+ }
+ }
+ /* If there are more arguments */
+ else {
+ char *cName = list_get(opt->args);
+ if (edit_get_config(&db, pName, confName, cName, &secret)) {
+ ERR("Program %s doesn't have a config named %s", pName, cName);
+ print_suggested_configs(&db, pName);
+ goto error;
+ }
+ }
+ close_DB(&db);
+ str_join_dirname_with_basename(confPath, secret ? conf->scrt_dir : conf->vc_dir, confName);
+
+ char *editor = getenv("EDITOR");
+ char command[STR_L] = "";
+ 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;
+}
+
+void print_EDIT_help() {
+ HELP("ck edit PROGRAM_NAME [CONFIG_BASENAME]");
+}
+
diff --git a/src/help.c b/src/help.c
new file mode 100644
index 0000000..ead8d45
--- /dev/null
+++ b/src/help.c
@@ -0,0 +1,53 @@
+/* help.c - the help action --------------------------------------------*- C -*-
+ *
+ * This file is part of ck, the config keeper
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Copyright (C) 2018 Anastasis Grammenos
+ * GPLv3 (see LICENCE for the full notice)
+ *
+ * -------------------------------------------------------------------------- */
+#include "dblayer.h"
+#include "ckerrlog.h"
+
+ERRLOG(action);
+
+static void print_conf_help(void) {
+ HELP("ck [-v|--verbose] [-c|--config DIR] action [...]");
+}
+
+static void print_verbose_help(void) {
+ HELP("ck [-v|--verbose] [-c|--config DIR] action [...]");
+}
+
+int run_HELP(UserOpt *opt, Conf *conf) {
+ UNUSED(conf);
+ char tmp[STR_M] = "";
+ if (strcmp(list_get(opt->args), "config") == 0) {
+ print_conf_help();
+ return 0;
+ }
+
+ if (strcmp(list_get(opt->args), "verbose") == 0) {
+ print_verbose_help();
+ return 0;
+ }
+
+ switch(parser_get_action(list_get(opt->args), NULL)) {
+#define X(ACTION, MIN, MAX) \
+ case CKA_##ACTION: \
+ HELP("%s", get_possible_action_strings(tmp, CKA_##ACTION)); \
+ print_##ACTION##_help(); \
+ return 0;
+ CK_ACTIONS
+#undef X
+ default:
+ ERR("Unknown action: %s", list_get(opt->args));
+ }
+ return -1;
+}
+
+void print_HELP_help() {
+ HELP("ck help action");
+}
diff --git a/src/init.c b/src/init.c
index e38a6f5..5f47b33 100644
--- a/src/init.c
+++ b/src/init.c
@@ -1,11 +1,19 @@
-#include "actions.h"
+/* init.c - the init action --------------------------------------------*- C -*-
+ *
+ * This file is part of ck, the config keeper
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Copyright (C) 2018 Anastasis Grammenos
+ * GPLv3 (see LICENCE for the full notice)
+ *
+ * -------------------------------------------------------------------------- */
#include "dblayer.h"
-#include "queries.h"
#include "ckerrlog.h"
ERRLOG(init);
-int init_create_config_file(UserOpt *opt) {
+static int init_create_config_file(UserOpt *opt) {
char absVCdir[STR_L] = "";
if (!util_file_exists(list_get_at(opt->args, 0), absVCdir)) {
ERR("Version control directory: %s does not exist.", list_get_at(opt->args, 0));
@@ -49,7 +57,7 @@ int init_create_config_file(UserOpt *opt) {
return 0;
}
-void init_make_tables(DB *db) {
+static void init_make_tables(DB *db) {
char sql[STR_L] = "";
dbh_form_query_make_tables(sql);
@@ -60,3 +68,27 @@ void init_make_tables(DB *db) {
return;
}
}
+
+int run_INIT(UserOpt * opt, Conf *conf) {
+ UNUSED(conf);
+ if (db_exists(opt)) {
+ ERR("ck is already initialized in %s", opt->confDir);
+ return -1;
+ }
+ if (init_create_config_file(opt)) {
+ sERR("Cound not create config file.");
+ return -1;
+ }
+ DB db;
+ if (open_DB(&db, opt)) {
+ return -1;
+ }
+ init_make_tables(&db);
+ sqlite3_close(db.ptr);
+ hLOG("Initialized empty ckdb.");
+ return 0;
+}
+
+void print_INIT_help() {
+ HELP("ck init VERSION_CONTROL_DIR SECRET_DIR");
+}
diff --git a/src/list.c b/src/list.c
index 105c101..52603b8 100644
--- a/src/list.c
+++ b/src/list.c
@@ -1,11 +1,21 @@
+/* list.c - the list action --------------------------------------------*- C -*-
+ *
+ * This file is part of ck, the config keeper
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Copyright (C) 2018 Anastasis Grammenos
+ * GPLv3 (see LICENCE for the full notice)
+ *
+ * -------------------------------------------------------------------------- */
#include <libgen.h>
-#include "actions.h"
#include "dblayer.h"
-#include "queries.h"
#include "ckerrlog.h"
-ListOpt list_make_options(cklist *args) {
+ERRLOG(list);
+
+static ListOpt list_make_options(cklist *args) {
list_rewind(args);
ListOpt listOpt = {
._lt = LT_TREE,
@@ -135,7 +145,7 @@ int list_get_programs(DB *db, cklist *ckl) {
return 1;
}
-int list_get_path_program_tree(DB *db, cklist *ckl, int bName, int attr, const char *home) {
+static int list_get_path_program_tree(DB *db, cklist *ckl, int bName, int attr, const char *home) {
sqlite3_stmt *stmt;
int rc;
@@ -204,3 +214,79 @@ int list_get_path_program_tree(DB *db, cklist *ckl, int bName, int attr, const c
return 1;
}
+
+int run_LIST(UserOpt *opt, Conf *conf) {
+ DB db;
+ if (open_DB(&db, opt)) {
+ return -1;
+ }
+
+ cklist *the_list = list_make_new();
+ ListOpt listOpt = list_make_options(opt->args);
+
+ if (listOpt.err) {
+ ERR("Wrong list options.");
+ goto error;
+ }
+ char tmp[STR_L] = "";
+ switch(listOpt._lt) {
+ case LT_PATH:
+ list_get_paths(&db, the_list, listOpt.bName, listOpt.attr, conf->home_dir);
+ break;
+ case LT_PROGRAM:
+ list_get_programs(&db, the_list);
+ break;
+ case LT_TREE:
+ list_get_path_program_tree(&db, the_list, listOpt.bName, listOpt.attr, conf->home_dir);
+ list_print(the_list);
+ goto close;
+ case LT_CKCONF:
+ strcat(tmp, "ck configuration directory path: ");
+ strcat(tmp, opt->confDir);
+ list_add(the_list, tmp);
+#define X(var, str, name) \
+ strcpy(tmp, ""); \
+ strcat(tmp, name); \
+ strcat(tmp, ": "); \
+ strcat(tmp, conf->var); \
+ list_add(the_list, tmp);
+ CONFIG_VARIABLES_TABLE;
+#undef X
+ list_print(the_list);
+ goto close;
+ case LT_PROG_CONFS:
+ if (!program_exists(&db, listOpt.pName)) {
+ ERR("Program %s doesn't exist in the database.", listOpt.pName);
+ goto error;
+ }
+ get_program_paths(&db, the_list, listOpt.pName, listOpt.bName, listOpt.attr, conf->home_dir);
+ break;
+ }
+ switch(listOpt._lst) {
+ case LST_PLAIN:
+ list_print(the_list);
+ break;
+ case LST_LISP:
+ list_print_lisp(the_list);
+ break;
+ case LST_PYTHON:
+ list_print_python(the_list);
+ }
+ close:
+ close_DB(&db);
+ list_free(the_list);
+ return 0;
+ error:
+ close_DB(&db);
+ list_free(the_list);
+ return -1;
+}
+
+void print_LIST_help() {
+ ckhelp("ck list tree [-a] [-b]");
+ ckhelp("ck list -p PROGRAM_NAME [-t list-type] [-a] [-b]");
+ ckhelp("ck list programs [-t list-type] [-a] [-b]");
+ ckhelp("ck list paths [-t list-type] [-a] [-b]");
+ ckhelp("ck list ckconf");
+ report_help();
+}
diff --git a/src/queries.c b/src/queries.c
index 0c56cc1..15e5acb 100644
--- a/src/queries.c
+++ b/src/queries.c
@@ -9,6 +9,7 @@
*
* -------------------------------------------------------------------------- */
#include "queries.h"
+#include "ckutil.h"
void dbh_form_query_make_tables(char *query) {
char tmp[STR_L] = "CREATE TABLE ";
diff --git a/src/queries.h b/src/queries.h
index d4c3ae0..7d5ec47 100644
--- a/src/queries.h
+++ b/src/queries.h
@@ -18,9 +18,6 @@
#include <sqlite3.h>
-#include "clparser.h"
-#include "ckutil.h"
-
/********************/
/* sqlite constants */
/********************/
diff --git a/src/restore.c b/src/restore.c
index cb8e6c9..a02f6f3 100644
--- a/src/restore.c
+++ b/src/restore.c
@@ -1,13 +1,21 @@
+/* restore.c - the restore action --------------------------------------*- C -*-
+ *
+ * This file is part of ck, the config keeper
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Copyright (C) 2018 Anastasis Grammenos
+ * GPLv3 (see LICENCE for the full notice)
+ *
+ * -------------------------------------------------------------------------- */
#include <libgen.h>
-#include "actions.h"
#include "dblayer.h"
-#include "queries.h"
#include "ckerrlog.h"
ERRLOG(restore);
-int restore_make_links(cklist *from, cklist *to) {
+static int restore_make_links(cklist *from, cklist *to) {
list_rewind(from);
list_rewind(to);
if (list_size(from) > 0
@@ -44,7 +52,7 @@ int restore_make_links(cklist *from, cklist *to) {
return 0;
}
-int restore_configs_exists(DB *db, Conf *conf, const char *pName, cklist *from, cklist *to) {
+static int restore_configs_exists(DB *db, Conf *conf, const char *pName, cklist *from, cklist *to) {
sqlite3_stmt *stmt;
int rc;
@@ -95,7 +103,7 @@ int restore_configs_exists(DB *db, Conf *conf, const char *pName, cklist *from,
return !err_flag;
}
-int restore_all_exist(DB *db, Conf *conf, cklist *from, cklist *to) {
+static int restore_all_exist(DB *db, Conf *conf, cklist *from, cklist *to) {
cklist *programs = list_make_new();
if (list_get_programs(db, programs) != 1) {
ERR("No programs in ckdb");
@@ -113,3 +121,68 @@ int restore_all_exist(DB *db, Conf *conf, cklist *from, cklist *to) {
list_free(programs);
return !err_flag;
}
+
+int run_RESTORE(UserOpt *opt, Conf *conf) {
+ DB db;
+ if (open_DB(&db, opt)) {
+ return -1;
+ }
+ cklist *from = list_make_new();
+ cklist *to = list_make_new();
+ int err_flag = 0;
+ if (strcmp(list_get(opt->args), "-p") == 0) {
+ if (list_next(opt->args)) {
+ if (program_exists(&db, list_get(opt->args))) {
+ if (restore_configs_exists(&db, conf, list_get(opt->args), from, to)) {
+ hLOG("Restoring links for %s...", list_get(opt->args));
+ }
+ else {
+ err_flag = 1;
+ }
+ }
+ else {
+ ERR("Program %s does not exist", list_get(opt->args));
+ err_flag = 1;
+ }
+ }
+ else {
+ sERR("-p needs a program name");
+ err_flag = 1;
+ }
+ }
+ else if (strcmp(list_get(opt->args), "all") == 0) {
+ if (!list_next(opt->args)) {
+ if (restore_all_exist(&db, conf, from, to)) {
+ hLOG("Restoring all links...");
+ }
+ else {
+ err_flag = 1;
+ }
+ }
+ else {
+ sERR("Wrong argument");
+ err_flag = 1;
+ }
+ }
+ else {
+ sERR("Wrong argument");
+ err_flag = 1;
+ }
+ close_DB(&db);
+ int rc = -1;
+ if (!err_flag) {
+ rc = restore_make_links(from, to);
+ if (rc) {
+ sERR("Restore failed.");
+ }
+ }
+ list_free(from);
+ list_free(to);
+ return rc;
+}
+
+void print_RESTORE_help() {
+ ckhelp("ck restore -p PROGRAM_NAME");
+ ckhelp("ck restore all");
+ report_help();
+}
diff --git a/src/search.c b/src/search.c
new file mode 100644
index 0000000..a76bb29
--- /dev/null
+++ b/src/search.c
@@ -0,0 +1,54 @@
+/* search.c - the search action ----------------------------------------*- C -*-
+ *
+ * This file is part of ck, the config keeper
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Copyright (C) 2018 Anastasis Grammenos
+ * GPLv3 (see LICENCE for the full notice)
+ *
+ * -------------------------------------------------------------------------- */
+#include "dblayer.h"
+#include "ckerrlog.h"
+
+ERRLOG(search);
+
+int run_SEARCH(UserOpt *opt, Conf *conf) {
+ if (system("which grep > /dev/null 2>&1") != 0) {
+ ERR("No grep avaliable. Please make sure you have grep installed.");
+ return -1;
+ }
+ DB db;
+ if (open_DB(&db, opt)) {
+ return -1;
+ }
+
+ cklist *paths = list_make_new();
+ list_get_paths(&db, paths, 0 /*basename*/, 0/*attributes*/, conf->home_dir);
+ close_DB(&db);
+ if (list_size(paths) && list_size(opt->args)) {
+ do {
+ FILE *cmd;
+ char result[STR_L] = "";
+ char command[STR_L] = "grep -H -n \"";
+ strcat(command, list_get(opt->args));
+ strcat(command, "\" ");
+ strcat(command, list_get(paths));
+ cmd = popen(command, "r");
+ if (cmd == NULL) {
+ list_free(paths);
+ return -1;
+ }
+ while (fgets(result, sizeof(result), cmd)) {
+ printf("%s", result);
+ }
+ pclose(cmd);
+ } while(list_next(paths));
+ }
+ list_free(paths);
+ return 0;
+}
+
+void print_SEARCH_help() {
+ HELP("ck search SEARCH_TERM");
+}