aboutsummaryrefslogtreecommitdiffstats
path: root/src/actionparser.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/actionparser.c')
-rw-r--r--src/actionparser.c67
1 files changed, 31 insertions, 36 deletions
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;
}
-