From 5683c51d17b5eed7d2f070aa4e49cc21b65d82e5 Mon Sep 17 00:00:00 2001
From: gramanas <anastasis.gramm2@gmail.com>
Date: Mon, 29 Oct 2018 17:20:28 +0200
Subject: Add list with basename and update readme

---
 src/actionhelper.c |  8 +++++++-
 src/actionparser.c |  6 +++---
 src/actions.c      |  8 ++++----
 src/actions.h      |  1 +
 src/dblayer.c      | 36 ++++++++++++++++++++++++++++++++----
 src/dblayer.h      |  6 +++---
 6 files changed, 50 insertions(+), 15 deletions(-)

(limited to 'src')

diff --git a/src/actionhelper.c b/src/actionhelper.c
index 6428ab0..5899024 100644
--- a/src/actionhelper.c
+++ b/src/actionhelper.c
@@ -202,6 +202,7 @@ ListOpt list_make_options(cklist *args) {
     ._lst = LST_PLAIN,
     .pName = NULL,
     .attr = 0,
+    .bName = 0,
     .err = 0
   };
 
@@ -211,6 +212,10 @@ ListOpt list_make_options(cklist *args) {
         listOpt.attr = 1;
         continue;
       }
+      if (strcmp(list_get(args), "-b") == 0) {
+        listOpt.bName = 1;
+        continue;
+      }
       if (strcmp(list_get(args), "-t") == 0) {
 	if (!list_next(args)) {
 	  listOpt.err = 1;
@@ -421,7 +426,8 @@ void print_LIST_help() {
   ckhelp("  `-p program_name`: list only the configs of the specified program.");
   ckhelp("  `ckconf`: list the ck configuration values.\n");
   ckhelp("There are also some flags:");
-  ckhelp("  `-a`: add the attributes next to the config paths (secret,primary)");
+  ckhelp("  `-a`: add the attributes next to the config paths (secret,primary,root)");
+  ckhelp("  `-b`: print the config basenames instead of the full path");
   ckhelp("  `-t`: change the list type. (does not work with tree or ckconf)");
   ckhelp("      `plain`: default plain listing");
   ckhelp("      `python`: enclose the list in [ , ]");
diff --git a/src/actionparser.c b/src/actionparser.c
index f04c1d5..1648372 100644
--- a/src/actionparser.c
+++ b/src/actionparser.c
@@ -123,9 +123,9 @@ int parse_EDIT(UserOpt *opt) {
 }
 
 int parse_LIST(UserOpt *opt) {
-  /* List expects 1 to 5 arguments */
+  /* List expects 1 to 6 arguments */
   if (optNum < pos + 1
-      || optNum > pos + 5) {
+      || optNum > pos + 6) {
     opt->err = PERR_LIST_WRONG;
     return -1;
   }
@@ -360,7 +360,7 @@ void print_parser_error(UserOpt *opt) {
     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 {programs|paths|-p ProgramName} [-t list-type] | {tree | ckconf} [-a]", names);
+    sprintf(errStr, "List programs, configs and more\nUsage: %s {programs|paths|-p ProgramName} [-t list-type] | {tree | ckconf} [-a] [-b]", names);
     break;
   case PERR_SEARCH_WRONG:
     sprintf(errStr, "Search through the configs with grep\nUsage: %s search-term", names);
diff --git a/src/actions.c b/src/actions.c
index 649ffb8..ddfe9b9 100644
--- a/src/actions.c
+++ b/src/actions.c
@@ -179,13 +179,13 @@ int run_LIST(UserOpt *opt, Conf *conf) {
   char tmp[STR_L] = "";
   switch(listOpt._lt) {
   case LT_PATH:
-    list_get_paths(&db, the_list, listOpt.attr);
+    list_get_paths(&db, the_list, listOpt.bName, listOpt.attr);
     break;
   case LT_PROGRAM:
     list_get_programs(&db, the_list);
     break;
   case LT_TREE:
-    list_get_path_program_tree(&db, the_list, listOpt.attr);
+    list_get_path_program_tree(&db, the_list, listOpt.bName, listOpt.attr);
     list_print(the_list);
     goto close;
   case LT_CKCONF:
@@ -207,7 +207,7 @@ int run_LIST(UserOpt *opt, Conf *conf) {
       ERR("Program %s doesn't exist in the database.", listOpt.pName);
       goto error;
     }
-    get_program_paths(&db, the_list, listOpt.pName, 0, listOpt.attr);
+    get_program_paths(&db, the_list, listOpt.pName, listOpt.bName, listOpt.attr);
     break;
   }
   switch(listOpt._lst) {
@@ -237,7 +237,7 @@ int run_SEARCH(UserOpt *opt, Conf *conf) {
   }
   DB db = open_DB(opt);
   cklist *paths = list_make_new();
-  list_get_paths(&db, paths, 0);
+  list_get_paths(&db, paths, 0 /*basename*/, 0/*attributes*/);
   close_DB(&db);
   if (list_size(paths) && list_size(opt->args)) {
     do {
diff --git a/src/actions.h b/src/actions.h
index 69c5616..e6fe336 100644
--- a/src/actions.h
+++ b/src/actions.h
@@ -77,6 +77,7 @@ struct ListOptions {
   ListShowType _lst;
   char *pName;
   int attr;
+  int bName;
   int err;
 };
 
diff --git a/src/dblayer.c b/src/dblayer.c
index f517dbe..244db62 100644
--- a/src/dblayer.c
+++ b/src/dblayer.c
@@ -363,6 +363,16 @@ int add_get_or_insert_program_to_db(DB *db, const char *name) {
   return pid;
 }
 
+int add_basename_exists(DB *db, const char *pName, const char *path) {
+  cklist *baseNames = list_make_new();
+  get_program_paths(db, baseNames, pName, 1 /*basename */, 0);
+  char *tmp = strdup(path);
+  int rc = list_exists(baseNames, basename(tmp));
+  free(tmp);
+  list_free(baseNames);
+  return rc;
+}
+
 int add_transaction_try(DB *db, const AddOpt * const opt) {
   __BEGIN_TRANSACTION__
   int pid = add_get_or_insert_program_to_db(db, opt->progName);
@@ -370,6 +380,10 @@ int add_transaction_try(DB *db, const AddOpt * const opt) {
     PRINT_ERR("Could not insert program to db.\n");
     return 1;
   }
+  if (add_basename_exists(db, opt->progName, opt->confPath)) {
+    ERR("Cannot have two configs with the same basename, for the same program.");
+    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");
@@ -540,7 +554,7 @@ int get_program_paths(DB *db, cklist *ckl, const char* pName, int bname, int att
   return -1;
 }
 
-int list_get_paths(DB *db, cklist *ckl, int attr) {
+int list_get_paths(DB *db, cklist *ckl, int bName, int attr) {
   sqlite3_stmt *stmt;
   int rc;
 
@@ -553,8 +567,15 @@ int list_get_paths(DB *db, cklist *ckl, int attr) {
   }
 
   while (sqlite3_step(stmt) == SQLITE_ROW) {
+    char *tmp = strdup((char *)sqlite3_column_text(stmt, 0));
     char path[STR_L] = "";
-    strcat(path, (char *)sqlite3_column_text(stmt, 0));
+    if (bName) {
+      strcat(path, basename(tmp));
+    }
+    else {
+      strcat(path, tmp);
+    }
+    free(tmp);
     if (attr) {
       /* secret */
       if (sqlite3_column_int(stmt, 1)) {
@@ -596,7 +617,7 @@ int list_get_programs(DB *db, cklist *ckl) {
   return 1;
 }
 
-int list_get_path_program_tree(DB *db, cklist *ckl, int attr) {
+int list_get_path_program_tree(DB *db, cklist *ckl, int bName, int attr) {
   sqlite3_stmt *stmt;
   int rc;
 
@@ -638,7 +659,14 @@ int list_get_path_program_tree(DB *db, cklist *ckl, int attr) {
     sqlite3_bind_text(stmt2, 1, (char *)sqlite3_column_text(stmt, 0), -1, 0);
     while (sqlite3_step(stmt2) == SQLITE_ROW) {
       char treePath[STR_L] = "|- ";
-      strcat(treePath, (char *)sqlite3_column_text(stmt2, 0));
+      char *tmp = strdup((char *)sqlite3_column_text(stmt2, 0));
+      if (bName) {
+        strcat(treePath, basename(tmp));
+      }
+      else {
+        strcat(treePath, tmp);
+      }
+      free(tmp);
       if (attr) {
         /* secret */
         if (sqlite3_column_int(stmt2, 1)) {
diff --git a/src/dblayer.h b/src/dblayer.h
index 13ac187..20ab4b2 100644
--- a/src/dblayer.h
+++ b/src/dblayer.h
@@ -43,7 +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);
+extern int get_program_paths(DB *db, cklist *ckl, const char* pName, int bName, int attr);
 
 /********/
 /* init */
@@ -72,9 +72,9 @@ extern int edit_get_config(DB *db, const char *pName, char *ret, const char *cNa
 /* list */
 /********/
 
-extern int list_get_paths(DB *db, cklist *ckl, int attr);
+extern int list_get_paths(DB *db, cklist *ckl, int bName, int attr);
 extern int list_get_programs(DB *db, cklist *ckl);
-extern int list_get_path_program_tree(DB *db, cklist *ckl, int attr);
+extern int list_get_path_program_tree(DB *db, cklist *ckl, int bName, int attr);
 
 /*******/
 /* del */
-- 
cgit v1.2.3