From 2e1a9e0832b53c0f03a71ba76fdd76612517d03c Mon Sep 17 00:00:00 2001
From: Anastasis Grammenos <anastasis.gramm2@gmail.com>
Date: Fri, 26 Oct 2018 21:20:36 +0300
Subject: actiopnarser fixes

---
 CMakeLists.txt     |  2 +-
 src/actionparser.c | 67 +++++++++++++++++++++++++-----------------------------
 src/actionparser.h |  2 +-
 src/ck.c           |  2 +-
 4 files changed, 34 insertions(+), 39 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 217b5bf..4327f84 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -118,6 +118,6 @@ if (CK_TESTS)
   configure_file(${TEST_DIR}/03_delete ${BIN_TEST_DIR}/03_delete @ONLY)
   configure_file(${TEST_DIR}/03_delete ${BIN_TEST_DIR}/03_delete @ONLY)
   configure_file(${TEST_DIR}/04_search ${BIN_TEST_DIR}/04_search @ONLY)
-  # check_ck
+  # test-ck
   configure_file(${RES_DIR}/test-ck test-ck @ONLY)
 endif()
diff --git a/src/actionparser.c b/src/actionparser.c
index 9954fbc..529876e 100644
--- a/src/actionparser.c
+++ b/src/actionparser.c
@@ -42,30 +42,26 @@ static const char *token;
 /* the position to be read */
 static int pos = 0;
 
-/* Reads the next token and returns it's position
- * Returns -1 otherwise */
+/* Advance one token.
+ * Returns 1 if it exists
+ * 0 otherwise */
 int next_token() {
   if (pos < optNum) {
     token = opts[pos];
-    return pos++;
+    pos++;
+    return 1;
   }
   else {
     token = NULL;
-    return -1;
+    return 0;
   }
 }
 
-/* copy the option from the list
- * to the UserOpt struct */
-void get_opt(UserOpt *opt) {
-  /* get arg */
-  next_token();
-  list_add(opt->args, token);
-}
-
 void fill_args_list(int arg_num, UserOpt *opt) {
   for (int i = 0; i < arg_num; i++) {
-    get_opt(opt);
+    if (next_token()) {
+      list_add(opt->args, token);
+    }
   }
 }
 
@@ -88,7 +84,7 @@ int parse_INIT(UserOpt *opt) {
 
 int parse_ADD(UserOpt *opt) {
   /* ADD expects 2 to 4 arguments */
-  if (optNum <= pos + 1
+  if (optNum < pos + 2
       || optNum > pos + 4) {
     opt->err = PERR_ADD_WRONG;
     return -1;
@@ -101,7 +97,8 @@ int parse_ADD(UserOpt *opt) {
 
 int parse_DEL(UserOpt *opt) {
   /* DEL expects 1 to 2 arguments */
-  if (optNum <= pos || optNum > pos + 2) {
+  if (optNum < pos + 1
+      || optNum > pos + 2) {
     opt->err = PERR_DEL_WRONG;
     return -1;
   }
@@ -113,7 +110,8 @@ int parse_DEL(UserOpt *opt) {
 
 int parse_EDIT(UserOpt *opt) {
   /* EDIT expects 1 to 2 arguments */
-  if (optNum <= pos || optNum > pos + 2) {
+  if (optNum < pos + 1
+      || optNum > pos + 2) {
     opt->err = PERR_EDIT_WRONG;
     return -1;
   }
@@ -124,8 +122,9 @@ int parse_EDIT(UserOpt *opt) {
 }
 
 int parse_LIST(UserOpt *opt) {
-  /* List expects a maximum of than 3 arguments */
-  if (optNum <= pos || optNum > pos + 5) {
+  /* List expects 1 to 5 arguments */
+  if (optNum < pos + 1
+      || optNum > pos + 5) {
     opt->err = PERR_LIST_WRONG;
     return -1;
   }
@@ -137,7 +136,8 @@ int parse_LIST(UserOpt *opt) {
 
 int parse_SEARCH(UserOpt *opt) {
   /* Search expects a maximum of 1 argument */
-  if (optNum <= pos || optNum > pos + 1) {
+  if (optNum < pos + 1
+      || optNum > pos + 1) {
     opt->err = PERR_SEARCH_WRONG;
     return -1;
   }
@@ -149,7 +149,8 @@ int parse_SEARCH(UserOpt *opt) {
 
 int parse_HELP(UserOpt *opt) {
   /* Help expects a maximum of 1 argument */
-  if (optNum <= pos || optNum > pos + 1) {
+  if (optNum < pos + 1
+      || optNum > pos + 1) {
     opt->err = PERR_HELP_WRONG;
     return -1;
   }
@@ -190,7 +191,7 @@ CkAction parser_get_action(const char *name, char *actionName) {
 
 void determine_action(UserOpt *opt) {
   /* get action */
-  if (next_token() == -1) {
+  if (!next_token()) {
     opt->action = CK_WRONG_ACTION;
     return;
   }
@@ -226,10 +227,10 @@ void free_user_opt(UserOpt *opt) {
  * than the default get it now */
 int get_config(UserOpt *opt) {
   /* get first token */
-  if (next_token() != -1) {
+  if (next_token()) {
     for (int i = 1; i < atoi(strConfDir[0]) + 1; i++) {
       if (strcmp(token, strConfDir[i]) == 0) {
-        if (next_token() == -1) {
+        if (!next_token()) {
           ERR("Config needs a value");
           return -1;
         }
@@ -262,7 +263,7 @@ int get_config(UserOpt *opt) {
 
 int version() {
   /* get first token */
-  if (next_token() != -1) {
+  if (next_token()) {
     for (int i = 1; i < atoi(strVersion[0]) + 1; i++) {
       if (strcmp(token, strVersion[i]) == 0) {
         print_version();
@@ -278,7 +279,7 @@ int version() {
 
 void verbose() {
   /* get first token */
-  if (next_token() != -1) {
+  if (next_token()) {
     for (int i = 1; i < atoi(strVerbose1[0]) + 1; i++) {
       if (strcmp(token, strVerbose1[i]) == 0) {
         errlog_set_verbose(1);
@@ -330,7 +331,7 @@ void print_parser_error(UserOpt *opt) {
   switch (opt->err) {
   case PERR_NOERR:
     return;
-  case PERR_UNKONW_ACTION:
+  case PERR_UNKNOWN_ACTION:
     ERR("Unknown action: %s", token);
     return;
   case PERR_INIT_WRONG:
@@ -385,43 +386,37 @@ int parse_action(int argc, const char **argv, UserOpt *opt) {
   optNum = argc;
   /* skip the program name */
   next_token();
-
+  /* set verbose level */
+  verbose();
   /* handle version info */
   if (version()) {
     return -1;
   }
-  /* set verbose level */
-  verbose();
   /* figure what is the config file */
   if (get_config(opt)) {
     return -1;
   }
-
   /* If the remaining arguments are < 1
    * print help and exit */
   if (optNum - pos < 1) {
     print_parser_help();
     return -1;
   }
-
   /* find the action */
   determine_action(opt);
   if (opt->action == CK_WRONG_ACTION) {
-    opt->err = PERR_UNKONW_ACTION;
+    opt->err = PERR_UNKNOWN_ACTION;
     print_parser_error(opt);
     return -1;
   }
-
-  // parse values
+  /* parse values */
   if (parse_vals(opt)) {
     print_parser_error(opt);
     return -1;
   }
-
   if (opt->err == PERR_NOERR) {
     return 0;
   }
   print_parser_error(opt);
   return -1;
 }
-
diff --git a/src/actionparser.h b/src/actionparser.h
index 3735ea8..d3c97c4 100644
--- a/src/actionparser.h
+++ b/src/actionparser.h
@@ -35,7 +35,7 @@ enum ParseErrors {
   PERR_##ACTION##_WRONG,
   CK_ACTIONS
 #undef X
-  PERR_UNKONW_ACTION,
+  PERR_UNKNOWN_ACTION,
 };
 typedef enum ParseErrors ParseError;
 
diff --git a/src/ck.c b/src/ck.c
index 2de5e70..e0f8a94 100644
--- a/src/ck.c
+++ b/src/ck.c
@@ -39,7 +39,7 @@ ERRLOG(main);
 int main(int argc, const char **argv) {
   initialize_errlog(argc, argv);
   UserOpt opt;
-  Conf conf = {.vc_dir = NULL, .scrt_dir = NULL};
+  Conf conf;
   /* get user opt */
   if (parse_action(argc, argv, &opt)) {
     goto error;
-- 
cgit v1.2.3