aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2018-10-09 20:53:30 +0300
committergramanas <anastasis.gramm2@gmail.com>2018-10-09 20:53:30 +0300
commit598e0522de924a50ea9c640955daab8512029eb4 (patch)
tree3f3cf027349a414e2f24d1a4c7976916e8b7dc21 /src
parentfe781e5ae7116733e5b335a0ac016af97266db5f (diff)
downloadck-598e0522de924a50ea9c640955daab8512029eb4.tar.gz
ck-598e0522de924a50ea9c640955daab8512029eb4.tar.bz2
ck-598e0522de924a50ea9c640955daab8512029eb4.zip
Better list
Diffstat (limited to 'src')
-rw-r--r--src/actionhelper.c21
-rw-r--r--src/actionparser.c8
-rw-r--r--src/actions.c37
-rw-r--r--src/actions.h5
-rw-r--r--src/dblayer.c36
-rw-r--r--src/dblayer.h2
6 files changed, 80 insertions, 29 deletions
diff --git a/src/actionhelper.c b/src/actionhelper.c
index 3e898af..582333e 100644
--- a/src/actionhelper.c
+++ b/src/actionhelper.c
@@ -149,15 +149,12 @@ void add_make_link(const AddOpt *opt, const Conf *conf) {
}
}
-int edit_make_options(cklist *args) {
- UNUSED(args);
-}
-
ListOpt list_make_options(cklist *args) {
list_rewind(args);
ListOpt listOpt = {
._lt = LT_NONE,
._lst = LST_PLAIN,
+ .pName = NULL,
.attr = 0,
.err = 0
};
@@ -195,8 +192,21 @@ ListOpt list_make_options(cklist *args) {
else if (strcmp(list_get(args), "tree") == 0) {
listOpt._lt = LT_TREE;
}
+ else if (strcmp(list_get(args), "ckconf") == 0) {
+ listOpt._lt = LT_CKCONF;
+ }
+ else if (strcmp(list_get(args), "-p") == 0) {
+ if (list_next(args)) {
+ listOpt._lt = LT_PROG_CONFS;
+ listOpt.pName = list_get(args);
+ }
+ else {
+ listOpt.err = 1;
+ break;
+ }
+ }
else {
- listOpt.err = 1;
+ listOpt.err = 1;
}
} while(list_next(args));
}
@@ -240,7 +250,6 @@ void print_LIST_result(int err) {
if (!err) {
return;
}
- ERR("Wrong list arguments");
}
void print_SEARCH_result(int err) {
diff --git a/src/actionparser.c b/src/actionparser.c
index 7c1186a..1da1d2f 100644
--- a/src/actionparser.c
+++ b/src/actionparser.c
@@ -130,7 +130,7 @@ int parse_EDIT(UserOpt *opt) {
int parse_LIST(UserOpt *opt) {
/* List expects a maximum of than 3 arguments */
- if (optNum <= pos || optNum > pos + 4) {
+ if (optNum <= pos || optNum > pos + 5) {
opt->err = PERR_LIST_WRONG;
return -1;
}
@@ -329,13 +329,13 @@ void print_parser_error(UserOpt *opt) {
sprintf(errStr, "Add config \nUsage: %s ProgramName ConfigPath [-s](secret) [-p](primary)", names);
break;
case PERR_DEL_WRONG:
- sprintf(errStr, "Delete config or program\nUsage: %s ....", names);
+ sprintf(errStr, "Delete config or program\nUsage: %s {ProgramName} | {-c ConfigPath (as shown by ck list)}", names);
break;
case PERR_EDIT_WRONG:
sprintf(errStr, "Edit config with $EDITOR (%s)\nUsage: %s ProgramName [configBasename]", getenv("EDITOR"), names);
break;
case PERR_LIST_WRONG:
- sprintf(errStr, "List programs, configs and more\nUsage: %s value-to-list (or tree) [-t list-type] [-a]", names);
+ sprintf(errStr, "List programs, configs and more\nUsage: %s {programs|paths|-p ProgramName} [-t list-type] | {tree | ckconf} [-a]", names);
break;
case PERR_SEARCH_WRONG:
sprintf(errStr, "Search through the configs with grep\nUsage: %s search-term", names);
@@ -344,7 +344,7 @@ void print_parser_error(UserOpt *opt) {
sprintf(errStr, "Usage: ........");
break;
}
- ERR("%s", errStr);
+ HELP("%s", errStr);
}
void print_parser_help() {
diff --git a/src/actions.c b/src/actions.c
index 4ff6816..1b4c9b5 100644
--- a/src/actions.c
+++ b/src/actions.c
@@ -134,8 +134,11 @@ int run_EDIT(UserOpt *opt, Conf *conf) {
/* If the program has many configs */
else {
HELP("Ambiguous config. Please type the config name after the program.");
- cklist *paths = list_make_new();
- edit_get_avaliable_paths(&db, paths, pName);
+ char name[STR_M] = "";
+ strcat(name, pName);
+ strcat(name, ":");
+ cklist *paths = list_make_and_add(name);
+ get_program_paths(&db, paths, pName, 1, 0);
list_print(paths);
list_free(paths);
goto error;
@@ -147,8 +150,11 @@ int run_EDIT(UserOpt *opt, Conf *conf) {
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);
- cklist *paths = list_make_new();
- edit_get_avaliable_paths(&db, paths, pName);
+ char name[STR_M] = "";
+ strcat(name, pName);
+ strcat(name, ":");
+ cklist *paths = list_make_and_add(name);
+ get_program_paths(&db, paths, pName, 1, 0);
list_print(paths);
list_free(paths);
goto error;
@@ -171,7 +177,6 @@ int run_EDIT(UserOpt *opt, Conf *conf) {
}
int run_LIST(UserOpt *opt, Conf *conf) {
- UNUSED(conf);
DB db = open_DB(opt);
if (db.ptr == NULL) {
if (db.error == SQL_ERR_NO_TABLES) {
@@ -186,6 +191,7 @@ int run_LIST(UserOpt *opt, Conf *conf) {
if (listOpt.err) {
goto error;
}
+ char tmp[STR_L] = "";
switch(listOpt._lt) {
case LT_PATH:
list_get_paths(&db, the_list, listOpt.attr);
@@ -197,6 +203,27 @@ int run_LIST(UserOpt *opt, Conf *conf) {
list_get_path_program_tree(&db, the_list, listOpt.attr);
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, 0, listOpt.attr);
+ break;
case LT_NONE:
goto error;
}
diff --git a/src/actions.h b/src/actions.h
index c9da4fb..624ae30 100644
--- a/src/actions.h
+++ b/src/actions.h
@@ -56,10 +56,12 @@ struct DelOptions {
typedef enum ListTypes ListType;
enum ListTypes {
+ LT_NONE,
LT_PATH,
LT_PROGRAM,
LT_TREE,
- LT_NONE,
+ LT_CKCONF,
+ LT_PROG_CONFS
};
typedef enum ListShowTypes ListShowType;
@@ -73,6 +75,7 @@ typedef struct ListOptions ListOpt;
struct ListOptions {
ListType _lt;
ListShowType _lst;
+ char *pName;
int attr;
int err;
};
diff --git a/src/dblayer.c b/src/dblayer.c
index 4290a7a..973d561 100644
--- a/src/dblayer.c
+++ b/src/dblayer.c
@@ -478,9 +478,10 @@ int edit_get_config(DB *db, const char *pName, char *ret, const char *cName, int
sqlite3_finalize(stmt);
return flag;
}
+ return -1;
}
-int edit_get_avaliable_paths(DB *db, cklist *ckl, const char* pName) {
+int get_program_paths(DB *db, cklist *ckl, const char* pName, int bname, int attr) {
int pid = get_program_id(db, pName);
/* error */
if (pid == -2) {
@@ -493,6 +494,10 @@ int edit_get_avaliable_paths(DB *db, cklist *ckl, const char* pName) {
int rc;
char selection[STR_M] = COL_CONFIG_PATH;
+ strcat(selection, ",");
+ strcat(selection, COL_CONFIG_SECRET);
+ strcat(selection, ",");
+ strcat(selection, COL_CONFIG_PRIMARY);
char condition[STR_M] = TBL_PROGRAM;
strcat(condition, ".");
strcat(condition, COL_PROGRAM_ID);
@@ -506,21 +511,32 @@ int edit_get_avaliable_paths(DB *db, cklist *ckl, const char* pName) {
return -2;
}
- char name[STR_M] = "";
- strcat(name, pName);
- strcat(name, ":");
- list_add(ckl, name);
while (sqlite3_step(stmt) == SQLITE_ROW) {
char *tmp = strdup((char *)sqlite3_column_text(stmt, 0));
char entry[STR_M] = "";
- strcat(entry, "|- ");
- strcat(entry, basename(tmp));
+ if (bname) {
+ strcat(entry, basename(tmp));
+ }
+ else {
+ strcat(entry, tmp);
+ }
+ if (attr) {
+ /* secret */
+ if (sqlite3_column_int(stmt, 1)) {
+ strcat(entry, " [s]");
+ }
+ /* primary */
+ if (sqlite3_column_int(stmt, 2)) {
+ strcat(entry, " [p]");
+ }
+ }
list_add(ckl, entry);
free(tmp);
}
sqlite3_finalize(stmt);
return 0;
}
+ return -1;
}
int list_get_paths(DB *db, cklist *ckl, int attr) {
@@ -732,15 +748,11 @@ int get_program_relations(DB *db, int pid) {
int get_config_number(DB *db, char* pName) {
int pid = get_program_id(db, pName);
- /* error */
- if (pid == -2) {
- return -1;
- }
-
/* program exists */
if (pid > -1) {
return get_program_relations(db, pid);
}
+ return -1;
}
/* Removes the relationship of `cid` with the corresponding program.
diff --git a/src/dblayer.h b/src/dblayer.h
index 05ba5bd..c99f143 100644
--- a/src/dblayer.h
+++ b/src/dblayer.h
@@ -43,6 +43,7 @@ extern DB open_DB(const UserOpt *opt);
extern void close_DB(DB *db);
extern int program_exists(DB *db, const char *pName);
+extern int get_program_paths(DB *db, cklist *ckl, const char* pName, int basename, int attr);
/********/
/* init */
@@ -66,7 +67,6 @@ extern int add_transaction_try(DB *db, const AddOpt * const opt);
extern int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secret);
extern int get_config_number(DB *db, char *pName);
extern int edit_get_config(DB *db, const char *pName, char *ret, const char *cName, int *sec);
-extern int edit_get_avaliable_paths(DB *db, cklist *ckl, const char* pName);
/********/
/* list */