aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/actionparser.c219
-rw-r--r--src/actionparser.h20
-rw-r--r--src/ck.c19
-rw-r--r--src/dbhelper.c2
-rw-r--r--src/dbhelper.h4
-rw-r--r--src/dblayer.c2
6 files changed, 138 insertions, 128 deletions
diff --git a/src/actionparser.c b/src/actionparser.c
index ac01b84..207a089 100644
--- a/src/actionparser.c
+++ b/src/actionparser.c
@@ -18,44 +18,48 @@
/* accepted commands */
/* [0] is the count */
-const char* const strINIT[] = {"2", "init", "-i"};
-const char* const strADD[] = {"2", "add", "-a"};
-const char* const strDEL[] = {"2", "del", "-d"};
-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 strINIT[] = {"2", "init", "-i"};
+const char* const strADD[] = {"2", "add", "-a"};
+const char* const strDEL[] = {"2", "del", "-d"};
+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;
+
/* holds the list of the opts
- as given by the user */
+ * as given by the user */
static char **opts;
+
/* points to the current token */
static char *token;
-/* points to the position
- * to be read */
+
+/* the position to be read */
static int pos = 0;
/* Reads the next token and returns it's position
* Returns -1 otherwise */
-int nextToken() {
+int next_token() {
if (pos < optNum) {
token = opts[pos];
return pos++;
}
else {
- token = "";
+ token = NULL;
return -1;
}
}
-void getOpt(int position, UserOpt *opt) {
- // get arg
- nextToken();
+/* copy the option from the list
+ * to the UserOpt struct */
+void get_opt(int position, UserOpt *opt) {
+ /* get arg */
+ next_token();
- // allocate memory
+ /* allocate memory */
opt->argv[position] = (char *)malloc((strlen(token))*sizeof(char) + 1);
strcpy(opt->argv[position], token);
}
@@ -65,8 +69,8 @@ void getOpt(int position, UserOpt *opt) {
* like so "ck ACTION ..."
* ^ */
int parse_INIT(UserOpt *opt) {
- // INIT expects 2 arguments
- // starting from 0
+ /* INIT expects 2 arguments
+ * starting from 0 */
opt->argc = 2;
if (optNum != pos /* already consumed */ + opt->argc) {
opt->err = PERR_INIT_WRONG;
@@ -74,13 +78,13 @@ int parse_INIT(UserOpt *opt) {
}
for (int i = 0; i < opt->argc; i++) {
- getOpt(i, opt);
+ get_opt(i, opt);
}
return 1;
}
int parse_ADD(UserOpt *opt) {
- // ADD expects 2 to 4 arguments
+ /* ADD expects 2 to 4 arguments */
if (optNum <= pos + 1
|| optNum > pos + 4) {
opt->err = PERR_ADD_WRONG;
@@ -89,7 +93,7 @@ int parse_ADD(UserOpt *opt) {
opt->argc = optNum - pos;
for (int i = 0; i < opt->argc; i++) {
- getOpt(i, opt);
+ get_opt(i, opt);
}
return 1;
}
@@ -111,7 +115,7 @@ int parse_HELP(UserOpt *opt) {
}
-int parseVals(UserOpt *opt) {
+int parse_vals(UserOpt *opt) {
switch (opt->action) {
#define X(ACTION) \
case CKA_##ACTION: \
@@ -123,17 +127,26 @@ int parseVals(UserOpt *opt) {
}
}
-CkAction determineAction() {
- int i = 0;
+void determine_action(UserOpt *opt) {
+ /* get action */
+ if (next_token() == -1) {
+ opt->action = CK_WRONG_ACTION;
+ return;
+ }
+
+ int i;
#define X(ACTION) \
for (i = 1; i < atoi(str##ACTION[0]) + 1; i++) { \
if (strcmp(token, str##ACTION[i]) == 0) { \
- return CKA_##ACTION; \
+ opt->action = CKA_##ACTION; \
+ return; \
} \
}
- CK_ACTIONS
+ CK_ACTIONS;
#undef X
- return CK_WRONG_ACTION;
+
+ opt->action = CK_WRONG_ACTION;
+ return;
}
UserOpt make_empty_user_opt() {
@@ -148,24 +161,29 @@ UserOpt make_empty_user_opt() {
return opt;
}
+
+/* called to free the resources
+ * UserOpt holds */
void free_user_opt(UserOpt *opt) {
for (int i = 0; i < 10; i++) {
if (opt->argv[i] != NULL) {
free(opt->argv[i]);
- }
+ }
}
if (opt->confDir != NULL) {
free(opt->confDir);
}
}
-void getConfig(UserOpt *opt) {
- // get first token
- nextToken();
- int ok = 1;
+/* If the used has specified a config file other
+ * than the default get it now */
+void get_config(UserOpt *opt) {
+ /* get first token */
+ next_token();
+
for (int i = 1; i < atoi(strConfDir[0]) + 1; i++) {
if (strcmp(token, strConfDir[i]) == 0) {
- if (nextToken() == -1) {
+ if (next_token() == -1) {
printf("Config needs a value\n");
exit(1);
}
@@ -196,74 +214,33 @@ void getConfig(UserOpt *opt) {
token = opts[pos];
}
-ParseResult parseAction(int argc, char* argv[], UserOpt *opt) {
- *opt = make_empty_user_opt();
- if (argc < 2) {
- return OPR_HELP;
- }
- opts = argv;
- optNum = argc;
-
- // skip the program nake
- nextToken();
-
- getConfig(opt);
- // get action
- nextToken();
- opt->action = determineAction();
- if (opt->action == CK_WRONG_ACTION) {
- opt->err = PERR_UNKONW_ACTION;
- return OPR_ERR;
- }
- if (opt->action == CKA_HELP) {
- return OPR_HELP;
- }
-
- // parse values
- if (!parseVals(opt)) {
- return OPR_ERR;
- }
-
- if (opt->err == PERR_NOERR) {
- return OPR_OK;
- }
- else {
- return OPR_ERR;
- }
-}
-
-void getPossibleActionNames(char * dest, CkAction ckAction) {
- if (ckAction == -1) {
- dest = NULL;
- return;
- }
-
+void get_possible_action_strings(char *dest, CkAction ckAction) {
char buf[STR_S];
-
switch (ckAction) {
#define X(ACTION) \
case CKA_##ACTION: \
- strcpy(buf, "{ "); \
- for (int i = 1; i < atoi(str##ACTION[0]); i++) { \
- strcat(buf, str##ACTION[i]); \
- strcat(buf, ", "); \
- } \
- strcat(buf, str##ACTION[atoi(str##ACTION[0])]); \
- strcat(buf, " }"); \
- break;
+ strcpy(buf, "{ "); \
+ for (int i = 1; i < atoi(str##ACTION[0]); i++) { \
+ strcat(buf, str##ACTION[i]); \
+ strcat(buf, ", "); \
+ } \
+ strcat(buf, str##ACTION[atoi(str##ACTION[0])]); \
+ strcat(buf, " }"); \
+ break;
CK_ACTIONS
#undef X
default:
- break;
+ dest = NULL;
+ return;
}
strcpy(dest, buf);
}
-void printParserError(UserOpt *opt) {
+void print_parser_error(UserOpt *opt) {
char errStr[STR_M];
char names[STR_S];
- getPossibleActionNames(names, opt->action);
+ get_possible_action_strings(names, opt->action);
switch (opt->err) {
case PERR_NOERR:
@@ -296,22 +273,60 @@ void printParserError(UserOpt *opt) {
printf("Parsing error\n%s\n", errStr);
}
-void printParserHelp() {
+void print_parser_help() {
char names[STR_S];
printf("ck - the config keeper\n");
printf("Usage:\n");
- getPossibleActionNames(names, CKA_INIT);
- printf("Initialize: \t%s\n", names);
- getPossibleActionNames(names, CKA_ADD);
- printf("Add config: \t%s\n", names);
- getPossibleActionNames(names, CKA_DEL);
- printf("Delete config: \t%s\n", names);
- getPossibleActionNames(names, CKA_EDIT);
- printf("Edit config: \t%s\n", names);
- getPossibleActionNames(names, CKA_LIST);
- printf("List configs: \t%s\n", names);
- getPossibleActionNames(names, CKA_SEARCH);
- printf("Search: \t%s\n", names);
- getPossibleActionNames(names, CKA_HELP);
- printf("Print this: \t%s\n", names);
+ get_possible_action_strings(names, CKA_INIT);
+ printf("Initialize: \t%s\n", names);
+ get_possible_action_strings(names, CKA_ADD);
+ printf("Add config: \t%s\n", names);
+ get_possible_action_strings(names, CKA_DEL);
+ printf("Delete config: \t%s\n", names);
+ get_possible_action_strings(names, CKA_EDIT);
+ printf("Edit config: \t%s\n", names);
+ get_possible_action_strings(names, CKA_LIST);
+ printf("List configs: \t%s\n", names);
+ get_possible_action_strings(names, CKA_SEARCH);
+ printf("Search: \t%s\n", names);
+ get_possible_action_strings(names, CKA_HELP);
+ printf("Print this: \t%s\n", names);
+}
+
+ActionParseResult parse_action(int argc, char* argv[], UserOpt *opt) {
+ /* make empty user opt */
+ *opt = make_empty_user_opt();
+ if (argc < 2) {
+ return APR_HELP;
+ }
+ opts = argv;
+ optNum = argc;
+
+ /* skip the program name */
+ next_token();
+
+ /* figure what is the config file */
+ get_config(opt);
+
+ /* find the action */
+ determine_action(opt);
+ if (opt->action == CK_WRONG_ACTION) {
+ opt->err = PERR_UNKONW_ACTION;
+ return APR_ERR;
+ }
+ if (opt->action == CKA_HELP) {
+ return APR_HELP;
+ }
+
+ // parse values
+ if (!parse_vals(opt)) {
+ return APR_ERR;
+ }
+
+ if (opt->err == PERR_NOERR) {
+ return APR_OK;
+ }
+ else {
+ return APR_ERR;
+ }
}
diff --git a/src/actionparser.h b/src/actionparser.h
index 3defde8..6473cb9 100644
--- a/src/actionparser.h
+++ b/src/actionparser.h
@@ -15,7 +15,7 @@
* procedures.
*
* Keeps track of what error occured where and provides
- * printParserHelp() and printParserError() functions
+ * print_parser_help() and print_parser_error() functions
* to notify the user
*
* -------------------------------------------------------------------------- */
@@ -34,7 +34,7 @@
typedef enum ParseErrors ParseError;
enum ParseErrors {
PERR_NOERR = 0,
-#define X(ACTION) \
+#define X(ACTION) \
PERR_##ACTION##_WRONG,
CK_ACTIONS
#undef X
@@ -50,11 +50,11 @@ enum CkActions {
#undef X
};
-typedef enum OptParserResults ParseResult;
-enum OptParserResults {
- OPR_OK,
- OPR_ERR,
- OPR_HELP
+typedef enum ActionParserResults ActionParseResult;
+enum ActionParserResults {
+ APR_OK,
+ APR_ERR,
+ APR_HELP
};
typedef struct UserOptions UserOpt;
@@ -66,9 +66,9 @@ struct UserOptions {
char *argv[10]; // action's options
};
-extern ParseResult parseAction(int argc, char* argv[], UserOpt *opt);
-extern void printParserError();
-extern void printParserHelp();
+extern ActionParseResult parse_action(int argc, char* argv[], UserOpt *opt);
+extern void print_parser_error();
+extern void print_parser_help();
extern void free_user_opt(UserOpt *opt);
#endif // ACTIONPARSER_H
diff --git a/src/ck.c b/src/ck.c
index a9ef001..a63eb63 100644
--- a/src/ck.c
+++ b/src/ck.c
@@ -28,27 +28,22 @@
*
* -------------------------------------------------------------------------- */
-#include <stdio.h>
-#include <string.h>
-#include <sqlite3.h>
-
-#include "actionparser.h"
#include "actions.h"
-#include "confparser.h"
#include "dblayer.h"
+#include "ckutil.h"
int main(int argc, char *argv[]) {
UserOpt opt;
- switch(parseAction(argc, argv, &opt)) {
- case OPR_HELP:
+ switch(parse_action(argc, argv, &opt)) {
+ case APR_HELP:
free_user_opt(&opt);
- printParserHelp();
+ print_parser_help();
return 0;
- case OPR_ERR:
- printParserError(&opt);
+ case APR_ERR:
+ print_parser_error(&opt);
free_user_opt(&opt);
return 1;
- case OPR_OK:
+ case APR_OK:
break;
}
diff --git a/src/dbhelper.c b/src/dbhelper.c
index 66bae7d..b6421db 100644
--- a/src/dbhelper.c
+++ b/src/dbhelper.c
@@ -114,7 +114,7 @@ void dhb_form_query_find_relationship(char *query) {
strcpy(query, tmp);
}
-void dbh_format_query_select_from_joined_eq(char *query, const char *selection, const char* condition) {
+void dbh_form_query_select_from_joined_eq(char *query, const char *selection, const char* condition) {
char tmp[STR_L] = "SELECT ";
strcat(tmp, selection);
strcat(tmp, " FROM ");
diff --git a/src/dbhelper.h b/src/dbhelper.h
index df14ebf..197171f 100644
--- a/src/dbhelper.h
+++ b/src/dbhelper.h
@@ -56,6 +56,6 @@ 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_format_query_select_from_joined_eq(char *query, const char *selection, const char* condition);
-extern void dbh_format_query_select_from_joined_like(char *query, const char *selection, const char* condition);
+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 b2be023..bde712b 100644
--- a/src/dblayer.c
+++ b/src/dblayer.c
@@ -289,7 +289,7 @@ int program_has_primary_config(DB *db, const int pid) {
strcat(condition, ".");
strcat(condition, COL_PROGRAM_ID);
- dbh_format_query_select_from_joined_eq(sql, COL_CONFIG_PRIMARY, condition);
+ dbh_form_query_select_from_joined_eq(sql, COL_CONFIG_PRIMARY, condition);
rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
if (rc != SQLITE_OK) {