From 0f33c8a9cbabda257c4dd73009266a4cf5f857a7 Mon Sep 17 00:00:00 2001
From: gramanas <anastasis.gramm2@gmail.com>
Date: Sat, 18 Aug 2018 18:44:14 +0300
Subject: steps towards list

---
 src/actionparser.c |  7 ++++---
 src/actions.c      | 10 +++++++++-
 src/cklist.c       | 17 +++++++++++++----
 src/cklist.h       |  1 +
 src/ckutil.h       |  2 +-
 src/dbhelper.c     | 10 ++++++++++
 src/dbhelper.h     |  1 +
 src/dblayer.c      | 26 +++++++++++++++++++++++---
 src/dblayer.h      |  6 ++++++
 9 files changed, 68 insertions(+), 12 deletions(-)

diff --git a/src/actionparser.c b/src/actionparser.c
index 55fe79c..4b8ed6b 100644
--- a/src/actionparser.c
+++ b/src/actionparser.c
@@ -114,7 +114,7 @@ int parse_EDIT(UserOpt *opt) {
 int parse_LIST(UserOpt *opt) {
   /* List expects a maximum of than 2 arguments */
   if (optNum > pos + 2) {
-    opt->err = PERR_EDIT_WRONG;
+    opt->err = PERR_LIST_WRONG;
     return -1;
   }
 
@@ -196,6 +196,7 @@ void get_config(UserOpt *opt) {
         exit(1);
       }
       if (strcmp(token, ".") == 0){
+        /* WHAT? */
         printf("Dot\n");
       }
       if (!util_is_dir(token)) {
@@ -262,13 +263,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 ProgramName ConfigPath [-s](secret) [-p](primary)", names);
+    sprintf(errStr, "Delete config or program\nUsage: %s ....", names);
     break;
   case PERR_EDIT_WRONG:
     sprintf(errStr, "Edit config with $EDITOR\nUsage: %s ProgramName or configBasename (or both)", names);
     break;
   case PERR_LIST_WRONG:
-    sprintf(errStr, "Usage: .........)");
+    sprintf(errStr, "List programs, configs and more\nUsage: %s [list-type] value-to-list", names);
     break;
   case PERR_SEARCH_WRONG:
     sprintf(errStr, "Usage: .............");
diff --git a/src/actions.c b/src/actions.c
index 680ae68..3cce2f6 100644
--- a/src/actions.c
+++ b/src/actions.c
@@ -172,7 +172,15 @@ int run_LIST(UserOpt *opt, Conf *conf) {
     }
     return 0;
   }
-  list_print_lisp(opt->args);
+
+  cklist *paths = list_make_new();
+
+  list_get_paths(&db, paths);
+  
+  //list_print_lisp(paths);
+  //  list_print_python(opt->args);
+  list_print(paths);
+  list_free(paths);
   close_DB(&db);
   return 0;
 }
diff --git a/src/cklist.c b/src/cklist.c
index d22ee49..e1ca7d8 100644
--- a/src/cklist.c
+++ b/src/cklist.c
@@ -137,16 +137,16 @@ cklist* list_copy_part(cklist *ckl, int from, int until) {
   return _ckl;
 }
 
-void list_print_lisp(cklist *ckl) {
+void print_list_enclose_in(cklist *ckl, const char before, const char after) {
   if (ckl->size > 0) {
     list_rewind(ckl);
-    printf("( \"%s\"", list_get(ckl));
+    printf("%c \"%s\"", before, list_get(ckl));
     if (ckl->size == 1) {
-      printf(" )\n");
+      printf(" %c\n", after);
     }
     while (list_next(ckl)) {
       if (ckl->pos == ckl->size-1) {
-        printf(", \"%s\" )\n", list_get(ckl));
+        printf(", \"%s\" %c\n", list_get(ckl), after);
       }
       else {
         printf(", \"%s\"", list_get(ckl));
@@ -156,9 +156,18 @@ void list_print_lisp(cklist *ckl) {
   }
 }
 
+void list_print_lisp(cklist *ckl) {
+  print_list_enclose_in(ckl, '(', ')');
+}
+
+void list_print_python(cklist *ckl) {
+  print_list_enclose_in(ckl, '[', ']');
+}
+
 void list_print(cklist *ckl) {
   if (ckl->size > 0) {
     list_rewind(ckl);
+    printf("%s\n", list_get(ckl));
     while (list_next(ckl)) {
       printf("%s\n", list_get(ckl));
     }
diff --git a/src/cklist.h b/src/cklist.h
index a7f5cc2..b5608e5 100644
--- a/src/cklist.h
+++ b/src/cklist.h
@@ -50,6 +50,7 @@ extern cklist* list_copy_part(cklist *ckl, int from, int until);
 
 /* rewinds */
 extern void list_print_lisp(cklist *ckl);
+extern void list_print_python(cklist *ckl);
 /* rewinds */
 extern void list_print(cklist *ckl);
 
diff --git a/src/ckutil.h b/src/ckutil.h
index fc7cea2..8f5eda8 100644
--- a/src/ckutil.h
+++ b/src/ckutil.h
@@ -48,7 +48,7 @@
 /* Create the config name to be used when storing a new config to 
  * the VC or SCRT dir */
 extern void str_make_ck_config_name(char *ret, const char *path,
-                                     const char *progName);
+                                    const char *progName);
 
 /* Joins the two strings into ret, with a '/' in between */
 extern void str_join_dirname_with_basename(char *ret, const char *path,
diff --git a/src/dbhelper.c b/src/dbhelper.c
index b6421db..b27e81a 100644
--- a/src/dbhelper.c
+++ b/src/dbhelper.c
@@ -169,3 +169,13 @@ void dbh_format_query_select_from_joined_like(char *query, const char *selection
 
   strcpy(query, tmp);
 }
+
+void dbh_form_query_select_paths(char *query) {
+  char tmp[STR_L] = "SELECT ";
+  strcat(tmp, COL_CONFIG_PATH);
+  strcat(tmp, " FROM ");
+  strcat(tmp, TBL_CONFIG);
+  strcat(tmp, ";");
+
+  strcpy(query, tmp);
+}
diff --git a/src/dbhelper.h b/src/dbhelper.h
index 197171f..d1c279f 100644
--- a/src/dbhelper.h
+++ b/src/dbhelper.h
@@ -56,6 +56,7 @@ extern void dhb_form_query_insert_relationship(char *query);
 extern void dhb_form_query_find_program(char *query);
 extern void dhb_form_query_find_config(char *query);
 extern void dhb_form_query_find_relationship(char *query);
+extern void dbh_form_query_select_paths(char *query);
 extern void dbh_form_query_select_from_joined_eq(char *query, const char *selection, const char* condition);
 extern void dbh_form_query_select_from_joined_like(char *query, const char *selection, const char* condition);
 #endif /* DBHELPER_H */
diff --git a/src/dblayer.c b/src/dblayer.c
index 8014d08..4a356be 100644
--- a/src/dblayer.c
+++ b/src/dblayer.c
@@ -16,13 +16,13 @@
 #include "dbhelper.h"
 #include "ckutil.h"
 
-const char * const DB_NAME = "/ckdb";
+const char * const DB_FILE_NAME = "/ckdb";
 
 /* figure out the database name */
 void make_db_name(char *ret, const char *confPath) {
   char db_path[STR_L];
   strcpy(db_path, confPath);
-  strcat(db_path, DB_NAME);
+  strcat(db_path, DB_FILE_NAME);
 
   strcpy(ret, db_path);
 }
@@ -117,7 +117,7 @@ void init_make_tables(DB *db) {
 
   int rc = sqlite3_exec(db->ptr, sql, 0, 0, 0);
   if (rc != SQLITE_OK ) {
-    PRINT_ERR("Could not create empry db.");
+    PRINT_ERR("Could not create empty db.");
     db->error = SQL_ERR_SQLITE;
     return;
   }
@@ -411,3 +411,23 @@ int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secr
   /* No prime config found */
   return 0;
 }
+
+int list_get_paths(DB *db, cklist *ckl) {
+  sqlite3_stmt *stmt;
+  int rc;
+
+  char sql[STR_M];
+  dbh_form_query_select_paths(sql);
+
+  rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
+  if (rc != SQLITE_OK) {
+    return -2;
+  }
+
+  while (sqlite3_step(stmt) == SQLITE_ROW) {
+    list_add(ckl, (char *)sqlite3_column_text(stmt, 0));
+  }
+  sqlite3_finalize(stmt);
+
+  return 1;
+}
diff --git a/src/dblayer.h b/src/dblayer.h
index cec6290..144da63 100644
--- a/src/dblayer.h
+++ b/src/dblayer.h
@@ -62,4 +62,10 @@ extern int add_transaction_begin(DB *db, const AddOpt * const opt);
 /********/
 
 extern int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secret);
+
+/********/
+/* list */
+/********/
+
+extern int list_get_paths(DB *db, cklist *ckl);
 #endif /* DBLAYER_H */
-- 
cgit v1.2.3