aboutsummaryrefslogtreecommitdiffstats
path: root/src/actionparser.c
diff options
context:
space:
mode:
authorAnastasis Grammenos <anastasis.gramm2@gmail.com>2018-10-05 18:16:16 +0300
committerAnastasis Grammenos <anastasis.gramm2@gmail.com>2018-10-05 19:36:11 +0300
commita0db476bed7d7b814c426d23b55a74d585a6d8cb (patch)
treeffde1fae7d75701e7dba31cf53077a9ca21b3a53 /src/actionparser.c
parent764a205fe9676afcdb94998b5e553d650bda86ba (diff)
downloadck-a0db476bed7d7b814c426d23b55a74d585a6d8cb.tar.gz
ck-a0db476bed7d7b814c426d23b55a74d585a6d8cb.tar.bz2
ck-a0db476bed7d7b814c426d23b55a74d585a6d8cb.zip
errlog in actionparser and configparser
Diffstat (limited to 'src/actionparser.c')
-rw-r--r--src/actionparser.c170
1 files changed, 87 insertions, 83 deletions
diff --git a/src/actionparser.c b/src/actionparser.c
index d5ad071..266f771 100644
--- a/src/actionparser.c
+++ b/src/actionparser.c
@@ -10,7 +10,7 @@
* -----------------------------------------------------------------------------
*
* The following code is resposinble for parsing the command line arguments
- * and report any errors that might come up.
+ * and finding the correct ck action.
*
* -------------------------------------------------------------------------- */
#include "actionparser.h"
@@ -18,19 +18,19 @@
#include "ckinfo.h"
#include "ckerrlog.h"
-ERRLOG(action parser);
+ERRLOG(parser);
/* accepted commands */
/* [0] is the count */
-const char* const strINIT[] = {"3", "init", "i", "-i"};
-const char* const strADD[] = {"3", "add", "a", "-a"};
-const char* const strDEL[] = {"4", "delete", "del","d", "-d"};
-const char* const strEDIT[] = {"3", "edit", "e", "-e"};
-const char* const strLIST[] = {"5", "list", "ls", "l", "-l", "-ls"};
-const char* const strSEARCH[] = {"3", "search", "s", "-s"};
-const char* const strHELP[] = {"5", "help", "h", "-?", "-h", "--help"};
-const char* const strConfDir[] = {"4", "config", "conf", "c", "-c"};
-const char* const strVersion[] = {"2", "version", "--version"};
+const char* const strINIT[] = {"3", "init", "i", "-i"};
+const char* const strADD[] = {"3", "add", "a", "-a"};
+const char* const strDEL[] = {"4", "delete", "del","d", "-d"};
+const char* const strEDIT[] = {"3", "edit", "e", "-e"};
+const char* const strLIST[] = {"5", "list", "ls", "l", "-l", "-ls"};
+const char* const strSEARCH[] = {"3", "search", "s", "-s"};
+const char* const strHELP[] = {"5", "help", "h", "-?", "-h", "--help"};
+const char* const strConfDir[] = {"4", "config", "conf", "c", "-c"};
+const char* const strVersion[] = {"2", "version", "--version"};
const char* const strVerbose1[] = {"2", "--verbose", "-v"};
const char* const strVerbose2[] = {"2", "--Verbose", "-V"};
@@ -88,7 +88,7 @@ int parse_INIT(UserOpt *opt) {
}
fill_args_list(arg_num, opt);
- return 1;
+ return 0;
}
int parse_ADD(UserOpt *opt) {
@@ -101,7 +101,7 @@ int parse_ADD(UserOpt *opt) {
int arg_num = optNum - pos;
fill_args_list(arg_num, opt);
- return 1;
+ return 0;
}
int parse_DEL(UserOpt *opt) {
@@ -113,7 +113,7 @@ int parse_DEL(UserOpt *opt) {
int arg_num = optNum - pos;
fill_args_list(arg_num, opt);
- return 1;
+ return 0;
}
int parse_EDIT(UserOpt *opt) {
@@ -125,7 +125,7 @@ int parse_EDIT(UserOpt *opt) {
int arg_num = optNum - pos;
fill_args_list(arg_num, opt);
- return 1;
+ return 0;
}
int parse_LIST(UserOpt *opt) {
@@ -137,7 +137,7 @@ int parse_LIST(UserOpt *opt) {
int arg_num = optNum - pos;
fill_args_list(arg_num, opt);
- return 1;
+ return 0;
}
int parse_SEARCH(UserOpt *opt) {
@@ -149,7 +149,7 @@ int parse_SEARCH(UserOpt *opt) {
int arg_num = optNum - pos;
fill_args_list(arg_num, opt);
- return 1;
+ return 0;
}
int parse_HELP(UserOpt *opt) {
@@ -181,6 +181,7 @@ void determine_action(UserOpt *opt) {
for (i = 1; i < atoi(str##ACTION[0]) + 1; i++) { \
if (strcmp(token, str##ACTION[i]) == 0) { \
opt->action = CKA_##ACTION; \
+ LOG("Action to perform: %s", str##ACTION[1]); \
return; \
} \
}
@@ -212,31 +213,29 @@ void free_user_opt(UserOpt *opt) {
/* If the used has specified a config file other
* than the default get it now */
-void get_config(UserOpt *opt) {
+int 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 (next_token() == -1) {
- printf("Config needs a value\n");
- exit(1);
- }
- if (strcmp(token, ".") == 0){
- /* WHAT? */
- printf("Dot\n");
+ if (next_token() != -1) {
+ for (int i = 1; i < atoi(strConfDir[0]) + 1; i++) {
+ if (strcmp(token, strConfDir[i]) == 0) {
+ if (next_token() == -1) {
+ ERR("Config needs a value");
+ return -1;
+ }
+ char dir[STR_L];
+ realpath(token, dir);
+ if (!util_is_dir(dir)) {
+ ERR("%s is not a directory", token);
+ return -1;
+ }
+ opt->confDir = malloc(strlen(dir) + 1);
+ strcpy(opt->confDir, dir);
+ // remove trailing `/`
+ if (opt->confDir[strlen(dir) - 1] == '/') {
+ opt->confDir[strlen(dir) - 1] = '\0';
+ }
+ return 0;
}
- if (!util_is_dir(token)) {
- printf("%s is not a directory\n", token);
- exit(1);
- }
- opt->confDir = malloc(strlen(token) + 1);
- strcpy(opt->confDir, token);
- // remove trailing `/`
- if (opt->confDir[strlen(token) - 1] == '/') {
- opt->confDir[strlen(token) - 1] = '\0';
- }
- return;
}
}
char * defaultConf = ".ck";
@@ -247,46 +246,46 @@ void get_config(UserOpt *opt) {
// rewind
pos = pos - 1;
token = opts[pos];
+ return 0;
}
int version() {
/* get first token */
- next_token();
-
- for (int i = 1; i < atoi(strVersion[0]) + 1; i++) {
- if (strcmp(token, strVersion[i]) == 0) {
- print_version();
- return 1;
+ if (next_token() != -1) {
+ for (int i = 1; i < atoi(strVersion[0]) + 1; i++) {
+ if (strcmp(token, strVersion[i]) == 0) {
+ print_version();
+ return 1;
+ }
}
+ // rewind
+ pos = pos - 1;
+ token = opts[pos];
}
-
- // rewind
- pos = pos - 1;
- token = opts[pos];
return 0;
}
void verbose() {
/* get first token */
- next_token();
-
- for (int i = 1; i < atoi(strVerbose1[0]) + 1; i++) {
- if (strcmp(token, strVerbose1[i]) == 0) {
- errlog_set_verbose(1);;
- return;
+ if (next_token() != -1) {
+ for (int i = 1; i < atoi(strVerbose1[0]) + 1; i++) {
+ if (strcmp(token, strVerbose1[i]) == 0) {
+ errlog_set_verbose(1);
+ return;
+ }
}
- }
- for (int i = 1; i < atoi(strVerbose2[0]) + 1; i++) {
- if (strcmp(token, strVerbose2[i]) == 0) {
- errlog_set_verbose(2);;
- return;
+ for (int i = 1; i < atoi(strVerbose2[0]) + 1; i++) {
+ if (strcmp(token, strVerbose2[i]) == 0) {
+ errlog_set_verbose(2);
+ return;
+ }
}
- }
- // rewind
- pos = pos - 1;
- token = opts[pos];
+ // rewind
+ pos = pos - 1;
+ token = opts[pos];
+ }
}
void get_possible_action_strings(char *dest, CkAction ckAction) {
@@ -369,49 +368,54 @@ void print_parser_help() {
report_help();
}
-ActionParseResult parse_action(int argc, char* argv[], UserOpt *opt) {
+int 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();
/* handle version info */
if (version()) {
- return APR_VERSION;
+ return 1;
}
-
/* set verbose level */
verbose();
-
/* figure what is the config file */
- get_config(opt);
- cklog("Found ck configuration directory in %s", opt->confDir);
+ if (get_config(opt)) {
+ return 1;
+ }
+
+ /* If the remaining arguments are < 2
+ * print help and exit */
+ if (optNum - pos < 2) {
+ print_parser_help();
+ return -1;
+ }
/* find the action */
determine_action(opt);
if (opt->action == CK_WRONG_ACTION) {
opt->err = PERR_UNKONW_ACTION;
- return APR_ERR;
+ print_parser_error(opt);
+ return -1;
}
if (opt->action == CKA_HELP) {
- return APR_HELP;
+ print_parser_help();
+ return -1;
}
// parse values
- if (!parse_vals(opt)) {
- return APR_ERR;
+ if (parse_vals(opt)) {
+ print_parser_error(opt);
+ return -1;
}
if (opt->err == PERR_NOERR) {
- return APR_OK;
- }
- else {
- return APR_ERR;
+ return 0;
}
+ print_parser_error(opt);
+ return -1;
}
+