fix config str
[protos/libecoli.git] / lib / ecoli_node_cmd.c
index 277d696..99a7732 100644 (file)
@@ -1,28 +1,5 @@
-/*
- * Copyright (c) 2016-2017, Olivier MATZ <zer0@droids-corp.org>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above copyright
- *       notice, this list of conditions and the following disclaimer in the
- *       documentation and/or other materials provided with the distribution.
- *     * Neither the name of the University of California, Berkeley nor the
- *       names of its contributors may be used to endorse or promote products
- *       derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
  */
 
 #include <sys/queue.h>
@@ -39,8 +16,8 @@
 #include <ecoli_test.h>
 #include <ecoli_strvec.h>
 #include <ecoli_node.h>
-#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_parse.h>
+#include <ecoli_complete.h>
 #include <ecoli_node_expr.h>
 #include <ecoli_node_str.h>
 #include <ecoli_node_or.h>
@@ -67,7 +44,7 @@ struct ec_node_cmd {
 
 static int
 ec_node_cmd_eval_var(void **result, void *userctx,
-       const struct ec_parsed *var)
+       const struct ec_parse *var)
 {
        const struct ec_strvec *vec;
        struct ec_node_cmd *node = userctx;
@@ -78,9 +55,11 @@ ec_node_cmd_eval_var(void **result, void *userctx,
        (void)userctx;
 
        /* get parsed string vector, it should contain only one str */
-       vec = ec_parsed_strvec(var);
-       if (ec_strvec_len(vec) != 1)
-               return -EINVAL;
+       vec = ec_parse_strvec(var);
+       if (ec_strvec_len(vec) != 1) {
+               errno = EINVAL;
+               return -1;
+       }
        str = ec_strvec_val(vec, 0);
 
        for (i = 0; i < node->len; i++) {
@@ -92,7 +71,7 @@ ec_node_cmd_eval_var(void **result, void *userctx,
                /* if id matches, use a node provided by the user... */
                eval = ec_node_clone(node->table[i]);
                if (eval == NULL)
-                       return -ENOMEM;
+                       return -1;
                break;
        }
 
@@ -100,7 +79,7 @@ ec_node_cmd_eval_var(void **result, void *userctx,
        if (eval == NULL) {
                eval = ec_node_str(EC_NO_ID, str);
                if (eval == NULL)
-                       return -ENOMEM;
+                       return -1;
        }
 
        *result = eval;
@@ -110,19 +89,20 @@ ec_node_cmd_eval_var(void **result, void *userctx,
 
 static int
 ec_node_cmd_eval_pre_op(void **result, void *userctx, void *operand,
-       const struct ec_parsed *operator)
+       const struct ec_parse *operator)
 {
        (void)result;
        (void)userctx;
        (void)operand;
        (void)operator;
 
-       return -EINVAL;
+       errno = EINVAL;
+       return -1;
 }
 
 static int
 ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand,
-       const struct ec_parsed *operator)
+       const struct ec_parse *operator)
 {
        const struct ec_strvec *vec;
        struct ec_node *in = operand;;
@@ -131,19 +111,22 @@ ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand,
        (void)userctx;
 
        /* get parsed string vector, it should contain only one str */
-       vec = ec_parsed_strvec(operator);
-       if (ec_strvec_len(vec) != 1)
-               return -EINVAL;
+       vec = ec_parse_strvec(operator);
+       if (ec_strvec_len(vec) != 1) {
+               errno = EINVAL;
+               return -1;
+       }
 
        if (!strcmp(ec_strvec_val(vec, 0), "*")) {
                out = ec_node_many(EC_NO_ID,
                                ec_node_clone(in), 0, 0);
                if (out == NULL)
-                       return -EINVAL;
+                       return -1;
                ec_node_free(in);
                *result = out;
        } else {
-               return -EINVAL;
+               errno = EINVAL;
+               return -1;
        }
 
        return 0;
@@ -151,7 +134,7 @@ ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand,
 
 static int
 ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1,
-       const struct ec_parsed *operator, void *operand2)
+       const struct ec_parse *operator, void *operand2)
 
 {
        const struct ec_strvec *vec;
@@ -162,21 +145,23 @@ ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1,
        (void)userctx;
 
        /* get parsed string vector, it should contain only one str */
-       vec = ec_parsed_strvec(operator);
-       if (ec_strvec_len(vec) > 1)
-               return -EINVAL;
+       vec = ec_parse_strvec(operator);
+       if (ec_strvec_len(vec) > 1) {
+               errno = EINVAL;
+               return -1;
+       }
 
        if (ec_strvec_len(vec) == 0) {
                if (!strcmp(in1->type->name, "seq")) {
                        if (ec_node_seq_add(in1, ec_node_clone(in2)) < 0)
-                               return -EINVAL;
+                               return -1;
                        ec_node_free(in2);
                        *result = in1;
                } else {
                        out = EC_NODE_SEQ(EC_NO_ID, ec_node_clone(in1),
                                        ec_node_clone(in2));
                        if (out == NULL)
-                               return -EINVAL;
+                               return -1;
                        ec_node_free(in1);
                        ec_node_free(in2);
                        *result = out;
@@ -184,19 +169,19 @@ ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1,
        } else if (!strcmp(ec_strvec_val(vec, 0), "|")) {
                if (!strcmp(in2->type->name, "or")) {
                        if (ec_node_or_add(in2, ec_node_clone(in1)) < 0)
-                               return -EINVAL;
+                               return -1;
                        ec_node_free(in1);
                        *result = in2;
                } else if (!strcmp(in1->type->name, "or")) {
                        if (ec_node_or_add(in1, ec_node_clone(in2)) < 0)
-                               return -EINVAL;
+                               return -1;
                        ec_node_free(in2);
                        *result = in1;
                } else {
                        out = EC_NODE_OR(EC_NO_ID, ec_node_clone(in1),
                                        ec_node_clone(in2));
                        if (out == NULL)
-                               return -EINVAL;
+                               return -1;
                        ec_node_free(in1);
                        ec_node_free(in2);
                        *result = out;
@@ -204,25 +189,26 @@ ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1,
        } else if (!strcmp(ec_strvec_val(vec, 0), ",")) {
                if (!strcmp(in2->type->name, "subset")) {
                        if (ec_node_subset_add(in2, ec_node_clone(in1)) < 0)
-                               return -EINVAL;
+                               return -1;
                        ec_node_free(in1);
                        *result = in2;
                } else if (!strcmp(in1->type->name, "subset")) {
                        if (ec_node_subset_add(in1, ec_node_clone(in2)) < 0)
-                               return -EINVAL;
+                               return -1;
                        ec_node_free(in2);
                        *result = in1;
                } else {
                        out = EC_NODE_SUBSET(EC_NO_ID, ec_node_clone(in1),
                                        ec_node_clone(in2));
                        if (out == NULL)
-                               return -EINVAL;
+                               return -1;
                        ec_node_free(in1);
                        ec_node_free(in2);
                        *result = out;
                }
        } else {
-               return -EINVAL;
+               errno = EINVAL;
+               return -1;
        }
 
        return 0;
@@ -230,8 +216,8 @@ ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1,
 
 static int
 ec_node_cmd_eval_parenthesis(void **result, void *userctx,
-       const struct ec_parsed *open_paren,
-       const struct ec_parsed *close_paren,
+       const struct ec_parse *open_paren,
+       const struct ec_parse *close_paren,
        void *value)
 {
        const struct ec_strvec *vec;
@@ -242,19 +228,22 @@ ec_node_cmd_eval_parenthesis(void **result, void *userctx,
        (void)close_paren;
 
        /* get parsed string vector, it should contain only one str */
-       vec = ec_parsed_strvec(open_paren);
-       if (ec_strvec_len(vec) != 1)
-               return -EINVAL;
+       vec = ec_parse_strvec(open_paren);
+       if (ec_strvec_len(vec) != 1) {
+               errno = EINVAL;
+               return -1;
+       }
 
        if (!strcmp(ec_strvec_val(vec, 0), "[")) {
                out = ec_node_option(EC_NO_ID, ec_node_clone(in));
                if (out == NULL)
-                       return -EINVAL;
+                       return -1;
                ec_node_free(in);
        } else if (!strcmp(ec_strvec_val(vec, 0), "(")) {
                out = in;
        } else {
-               return -EINVAL;
+               errno = EINVAL;
+               return -1;
        }
 
        *result = out;
@@ -281,7 +270,7 @@ static const struct ec_node_expr_eval_ops test_ops = {
 static int ec_node_cmd_build(struct ec_node_cmd *node)
 {
        struct ec_node *expr = NULL, *lex = NULL, *cmd = NULL;
-       struct ec_parsed *p = NULL;
+       struct ec_parse *p = NULL;
        void *result;
        int ret;
 
@@ -293,11 +282,11 @@ static int ec_node_cmd_build(struct ec_node_cmd *node)
        node->cmd = NULL;
 
        /* build the expression parser */
-       ret = -ENOMEM;
        expr = ec_node("expr", "expr");
        if (expr == NULL)
                goto fail;
-       ret = ec_node_expr_set_val_node(expr, ec_node_re(EC_NO_ID, "[a-zA-Z0-9]+"));
+       ret = ec_node_expr_set_val_node(expr, ec_node_re(EC_NO_ID,
+                                       "[a-zA-Z0-9]+"));
        if (ret < 0)
                goto fail;
        ret = ec_node_expr_add_bin_op(expr, ec_node_str(EC_NO_ID, ","));
@@ -325,7 +314,6 @@ static int ec_node_cmd_build(struct ec_node_cmd *node)
                goto fail;
 
        /* prepend a lexer to the expression node */
-       ret = -ENOMEM;
        lex = ec_node_re_lex(EC_NO_ID, ec_node_clone(expr));
        if (lex == NULL)
                goto fail;
@@ -347,23 +335,25 @@ static int ec_node_cmd_build(struct ec_node_cmd *node)
                goto fail;
 
        /* parse the command expression */
-       ret = -ENOMEM;
        p = ec_node_parse(lex, node->cmd_str);
        if (p == NULL)
                goto fail;
 
-       ret = -EINVAL;
-       if (!ec_parsed_matches(p))
+       if (!ec_parse_matches(p)) {
+               errno = EINVAL;
                goto fail;
-       if (!ec_parsed_has_child(p))
+       }
+       if (!ec_parse_has_child(p)) {
+               errno = EINVAL;
                goto fail;
+       }
 
-       ret = ec_node_expr_eval(&result, expr, ec_parsed_get_first_child(p),
+       ret = ec_node_expr_eval(&result, expr, ec_parse_get_first_child(p),
                                &test_ops, node);
        if (ret < 0)
                goto fail;
 
-       ec_parsed_free(p);
+       ec_parse_free(p);
        p = NULL;
 
        node->expr = expr;
@@ -373,34 +363,31 @@ static int ec_node_cmd_build(struct ec_node_cmd *node)
        return 0;
 
 fail:
-       ec_parsed_free(p);
+       ec_parse_free(p);
        ec_node_free(expr);
        ec_node_free(lex);
        ec_node_free(cmd);
-       return ret;
+
+       return -1;
 }
 
 static int
-ec_node_cmd_parse(const struct ec_node *gen_node, struct ec_parsed *state,
+ec_node_cmd_parse(const struct ec_node *gen_node, struct ec_parse *state,
                const struct ec_strvec *strvec)
 {
        struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
 
-       if (node->cmd == NULL)
-               return -ENOENT;
        return ec_node_parse_child(node->cmd, state, strvec);
 }
 
 static int
 ec_node_cmd_complete(const struct ec_node *gen_node,
-               struct ec_completed *completed,
+               struct ec_comp *comp,
                const struct ec_strvec *strvec)
 {
        struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
 
-       if (node->cmd == NULL)
-               return -ENOENT;
-       return ec_node_complete_child(node->cmd, completed, strvec);
+       return ec_node_complete_child(node->cmd, comp, strvec);
 }
 
 static void ec_node_cmd_free_priv(struct ec_node *gen_node)