From 22fca0324c162fa9a6abc5192aea2b5453c057b5 Mon Sep 17 00:00:00 2001 From: Anastasis Grammenos Date: Wed, 19 Sep 2018 23:59:37 +0300 Subject: Add list action (no docs) --- src/actionparser.c | 4 +-- src/actions.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++-------- src/actions.h | 24 +++++++++++++- src/cksearch.h | 25 ++++++++++++++ src/confparser.c | 1 - src/dbhelper.c | 10 ++++++ src/dbhelper.h | 1 + src/dblayer.c | 20 ++++++++++++ src/dblayer.h | 2 ++ 9 files changed, 165 insertions(+), 17 deletions(-) create mode 100644 src/cksearch.h (limited to 'src') diff --git a/src/actionparser.c b/src/actionparser.c index 4b8ed6b..de162c2 100644 --- a/src/actionparser.c +++ b/src/actionparser.c @@ -112,8 +112,8 @@ int parse_EDIT(UserOpt *opt) { return 1; } int parse_LIST(UserOpt *opt) { - /* List expects a maximum of than 2 arguments */ - if (optNum > pos + 2) { + /* List expects a maximum of than 3 arguments */ + if (optNum > pos + 3) { opt->err = PERR_LIST_WRONG; return -1; } diff --git a/src/actions.c b/src/actions.c index 3cce2f6..7d5276a 100644 --- a/src/actions.c +++ b/src/actions.c @@ -8,6 +8,9 @@ * GPLv3 (see LICENCE for the full notice) * * -------------------------------------------------------------------------- */ +#include +#include + #include "actions.h" #include "actionhelper.h" #include "dblayer.h" @@ -37,7 +40,6 @@ AddOpt make_add_options(cklist* args) { /* since we are here, the first two argumens must exist */ AddOpt addOpt = { .progName = list_get(args), - .confPath = NULL, .secret = 0, .prime = 0, .err = ADD_NO_ERR @@ -48,7 +50,7 @@ AddOpt make_add_options(cklist* args) { addOpt.err = ADD_ERR_WRONG_CONFIG; return addOpt; } - addOpt.confPath = list_get(args); + realpath(list_get(args), addOpt.confPath); while (list_next(args)) { if (strcmp(list_get(args), "-s") == 0 && addOpt.secret == 0) { @@ -163,8 +165,50 @@ int run_EDIT(UserOpt *opt, Conf *conf) { return 1; } +ListOpt make_list_options(cklist *args) { + list_rewind(args); + ListOpt listOpt = { + ._lt = LT_NONE, + ._lst = LST_PLAIN, + .err = 0 + }; + + if (list_size(args)) { + do { + if (strcmp(list_get(args), "-t") == 0) { + if (!list_next(args)) { + listOpt.err = 1; + break; + } + if (strcmp(list_get(args), "plain") == 0) { + listOpt._lst = LST_PLAIN; + } + else if (strcmp(list_get(args), "lisp") == 0) { + listOpt._lst = LST_LISP; + } + else if (strcmp(list_get(args), "python") == 0) { + listOpt._lst = LST_PYTHON; + } + else { + listOpt.err = 1; + } + } + else if (strcmp(list_get(args), "paths") == 0) { + listOpt._lt = LT_PATH; + } + else if (strcmp(list_get(args), "programs") == 0) { + listOpt._lt = LT_PROGRAM; + } + else { + listOpt.err = 1; + } + } while(list_next(args)); + } + list_rewind(args); + return listOpt; +} + int run_LIST(UserOpt *opt, Conf *conf) { - printf("Running %s\n", "list"); DB db = open_DB(opt); if (db.ptr == NULL) { if (db.error == SQL_ERR_NO_TABLES) { @@ -173,20 +217,46 @@ int run_LIST(UserOpt *opt, Conf *conf) { return 0; } - cklist *paths = list_make_new(); + cklist *list_type = list_make_new(); - list_get_paths(&db, paths); - - //list_print_lisp(paths); - // list_print_python(opt->args); - list_print(paths); - list_free(paths); + ListOpt listOpt = make_list_options(opt->args); + if (listOpt.err) { + close_DB(&db); + list_free(list_type); + return 0; + } + switch(listOpt._lt) { + case LT_PATH: + list_get_paths(&db, list_type); + break; + case LT_PROGRAM: + list_get_programs(&db, list_type); + break; + case LT_NONE: + printf("What should I list? (paths, configs)\n"); + close_DB(&db); + list_free(list_type); + return 0; + } + switch(listOpt._lst) { + case LST_PLAIN: + list_print(list_type); + break; + case LST_LISP: + list_print_lisp(list_type); + break; + case LST_PYTHON: + list_print_python(list_type); + } close_DB(&db); - return 0; + list_free(list_type); + return 1; } int run_SEARCH(UserOpt *opt, Conf *conf) { printf("Running %s\n", "search"); + DB db = open_DB(opt); + close_DB(&db); return 0; } @@ -227,10 +297,9 @@ void print_EDIT_result(int ok) { void print_LIST_result(int ok) { if (ok) { - printf("succes\n"); return; } - printf("Not Supported\n"); + printf("Wrong list arguments\n"); } void print_SEARCH_result(int ok) { diff --git a/src/actions.h b/src/actions.h index a429a65..0e8d445 100644 --- a/src/actions.h +++ b/src/actions.h @@ -15,6 +15,7 @@ #ifndef ACTIONS_H #define ACTIONS_H +#include "ckutil.h" #include "actionparser.h" #include "confparser.h" @@ -38,10 +39,31 @@ enum AddOptErrors { typedef struct AddOptions AddOpt; struct AddOptions { char *progName; - char *confPath; + char confPath[STR_L]; int secret; int prime; AddOptErr err; }; +typedef enum ListTypes ListType; +enum ListTypes { + LT_PATH, + LT_PROGRAM, + LT_NONE +}; + +typedef enum ListShowTypes ListShowType; +enum ListShowTypes { + LST_PLAIN, + LST_LISP, + LST_PYTHON, +}; + +typedef struct ListOptions ListOpt; +struct ListOptions { + ListType _lt; + ListShowType _lst; + int err; +}; + #endif /* ACTIONS_H */ diff --git a/src/cksearch.h b/src/cksearch.h new file mode 100644 index 0000000..ee06a15 --- /dev/null +++ b/src/cksearch.h @@ -0,0 +1,25 @@ +/* cksearch.h - search for stuff in ck ---------------------------------*- C -*- + * + * This file is part of ck, the config keeper + * + * ----------------------------------------------------------------------------- + * + * Copyright (C) 2018 Anastasis Grammenos + * GPLv3 (see LICENCE for the full notice) + * + * ----------------------------------------------------------------------------- + * + * Search the database and get results for programs and confing paths. + * Provides functions to match user's query to actual entries in the + * database. + * + * (TODO) Also implements the infile search, to match strings in the files + * contained in the database. + * + * -------------------------------------------------------------------------- */ +#ifndef CKSEARCH_H +#define CKSEARCH_H + + + +#endif /* CKSEARCH_H */ diff --git a/src/confparser.c b/src/confparser.c index 948623c..0152825 100644 --- a/src/confparser.c +++ b/src/confparser.c @@ -13,7 +13,6 @@ #include #include "ckutil.h" -#include "cklist.h" #include "confparser.h" diff --git a/src/dbhelper.c b/src/dbhelper.c index b27e81a..d0c2795 100644 --- a/src/dbhelper.c +++ b/src/dbhelper.c @@ -179,3 +179,13 @@ void dbh_form_query_select_paths(char *query) { strcpy(query, tmp); } + +void dbh_form_query_select_programs(char *query) { + char tmp[STR_L] = "SELECT "; + strcat(tmp, COL_PROGRAM_NAME); + strcat(tmp, " FROM "); + strcat(tmp, TBL_PROGRAM); + strcat(tmp, ";"); + + strcpy(query, tmp); +} diff --git a/src/dbhelper.h b/src/dbhelper.h index d1c279f..44b9aa0 100644 --- a/src/dbhelper.h +++ b/src/dbhelper.h @@ -57,6 +57,7 @@ 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_programs(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 4a356be..2e19c5a 100644 --- a/src/dblayer.c +++ b/src/dblayer.c @@ -431,3 +431,23 @@ int list_get_paths(DB *db, cklist *ckl) { return 1; } + +int list_get_programs(DB *db, cklist *ckl) { + sqlite3_stmt *stmt; + int rc; + + char sql[STR_M]; + dbh_form_query_select_programs(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 144da63..58b0f0c 100644 --- a/src/dblayer.h +++ b/src/dblayer.h @@ -68,4 +68,6 @@ extern int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, in /********/ extern int list_get_paths(DB *db, cklist *ckl); +extern int list_get_programs(DB *db, cklist *ckl); + #endif /* DBLAYER_H */ -- cgit v1.2.3