aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2018-04-15 06:15:06 +0300
committergramanas <anastasis.gramm2@gmail.com>2018-04-15 06:15:06 +0300
commit6f319efab77e88e06acb689923ad619bf4c78304 (patch)
tree8d13fc584108627efb5d6b769e64a690b7d9552a /src
parentc42092733d664c29d3ac9084859f57ec463f72e3 (diff)
downloadck-6f319efab77e88e06acb689923ad619bf4c78304.tar.gz
ck-6f319efab77e88e06acb689923ad619bf4c78304.tar.bz2
ck-6f319efab77e88e06acb689923ad619bf4c78304.zip
variable conf
Diffstat (limited to 'src')
-rw-r--r--src/actionparser.c93
-rw-r--r--src/actionparser.h12
-rw-r--r--src/actions.c87
-rw-r--r--src/actions.h5
-rw-r--r--src/ck.c38
-rw-r--r--src/dblayer.c10
-rw-r--r--src/dblayer.h3
-rwxr-xr-xsrc/tests/init2
8 files changed, 170 insertions, 80 deletions
diff --git a/src/actionparser.c b/src/actionparser.c
index 33e8ccc..8261cf7 100644
--- a/src/actionparser.c
+++ b/src/actionparser.c
@@ -2,6 +2,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
#include "actionparser.h"
/* --------------------------------------------------------------------------
@@ -18,6 +22,7 @@ const char* const strEDIT[] = {"3", "edit", "e", "-e"};
const char* const strLIST[] = {"3", "list", "ls", "-ls"};
const char* const strSEARCH[] = {"3", "search", "s", "-s"};
const char* const strHELP[] = {"4", "help", "?", "-h", "--help"};
+const char* const strConfDir[] = {"3", "config", "conf", "-c"};
/* Number of opts */
static int optNum;
@@ -44,21 +49,19 @@ int nextToken() {
}
void getOpt(int position, UserOpt *opt) {
- // get arg
- nextToken();
+ // get arg
+ nextToken();
- // allocate memory
- opt->argv[position] = (char *)malloc((strlen(token)+1)*sizeof(char));
- strcpy(opt->argv[position], token);
+ // allocate memory
+ opt->argv[position] = (char *)malloc((strlen(token)+1)*sizeof(char));
+ strcpy(opt->argv[position], token);
}
/* When starting to parse the action,
* `pos` should be at 2
* like so "ck ACTION ..."
* ^ */
-int parseINIT(UserOpt *opt) {
- // if db exists init should fail before checking for args
-
+int parse_INIT(UserOpt *opt) {
// INIT expects 2 arguments
// starting from 0
opt->argc = 2;
@@ -73,25 +76,30 @@ int parseINIT(UserOpt *opt) {
return 1;
}
-int parseADD(UserOpt *opt) {
+int parse_ADD(UserOpt *opt) {
// ADD expects 2 to 4 arguments
if (optNum <= pos + 1
|| optNum > pos + 4) {
opt->err = PERR_ADD_WRONG;
return -1;
}
+
+ opt->argc = optNum - pos;
+ for (int i = 0; i < opt->argc; i++) {
+ getOpt(i, opt);
+ }
return 1;
}
-int parseDEL(UserOpt *opt) {
+int parse_DEL(UserOpt *opt) {
}
-int parseEDIT(UserOpt *opt) {
+int parse_EDIT(UserOpt *opt) {
}
-int parseLIST(UserOpt *opt) {
+int parse_LIST(UserOpt *opt) {
}
-int parseSEARCH(UserOpt *opt) {
+int parse_SEARCH(UserOpt *opt) {
}
-int parseHELP(UserOpt *opt) {
+int parse_HELP(UserOpt *opt) {
}
@@ -99,7 +107,7 @@ int parseVals(UserOpt *opt) {
switch (opt->action) {
#define X(ACTION) \
case CKA_##ACTION: \
- return parse##ACTION(opt);
+ return parse_##ACTION(opt);
CK_ACTIONS
#undef X
default:
@@ -135,6 +143,38 @@ UserOpt initUserOpt() {
return uo;
}
+
+void getConfig(UserOpt *opt) {
+ // get first token
+ nextToken();
+ int ok = 1;
+ for (int i = 1; i < atoi(strConfDir[0]) + 1; i++) {
+ if (strcmp(token, strConfDir[i]) == 0) {
+ if (nextToken() == -1) {
+ printf("Config needs a value\n");
+ exit(1);
+ }
+ struct stat st = {0};
+ if (stat(token, &st) == -1) {
+ printf("%s is not a directory\n", token);
+ exit(1);
+ }
+ opt->confDir = malloc(sizeof(token));
+ strcpy(opt->confDir, token);
+ return;
+ }
+ }
+ char * defaultConf = "/.ck";
+ char * home = getenv("HOME");
+ opt->confDir = malloc(sizeof(defaultConf)+sizeof(home));
+ strcpy(opt->confDir, home);
+ strcat(opt->confDir, defaultConf);
+
+ // rewind
+ pos = pos - 1;
+ token = opts[pos];
+}
+
ParseResult parseAction(int argc, char* argv[], UserOpt *opt) {
*opt = initUserOpt();
if (argc < 2) {
@@ -146,6 +186,7 @@ ParseResult parseAction(int argc, char* argv[], UserOpt *opt) {
// skip the program nake
nextToken();
+ getConfig(opt);
// get action
nextToken();
opt->action = determineAction();
@@ -200,7 +241,7 @@ void printParserError(UserOpt *opt) {
asprintf(&errStr, "Unknown action: %s", token);
break;
case PERR_INIT_WRONG:
- asprintf(&errStr, "Initialize database\nUsage: %s VC_dir SCRT_dir DB_dir", getPossibleActionName(strINIT));
+ asprintf(&errStr, "Initialize database\nUsage: %s version_control_dir secret_dir", getPossibleActionName(strINIT));
break;
case PERR_ADD_WRONG:
asprintf(&errStr, "Add config (new or existing)\nUsage: %s ProgramName ConfigPath [-s](secret) [-p](primary)", getPossibleActionName(strADD));
@@ -226,16 +267,14 @@ void printParserError(UserOpt *opt) {
}
void printParserHelp() {
- printf("ck - the config keeper");
- printf("\n----------------------");
- printf("\n----------------------\n");
- printf("Usage:\n");
- printf("Initialize: \t%s\n", getPossibleActionName(strINIT));
- printf("Add config: \t%s\n", getPossibleActionName(strADD));
- printf("Delete config: \t%s\n", getPossibleActionName(strDEL));
- printf("Edit config: \t%s\n", getPossibleActionName(strEDIT));
- printf("List configs: \t%s\n", getPossibleActionName(strLIST));
- printf("Search: \t%s\n", getPossibleActionName(strSEARCH));
- printf("Print this: \t%s\n", getPossibleActionName(strHELP));
+ printf("ck - the config keeper\n" );
+ printf("Usage:\n" );
+ printf("Initialize: \t%s\n", getPossibleActionName(strINIT));
+ printf("Add config: \t%s\n", getPossibleActionName(strADD));
+ printf("Delete config: \t%s\n", getPossibleActionName(strDEL));
+ printf("Edit config: \t%s\n", getPossibleActionName(strEDIT));
+ printf("List configs: \t%s\n", getPossibleActionName(strLIST));
+ printf("Search: \t%s\n", getPossibleActionName(strSEARCH));
+ printf("Print this: \t%s\n", getPossibleActionName(strHELP));
exit(0);
}
diff --git a/src/actionparser.h b/src/actionparser.h
index 97d9d52..a097464 100644
--- a/src/actionparser.h
+++ b/src/actionparser.h
@@ -38,13 +38,10 @@ enum ParseErrors {
typedef enum CkActions CkAction;
enum CkActions {
- CKA_INIT,
- CKA_ADD, // program, path, primary, secret
- CKA_DEL, // program regexp, if only programm, delete everything related
- CKA_EDIT, // program regexp, if only program, edit primary
- CKA_LIST, // list_type{tree,paths,programs}
- CKA_SEARCH, // search_mode, regexp
- CKA_HELP // help
+#define X(ACTION) \
+ CKA_##ACTION,
+ CK_ACTIONS
+#undef X
};
typedef enum OptParserResults ParseResult;
@@ -58,6 +55,7 @@ typedef struct UserOptions UserOpt;
struct UserOptions {
ParseError err;
CkAction action;
+ char *confDir;
int argc;
char *argv[10]; // action's options
};
diff --git a/src/actions.c b/src/actions.c
index 9840722..881c299 100644
--- a/src/actions.c
+++ b/src/actions.c
@@ -50,66 +50,101 @@ int run_INIT(UserOpt * opt, Conf *conf) {
if (db.error == SQL_NO_ERR) {
init_make_tables(&db);
}
- sqlite3_close(db.get);
+ sqlite3_close(db.ptr);
return 1;
}
int run_ADD(UserOpt * opt, Conf *conf) {
- if (!db_exists()) {
- printf("ck is not initialized.\nRun ck init first.");
- return 0;
- }
printf("Running %s\n", "add");
DB db = open_DB();
- if (db.get == NULL) {
+ if (db.ptr == NULL) {
if (db.error == SQL_ERR_NO_TABLES) {
printf("no tables\n");
}
}
+ for (int i = 0; i < opt->argc; i++) {
+ printf("[%d]: %s\n", i, opt->argv[i]);
+ }
return 0;
}
int run_DEL(UserOpt * opt, Conf *conf) {
- if (!db_exists()) {
- printf("ck is not initialized.\nRun ck init first.");
- return 0;
- }
printf("Running %s\n", "del");
return 0;
}
int run_EDIT(UserOpt * opt, Conf *conf) {
- if (!db_exists()) {
- printf("ck is not initialized.\nRun ck init first.");
- return 0;
- }
printf("Running %s\n", "edit");
return 0;
}
int run_LIST(UserOpt * opt, Conf *conf) {
- if (!db_exists()) {
- printf("ck is not initialized.\nRun ck init first.");
- return 0;
- }
printf("Running %s\n", "list");
return 0;
}
int run_SEARCH(UserOpt * opt, Conf *conf) {
- if (!db_exists()) {
- printf("ck is not initialized.\nRun ck init first.");
- return 0;
- }
printf("Running %s\n", "search");
return 0;
}
int run_HELP(UserOpt * opt, Conf *conf) {
- if (!db_exists()) {
- printf("ck is not initialized.\nRun ck init first.");
- return 0;
- }
printf("Running %s\n", "help");
return 0;
}
+
+void print_INIT_result(int ok) {
+ if (ok) {
+ printf("Initialized empty ckdb.\n");
+ return;
+ }
+}
+
+void print_ADD_result(int ok) {
+ if (ok) {
+ printf("succes\n");
+ return;
+ }
+ printf("failure\n");
+}
+
+void print_DEL_result(int ok) {
+ if (ok) {
+ printf("succes\n");
+ return;
+ }
+ printf("failure\n");
+}
+
+void print_EDIT_result(int ok) {
+ if (ok) {
+ printf("succes\n");
+ return;
+ }
+ printf("failure\n");
+}
+
+void print_LIST_result(int ok) {
+ if (ok) {
+ printf("succes\n");
+ return;
+ }
+ printf("failure\n");
+}
+
+void print_SEARCH_result(int ok) {
+ if (ok) {
+ printf("succes\n");
+ return;
+ }
+ printf("failure\n");
+}
+
+void print_HELP_result(int ok) {
+ if (ok) {
+ printf("succes\n");
+ return;
+ }
+ printf("failure\n");
+}
+
diff --git a/src/actions.h b/src/actions.h
index 9fc0f45..f75dd80 100644
--- a/src/actions.h
+++ b/src/actions.h
@@ -17,4 +17,9 @@
CK_ACTIONS
#undef X
+#define X(ACTION) \
+ extern void print_##ACTION##_result(int ok);
+CK_ACTIONS
+#undef X
+
#endif /* ACTIONS_H */
diff --git a/src/ck.c b/src/ck.c
index 56dec2e..2833176 100644
--- a/src/ck.c
+++ b/src/ck.c
@@ -7,18 +7,7 @@
#include "confparser.h"
#include "dblayer.h"
- int main(int argc, char *argv[]) {
- sqlite3 *db;
- int rc;
-
- Conf conf;
- config_file_parse(&conf);
- //return 1;
-
-#define X(var, str, name) \
- printf("%s: %s\n", name, conf.var);
- CONFIG_VARIABLES_TABLE
-#undef X
+int main(int argc, char *argv[]) {
UserOpt opt;
switch(parseAction(argc, argv, &opt)) {
case OPR_HELP:
@@ -27,18 +16,37 @@
printParserError(&opt);
case OPR_OK:
break;
- //
}
+ printf("%s\n", opt.confDir);
+ Conf conf;
+ if (opt.action != CKA_INIT) {
+ if (!db_exists()) {
+ printf("ck is not initialized.\nRun ck init first.\n");
+ return 1;
+ }
+ if (!config_file_parse(&conf)) {
+ return 1;
+ }
+ }
+
+ int ok;
switch(opt.action) {
#define X(ACTION) \
case CKA_##ACTION: \
- run_##ACTION(&opt, &conf); \
+ ok = run_##ACTION(&opt, &conf); \
break;
CK_ACTIONS
#undef X
-
}
+ switch(opt.action) {
+#define X(ACTION) \
+ case CKA_##ACTION: \
+ print_##ACTION##_result(ok); \
+ break;
+ CK_ACTIONS
+#undef X
+ }
return 0;
}
diff --git a/src/dblayer.c b/src/dblayer.c
index eee60a6..180cd93 100644
--- a/src/dblayer.c
+++ b/src/dblayer.c
@@ -51,11 +51,15 @@ int check_initialized_DB(sqlite3 *db) {
}
DB empty_DB(SqlError err) {
- return (DB){ .get = NULL, .error = err };
+ return (DB){ .ptr = NULL, .error = err };
}
DB new_DB(sqlite3 *db) {
- return (DB){ .get = db, .error = SQL_NO_ERR };
+ return (DB){ .ptr = db, .error = SQL_NO_ERR };
+}
+
+void close_DB(DB *db) {
+ sqlite3_close(db->ptr);
}
DB init_make_DB() {
@@ -102,7 +106,7 @@ void init_make_tables(DB *db) {
"prime INT NOT NULL);";
char *err_msg = NULL;
- int rc = sqlite3_exec(db->get, sql, 0, 0, &err_msg);
+ int rc = sqlite3_exec(db->ptr, sql, 0, 0, &err_msg);
if (rc != SQLITE_OK ) {
printf("SQL error: %s\n", err_msg);
sqlite3_free(err_msg);
diff --git a/src/dblayer.h b/src/dblayer.h
index 4fe4d99..e5c0b73 100644
--- a/src/dblayer.h
+++ b/src/dblayer.h
@@ -21,12 +21,13 @@ enum SqlErrors {
typedef struct DBstruct DB;
struct DBstruct {
- sqlite3 *get;
+ sqlite3 *ptr;
SqlError error;
};
extern int db_exists();
extern DB open_DB();
+extern void close_DB(DB *DB);
/* init */
extern void init_make_tables(DB *db);
diff --git a/src/tests/init b/src/tests/init
index 2091bdc..4f78802 100755
--- a/src/tests/init
+++ b/src/tests/init
@@ -5,7 +5,7 @@ TEST_LOCATION=@PROJECT_TESTING_GROUNDS@
mkdir -p $TEST_LOCATION/vc
mkdir $TEST_LOCATION/sec
-exec $BIN/ck init $TEST_LOCATION/vc $TEST_LOCATION/sec > /dev/null 2>&1 &
+exec $BIN/ck init $TEST_LOCATION/vc $TEST_LOCATION/sec &
wait $!
if [ ! -f ~/.ck/ckrc ]; then