diff options
author | Anastasis Grammenos <anastasis.gramm2@gmail.com> | 2018-10-08 23:47:07 +0300 |
---|---|---|
committer | Anastasis Grammenos <anastasis.gramm2@gmail.com> | 2018-10-08 23:47:07 +0300 |
commit | fe781e5ae7116733e5b335a0ac016af97266db5f (patch) | |
tree | d5cf6ebfc27b84745df5d6e3db04b04d0b524706 | |
parent | f67f2c3d5b67f54dae2c2c2754f43b4351445c80 (diff) | |
download | ck-fe781e5ae7116733e5b335a0ac016af97266db5f.tar.gz ck-fe781e5ae7116733e5b335a0ac016af97266db5f.tar.bz2 ck-fe781e5ae7116733e5b335a0ac016af97266db5f.zip |
Way better Edit
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | README.html | 186 | ||||
-rw-r--r-- | README.org | 34 | ||||
-rwxr-xr-x | res/test-ck (renamed from res/check_ck) | 0 | ||||
-rw-r--r-- | src/actionhelper.c | 26 | ||||
-rw-r--r-- | src/actionparser.c | 2 | ||||
-rw-r--r-- | src/actions.c | 102 | ||||
-rw-r--r-- | src/ckutil.c | 6 | ||||
-rw-r--r-- | src/dblayer.c | 130 | ||||
-rw-r--r-- | src/dblayer.h | 6 |
10 files changed, 342 insertions, 152 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f5117f..b5e474e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,5 +114,5 @@ if (CK_TESTS) configure_file(${TESTS_DIR}/03_delete ${BIN_TESTS_DIR}/03_delete @ONLY) configure_file(${TESTS_DIR}/04_search ${BIN_TESTS_DIR}/04_search @ONLY) # check_ck - configure_file(${RES_DIR}/check_ck check_ck @ONLY) + configure_file(${RES_DIR}/test-ck test-ck @ONLY) endif() diff --git a/README.html b/README.html index e51fc39..2180663 100644 --- a/README.html +++ b/README.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> -<!-- 2018-10-07 Sun 16:25 --> +<!-- 2018-10-08 Mon 23:46 --> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>‎</title> @@ -232,35 +232,35 @@ for the JavaScript code in this tag. <h2>Table of Contents</h2> <div id="text-table-of-contents"> <ul> -<li><a href="#org84db86d">ck</a></li> -<li><a href="#org079cefd">build it</a> +<li><a href="#org1c6aa01">ck</a></li> +<li><a href="#orgbf1d4c5">build it</a> <ul> -<li><a href="#org309e80c">requirements</a></li> -<li><a href="#org41965f5">compiler</a></li> -<li><a href="#org6696ef0">make</a></li> +<li><a href="#org8edf2df">requirements</a></li> +<li><a href="#org30b18cc">compiler</a></li> +<li><a href="#org28e3d41">make</a></li> </ul> </li> -<li><a href="#org06e00e7">for devs</a> +<li><a href="#orgcd61fbc">for devs</a> <ul> -<li><a href="#orgcb6d6ca">CMake options</a></li> -<li><a href="#org73af3d7">tests</a> +<li><a href="#org713cf8f">CMake options</a></li> +<li><a href="#orga6955dd">tests</a> <ul> -<li><a href="#org792dc4d">run tests</a></li> -<li><a href="#orgac7c553">test suite</a></li> +<li><a href="#org6219fcc">run tests</a></li> +<li><a href="#orgd4349ab">test suite</a></li> </ul> </li> </ul> </li> -<li><a href="#org52a2560">manual</a> +<li><a href="#org1d127b7">manual</a> <ul> -<li><a href="#org09a0c5c">ck configuration</a></li> -<li><a href="#orgd61b3e4">Actions</a> +<li><a href="#orge5785e3">ck configuration</a></li> +<li><a href="#orga717caf">Actions</a> <ul> -<li><a href="#org3ca7b35">init</a></li> -<li><a href="#orga12b072">add</a></li> -<li><a href="#orgab0f98c">list</a></li> -<li><a href="#orgdc0a7ed">search</a></li> -<li><a href="#orgd947583">edit</a></li> +<li><a href="#org29304ac">init</a></li> +<li><a href="#org8841e7c">add</a></li> +<li><a href="#orgc9a5e19">list</a></li> +<li><a href="#org73e5182">search</a></li> +<li><a href="#org9b41294">edit</a></li> </ul> </li> </ul> @@ -269,22 +269,22 @@ for the JavaScript code in this tag. </div> </div> <p align="center"><img src="res/logo.png" width="30%" height="30%" /></p> -<div id="outline-container-org84db86d" class="outline-2"> -<h2 id="org84db86d">ck</h2> -<div class="outline-text-2" id="text-org84db86d"> +<div id="outline-container-org1c6aa01" class="outline-2"> +<h2 id="org1c6aa01">ck</h2> +<div class="outline-text-2" id="text-org1c6aa01"> <p> <b>The Config Keeper</b> </p> </div> </div> -<div id="outline-container-org079cefd" class="outline-2"> -<h2 id="org079cefd">build it</h2> -<div class="outline-text-2" id="text-org079cefd"> +<div id="outline-container-orgbf1d4c5" class="outline-2"> +<h2 id="orgbf1d4c5">build it</h2> +<div class="outline-text-2" id="text-orgbf1d4c5"> </div> -<div id="outline-container-org309e80c" class="outline-3"> -<h3 id="org309e80c">requirements</h3> -<div class="outline-text-3" id="text-org309e80c"> +<div id="outline-container-org8edf2df" class="outline-3"> +<h3 id="org8edf2df">requirements</h3> +<div class="outline-text-3" id="text-org8edf2df"> <ul class="org-ul"> <li>clang (llvm) or gcc (gnu)</li> <li>cmake</li> @@ -293,9 +293,9 @@ for the JavaScript code in this tag. </div> </div> -<div id="outline-container-org41965f5" class="outline-3"> -<h3 id="org41965f5">compiler</h3> -<div class="outline-text-3" id="text-org41965f5"> +<div id="outline-container-org30b18cc" class="outline-3"> +<h3 id="org30b18cc">compiler</h3> +<div class="outline-text-3" id="text-org30b18cc"> <div class="org-src-container"> <pre class="src src-sh">> export <span style="color: #ffac4a;">CC</span>=clang <span style="color: #6A6D70;"># </span><span style="color: #6A6D70;">or</span> @@ -305,9 +305,9 @@ for the JavaScript code in this tag. </div> </div> -<div id="outline-container-org6696ef0" class="outline-3"> -<h3 id="org6696ef0">make</h3> -<div class="outline-text-3" id="text-org6696ef0"> +<div id="outline-container-org28e3d41" class="outline-3"> +<h3 id="org28e3d41">make</h3> +<div class="outline-text-3" id="text-org28e3d41"> <div class="org-src-container"> <pre class="src src-sh"><span style="color: #6A6D70;"># </span><span style="color: #6A6D70;">clone the repo</span> > cd ~/code; git clone https://gitlab.com/grm-grm/ck @@ -325,13 +325,13 @@ for the JavaScript code in this tag. </div> </div> -<div id="outline-container-org06e00e7" class="outline-2"> -<h2 id="org06e00e7">for devs</h2> -<div class="outline-text-2" id="text-org06e00e7"> +<div id="outline-container-orgcd61fbc" class="outline-2"> +<h2 id="orgcd61fbc">for devs</h2> +<div class="outline-text-2" id="text-orgcd61fbc"> </div> -<div id="outline-container-orgcb6d6ca" class="outline-3"> -<h3 id="orgcb6d6ca">CMake options</h3> -<div class="outline-text-3" id="text-orgcb6d6ca"> +<div id="outline-container-org713cf8f" class="outline-3"> +<h3 id="org713cf8f">CMake options</h3> +<div class="outline-text-3" id="text-org713cf8f"> <p> cmake accepts the following options: </p> @@ -371,9 +371,9 @@ llvm has better asan than gcc, so I use that. </div> </div> -<div id="outline-container-org73af3d7" class="outline-3"> -<h3 id="org73af3d7">tests</h3> -<div class="outline-text-3" id="text-org73af3d7"> +<div id="outline-container-orga6955dd" class="outline-3"> +<h3 id="orga6955dd">tests</h3> +<div class="outline-text-3" id="text-orga6955dd"> <p> The testing "suite" is a bash script that runs regression and unit tests. Regression tests are under the <code>tests/</code> directory @@ -382,24 +382,25 @@ under <code>unit/</code> directory and test the code. </p> </div> -<div id="outline-container-org792dc4d" class="outline-4"> -<h4 id="org792dc4d">run tests</h4> -<div class="outline-text-4" id="text-org792dc4d"> +<div id="outline-container-org6219fcc" class="outline-4"> +<h4 id="org6219fcc">run tests</h4> +<div class="outline-text-4" id="text-org6219fcc"> <p> -Simply go to the build dir and type. +First make sure you build ck with the <code>-DCK_TESTS=1</code> option. Then +go to the build directory and type: </p> <div class="org-src-container"> -<pre class="src src-sh">$ ./check_ck +<pre class="src src-sh">$ ./test-ck </pre> </div> </div> </div> -<div id="outline-container-orgac7c553" class="outline-4"> -<h4 id="orgac7c553">test suite</h4> -<div class="outline-text-4" id="text-orgac7c553"> +<div id="outline-container-orgd4349ab" class="outline-4"> +<h4 id="orgd4349ab">test suite</h4> +<div class="outline-text-4" id="text-orgd4349ab"> <div class="org-src-container"> -<pre class="src src-sh">$ ./check_ck -h +<pre class="src src-sh">$ ./test-ck -h ck test suite use without flags to run all tests @@ -415,9 +416,9 @@ flags: </div> </div> </div> -<div id="outline-container-org52a2560" class="outline-2"> -<h2 id="org52a2560">manual</h2> -<div class="outline-text-2" id="text-org52a2560"> +<div id="outline-container-org1d127b7" class="outline-2"> +<h2 id="org1d127b7">manual</h2> +<div class="outline-text-2" id="text-org1d127b7"> <p> ck's goal is to assist with the configuration file management. To that end it tries to provides a cli interface that is pretty straight-forward @@ -467,9 +468,9 @@ of flags one has to pass to ck. </p> </div> -<div id="outline-container-org09a0c5c" class="outline-3"> -<h3 id="org09a0c5c">ck configuration</h3> -<div class="outline-text-3" id="text-org09a0c5c"> +<div id="outline-container-orge5785e3" class="outline-3"> +<h3 id="orge5785e3">ck configuration</h3> +<div class="outline-text-3" id="text-orge5785e3"> <p> ck uses sqlite to index the configuration files. The init action creates a <b>.ck</b> directory (under $HOME) @@ -511,13 +512,13 @@ $ ck -c /someplace/else ... </div> </div> -<div id="outline-container-orgd61b3e4" class="outline-3"> -<h3 id="orgd61b3e4">Actions</h3> -<div class="outline-text-3" id="text-orgd61b3e4"> +<div id="outline-container-orga717caf" class="outline-3"> +<h3 id="orga717caf">Actions</h3> +<div class="outline-text-3" id="text-orga717caf"> </div> -<div id="outline-container-org3ca7b35" class="outline-4"> -<h4 id="org3ca7b35">init</h4> -<div class="outline-text-4" id="text-org3ca7b35"> +<div id="outline-container-org29304ac" class="outline-4"> +<h4 id="org29304ac">init</h4> +<div class="outline-text-4" id="text-org29304ac"> <p> or i or -i </p> @@ -547,9 +548,9 @@ $ ck init /path_to/where_you_want/your_configs/to_be <span style="color: #E6DB74 </div> </div> -<div id="outline-container-orga12b072" class="outline-4"> -<h4 id="orga12b072">add</h4> -<div class="outline-text-4" id="text-orga12b072"> +<div id="outline-container-org8841e7c" class="outline-4"> +<h4 id="org8841e7c">add</h4> +<div class="outline-text-4" id="text-org8841e7c"> <p> or a or -a </p> @@ -590,9 +591,9 @@ $ ck add program_name config_path [-s] [-p] </div> </div> -<div id="outline-container-orgab0f98c" class="outline-4"> -<h4 id="orgab0f98c">list</h4> -<div class="outline-text-4" id="text-orgab0f98c"> +<div id="outline-container-orgc9a5e19" class="outline-4"> +<h4 id="orgc9a5e19">list</h4> +<div class="outline-text-4" id="text-orgc9a5e19"> <p> or ls or l or -l </p> @@ -636,9 +637,9 @@ $ ck ls programs -t lisp </div> </div> -<div id="outline-container-orgdc0a7ed" class="outline-4"> -<h4 id="orgdc0a7ed">search</h4> -<div class="outline-text-4" id="text-orgdc0a7ed"> +<div id="outline-container-org73e5182" class="outline-4"> +<h4 id="org73e5182">search</h4> +<div class="outline-text-4" id="text-org73e5182"> <p> or s or -s </p> @@ -687,26 +688,49 @@ $ for i ($(ck ls paths)) grep -E <span style="color: #E6DB74;">'A|B'</span> $<sp </div> </div> -<div id="outline-container-orgd947583" class="outline-4"> -<h4 id="orgd947583">edit</h4> -<div class="outline-text-4" id="text-orgd947583"> +<div id="outline-container-org9b41294" class="outline-4"> +<h4 id="org9b41294">edit</h4> +<div class="outline-text-4" id="text-org9b41294"> <p> or e or -e </p> <p> -<code>=--WIP--=</code> +Edit configurations with <code>$EDITOR</code>. </p> <p> -Currently edit can only edit the primary config of a program. +Edit takes at least one and up to two arguments. </p> <p> -To do it use: +The first argument is the <b>programName</b>. If the program has a primary configuration +edit will open this. If the program has only one configuration edit will open it. +If the program has more than 1 configurations and no primary, it will print the +avaliable configurations and exit. +</p> + +<p> +The second argument is the <b>configName</b>. If it exists it will open, else it will +print the avaliable configurations and exit. +</p> + +<p> +Usage: </p> <div class="org-src-container"> -<pre class="src src-sh">$ ck edit program_name +<pre class="src src-sh"><span style="color: #6A6D70;"># </span><span style="color: #6A6D70;">suppose this is our ck instance</span> +$ ck list tree -a +emacs: +|- init.el +|- accounts.el [s] +|- orgconf.org [p] + +<span style="color: #6A6D70;"># </span><span style="color: #6A6D70;">edit the primary emacs config</span> +$ ck edit emacs + +<span style="color: #6A6D70;"># </span><span style="color: #6A6D70;">edit a specific emacs config, other than the primary</span> +$ ck edit emacs accounts.el </pre> </div> </div> @@ -715,7 +739,7 @@ To do it use: </div> </div> <div id="postamble" class="status"> -<p class="date">Created: 2018-10-07 Sun 16:25</p> +<p class="date">Created: 2018-10-08 Mon 23:46</p> <p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p> </div> </body> @@ -67,14 +67,15 @@ and are bash scripts that test =ck= functionality. Unit tests reside under =unit/= directory and test the code. *** run tests -Simply go to the build dir and type. +First make sure you build ck with the =-DCK_TESTS=1= option. Then +go to the build directory and type: #+BEGIN_SRC sh -$ ./check_ck +$ ./test-ck #+END_SRC *** test suite #+BEGIN_SRC sh -$ ./check_ck -h +$ ./test-ck -h ck test suite use without flags to run all tests @@ -259,11 +260,30 @@ a different pattern matching program you can do it like so: *** edit or e or -e -==--WIP--== +Edit configurations with =$EDITOR=. -Currently edit can only edit the primary config of a program. +Edit takes at least one and up to two arguments. -To do it use: +The first argument is the *programName*. If the program has a primary configuration +edit will open this. If the program has only one configuration edit will open it. +If the program has more than 1 configurations and no primary, it will print the +avaliable configurations and exit. + +The second argument is the *configName*. If it exists it will open, else it will +print the avaliable configurations and exit. + +Usage: #+BEGIN_SRC sh -$ ck edit program_name +# suppose this is our ck instance +$ ck list tree -a +emacs: +|- init.el +|- accounts.el [s] +|- orgconf.org [p] + +# edit the primary emacs config +$ ck edit emacs + +# edit a specific emacs config, other than the primary +$ ck edit emacs accounts.el #+END_SRC diff --git a/res/check_ck b/res/test-ck index 423d3bd..423d3bd 100755 --- a/res/check_ck +++ b/res/test-ck diff --git a/src/actionhelper.c b/src/actionhelper.c index 883229f..3e898af 100644 --- a/src/actionhelper.c +++ b/src/actionhelper.c @@ -15,6 +15,9 @@ #include <libgen.h> #include "actionhelper.h" +#include "ckerrlog.h" + +ERRLOG(action); char add_err[STR_M] = ""; @@ -146,11 +149,8 @@ void add_make_link(const AddOpt *opt, const Conf *conf) { } } -edit_rc -edit_get_config_or_suggestions(cklist *args, char *ret) { +int edit_make_options(cklist *args) { UNUSED(args); - UNUSED(ret); - return ERC_ERR; } ListOpt list_make_options(cklist *args) { @@ -210,50 +210,48 @@ ListOpt list_make_options(cklist *args) { void print_INIT_result(int err) { if (!err) { - printf("Initialized empty ckdb.\n"); + HELP("Initialized empty ckdb."); } } void print_ADD_result(int err) { if (!err) { - printf("ckdb updated succesfully.\n"); + HELP("ckdb updated succesfully."); return; } - printf("Could not complete add transaction.\n"); + ERR("Could not complete add transaction."); } void print_DEL_result(int err) { if (!err) { - printf("succes\n"); + HELP("ckdb updated succesfully"); return; } - printf("Not Supported\n"); + ERR("Could not complete delete transaction"); } void print_EDIT_result(int err) { if (!err) { - printf("succes\n"); return; } - printf("failure\n"); } void print_LIST_result(int err) { if (!err) { return; } - printf("Wrong list arguments\n"); + ERR("Wrong list arguments"); } void print_SEARCH_result(int err) { if (err == 2) { - printf("No grep avaliable. Please make sure you have grep installed.\n"); + ERR("No grep avaliable. Please make sure you have grep installed."); return; } if (!err) { return; } - printf("Wrong search.\n"); + ERR("Wrong search."); } void print_HELP_result(int err) { diff --git a/src/actionparser.c b/src/actionparser.c index 25c7c19..7c1186a 100644 --- a/src/actionparser.c +++ b/src/actionparser.c @@ -332,7 +332,7 @@ void print_parser_error(UserOpt *opt) { 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); + 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); diff --git a/src/actions.c b/src/actions.c index d5f9a29..4ff6816 100644 --- a/src/actions.c +++ b/src/actions.c @@ -102,7 +102,6 @@ int run_DEL(UserOpt * opt, Conf *conf) { } int run_EDIT(UserOpt *opt, Conf *conf) { - printf("Running %s\n", "edit"); DB db = open_DB(opt); if (db.ptr == NULL) { if (db.error == SQL_ERR_NO_TABLES) { @@ -112,31 +111,52 @@ int run_EDIT(UserOpt *opt, Conf *conf) { } list_rewind(opt->args); - char confPath[STR_M]; - if (list_size(opt->args) == 1) { - char confName[STR_M]; - int secret = 0; - if (edit_get_prime_config_from_program(&db, list_get(opt->args), confName, &secret) == 1) { - str_join_dirname_with_basename(confPath, secret ? conf->scrt_dir : conf->vc_dir, confName); - printf("%s\n", confPath); - } else { - PRINT_ERR("No primary config"); - close_DB(&db); - return 1; + 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."); + cklist *paths = list_make_new(); + edit_get_avaliable_paths(&db, paths, pName); + list_print(paths); + list_free(paths); + goto error; + } } - } else { - close_DB(&db); - char confName[STR_L]; - switch (edit_get_config_or_suggestions(opt->args, confName)) { - case ERC_OK: - return 0; - case ERC_ERR: - return 1; - case ERC_SUGGESTIONS: - return 1; + } + /* 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); + cklist *paths = list_make_new(); + edit_get_avaliable_paths(&db, paths, pName); + list_print(paths); + list_free(paths); + goto error; } } close_DB(&db); + str_join_dirname_with_basename(confPath, secret ? conf->scrt_dir : conf->vc_dir, confName); + HELP("Editing %s", confPath); char *editor = getenv("EDITOR"); char command[STR_L]; @@ -145,6 +165,9 @@ int run_EDIT(UserOpt *opt, Conf *conf) { strcat(command, confPath); system(command); return 0; + error: + close_DB(&db); + return 1; } int run_LIST(UserOpt *opt, Conf *conf) { @@ -157,45 +180,44 @@ int run_LIST(UserOpt *opt, Conf *conf) { return 1; } - cklist *list_type = list_make_new(); + cklist *the_list = list_make_new(); ListOpt listOpt = list_make_options(opt->args); if (listOpt.err) { - close_DB(&db); - list_free(list_type); - return 1; + goto error; } switch(listOpt._lt) { case LT_PATH: - list_get_paths(&db, list_type, listOpt.attr); + list_get_paths(&db, the_list, listOpt.attr); break; case LT_PROGRAM: - list_get_programs(&db, list_type); + list_get_programs(&db, the_list); break; case LT_TREE: - list_get_path_program_tree(&db, list_type, listOpt.attr); - list_print(list_type); - close_DB(&db); - list_free(list_type); - return 0; + list_get_path_program_tree(&db, the_list, listOpt.attr); + list_print(the_list); + goto close; case LT_NONE: - close_DB(&db); - list_free(list_type); - return 1; + goto error; } switch(listOpt._lst) { case LST_PLAIN: - list_print(list_type); + list_print(the_list); break; case LST_LISP: - list_print_lisp(list_type); + list_print_lisp(the_list); break; case LST_PYTHON: - list_print_python(list_type); + list_print_python(the_list); } + close: close_DB(&db); - list_free(list_type); + list_free(the_list); return 0; + error: + close_DB(&db); + list_free(the_list); + return 1; } FILE *popen(const char *command, const char *mode); diff --git a/src/ckutil.c b/src/ckutil.c index a477b36..9bfa139 100644 --- a/src/ckutil.c +++ b/src/ckutil.c @@ -97,13 +97,11 @@ int util_symlink_file(const char *path, const char* dest) { } void str_make_ck_config_name(char *ret, const char *path, - const char *progName) { + const char *progName) { char *basec = strdup(path); char *bname = basename(basec); - strcpy(ret, progName); - strcat(ret, "_"); - strcat(ret, bname); + str_join_dirname_with_basename(ret, progName, bname); free(basec); } diff --git a/src/dblayer.c b/src/dblayer.c index 8ec062c..4290a7a 100644 --- a/src/dblayer.c +++ b/src/dblayer.c @@ -12,6 +12,8 @@ * Give access to the database. * * -------------------------------------------------------------------------- */ +#include <libgen.h> + #include "dblayer.h" #include "dbhelper.h" #include "ckutil.h" @@ -254,6 +256,13 @@ int get_program_id(DB *db, const char* name) { return id; } +int program_exists(DB *db, const char *pName) { + if (get_program_id(db, pName) == -1) { + return 0; + } + return 1; +} + int get_config_id(DB *db, const char* path) { sqlite3_stmt *stmt; int rc; @@ -281,7 +290,6 @@ int add_insert_relationship(DB *db, const int pid, const int cid) { return insert_to_rel_table(db, pid, cid); } - /* Returns the path of the found config via *ret */ int program_has_primary_config(DB *db, const int pid, char *ret, int *sec) { sqlite3_stmt *stmt; @@ -403,13 +411,116 @@ int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secr str_make_ck_config_name(confName, path, pName); strcpy(ret, confName); } - return 1; + return 0; } } } /* No prime config found */ - return 0; + return -1; +} + +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) { + return -1; + } + + /* program exists */ + if (pid > -1) { + sqlite3_stmt *stmt; + int rc; + + char selection[STR_M] = COL_CONFIG_PATH; + strcat(selection, ", "); + strcat(selection, COL_CONFIG_SECRET); + char condition[STR_M] = TBL_PROGRAM; + strcat(condition, "."); + strcat(condition, COL_PROGRAM_ID); + + char sql[STR_L]; + dbh_form_query_select_from_joined_eq(sql, selection, condition); + + rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); + sqlite3_bind_int(stmt, 1, pid); + if (rc != SQLITE_OK) { + return -2; + } + + int flag = -1; + while (sqlite3_step(stmt) == SQLITE_ROW) { + char confName[STR_M]; + if (cName) { + char *tmp = strdup((char *)sqlite3_column_text(stmt, 0)); + if (strcmp(cName, basename(tmp)) == 0) { + flag = 0; + char confName[STR_M]; + str_make_ck_config_name(confName, (char *)sqlite3_column_text(stmt, 0), pName); + strcpy(ret, confName); + if (sec) { + *sec = sqlite3_column_int(stmt, 1); + } + } + free(tmp); + } + else { + char confName[STR_M]; + str_make_ck_config_name(confName, (char *)sqlite3_column_text(stmt, 0), pName); + strcpy(ret, confName); + flag = 0; + if (sec) { + *sec = sqlite3_column_int(stmt, 1); + } + break; + } + } + sqlite3_finalize(stmt); + return flag; + } +} + +int edit_get_avaliable_paths(DB *db, cklist *ckl, const char* pName) { + int pid = get_program_id(db, pName); + /* error */ + if (pid == -2) { + return -1; + } + + /* program exists */ + if (pid > -1) { + sqlite3_stmt *stmt; + int rc; + + char selection[STR_M] = COL_CONFIG_PATH; + char condition[STR_M] = TBL_PROGRAM; + strcat(condition, "."); + strcat(condition, COL_PROGRAM_ID); + + char sql[STR_L]; + dbh_form_query_select_from_joined_eq(sql, selection, condition); + + rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); + sqlite3_bind_int(stmt, 1, pid); + if (rc != SQLITE_OK) { + 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)); + list_add(ckl, entry); + free(tmp); + } + sqlite3_finalize(stmt); + return 0; + } } int list_get_paths(DB *db, cklist *ckl, int attr) { @@ -619,6 +730,19 @@ int get_program_relations(DB *db, int pid) { return count; } +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); + } +} + /* Removes the relationship of `cid` with the corresponding program. * Returns the program's pid on succes, negative integer otherwise. */ diff --git a/src/dblayer.h b/src/dblayer.h index cf01ccf..05ba5bd 100644 --- a/src/dblayer.h +++ b/src/dblayer.h @@ -41,7 +41,8 @@ extern int db_exists(const UserOpt *opt); * and the corresponding SQL error (NO_DB_FILE | NO_TABLES)*/ extern DB open_DB(const UserOpt *opt); -extern void close_DB(DB *DB); +extern void close_DB(DB *db); +extern int program_exists(DB *db, const char *pName); /********/ /* init */ @@ -63,6 +64,9 @@ 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 */ |