srcs += ecoli_node_str.c
srcs += ecoli_node_subset.c
srcs += ecoli_node_weakref.c
-srcs += ecoli_parsed.c
+srcs += ecoli_parse.c
srcs += ecoli_string.c
srcs += ecoli_vec.c
#include <ecoli_keyval.h>
#include <ecoli_log.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
struct ec_comp_item {
struct ec_keyval *attrs;
};
-struct ec_comp *ec_comp(struct ec_parsed *state)
+struct ec_comp *ec_comp(struct ec_parse *state)
{
struct ec_comp *comp = NULL;
return NULL;
}
-struct ec_parsed *ec_comp_get_state(struct ec_comp *comp)
+struct ec_parse *ec_comp_get_state(struct ec_comp *comp)
{
return comp->cur_state;
}
struct ec_comp *comp,
const struct ec_strvec *strvec)
{
- struct ec_parsed *child_state, *cur_state;
+ struct ec_parse *child_state, *cur_state;
struct ec_comp_group *cur_group;
int ret;
/* save previous parse state, prepare child state */
cur_state = comp->cur_state;
- child_state = ec_parsed(node);
+ child_state = ec_parse(node);
if (child_state == NULL)
return -ENOMEM;
if (cur_state != NULL)
- ec_parsed_link_child(cur_state, child_state);
+ ec_parse_link_child(cur_state, child_state);
comp->cur_state = child_state;
cur_group = comp->cur_group;
comp->cur_group = NULL;
/* restore parent parse state */
if (cur_state != NULL) {
- ec_parsed_unlink_child(cur_state, child_state);
- assert(!ec_parsed_has_child(child_state));
+ ec_parse_unlink_child(cur_state, child_state);
+ assert(!ec_parse_has_child(child_state));
}
- ec_parsed_free(child_state);
+ ec_parse_free(child_state);
comp->cur_state = cur_state;
comp->cur_group = cur_group;
}
static struct ec_comp_group *
-ec_comp_group(const struct ec_node *node, struct ec_parsed *parsed)
+ec_comp_group(const struct ec_node *node, struct ec_parse *parse)
{
struct ec_comp_group *grp = NULL;
if (grp->attrs == NULL)
goto fail;
- grp->state = ec_parsed_dup(parsed);
+ grp->state = ec_parse_dup(parse);
if (grp->state == NULL)
goto fail;
fail:
if (grp != NULL) {
- ec_parsed_free(grp->state);
+ ec_parse_free(grp->state);
ec_keyval_free(grp->attrs);
}
ec_free(grp);
TAILQ_REMOVE(&grp->items, item, next);
ec_comp_item_free(item);
}
- ec_parsed_free(ec_parsed_get_root(grp->state));
+ ec_parse_free(ec_parse_get_root(grp->state));
ec_keyval_free(grp->attrs);
ec_free(grp);
}
TAILQ_ENTRY(ec_comp_group) next;
const struct ec_node *node;
struct ec_comp_item_list items;
- struct ec_parsed *state;
+ struct ec_parse *state;
struct ec_keyval *attrs;
};
unsigned count_full;
unsigned count_partial;
unsigned count_unknown;
- struct ec_parsed *cur_state;
+ struct ec_parse *cur_state;
struct ec_comp_group *cur_group;
struct ec_comp_group_list groups;
struct ec_keyval *attrs;
*
*
*/
-struct ec_comp *ec_comp(struct ec_parsed *state);
+struct ec_comp *ec_comp(struct ec_parse *state);
/**
* Free a completion object and all its items.
int ec_comp_merge(struct ec_comp *to,
struct ec_comp *from);
-struct ec_parsed *ec_comp_get_state(struct ec_comp *comp);
+struct ec_parse *ec_comp_get_state(struct ec_comp *comp);
/* shortcut for ec_comp_item() + ec_comp_item_add() */
int ec_comp_add_item(struct ec_comp *comp,
#define EC_NODE_ENDLIST ((void *)1)
struct ec_node;
-struct ec_parsed;
+struct ec_parse;
struct ec_comp;
struct ec_strvec;
struct ec_keyval;
typedef int (*ec_node_build_t)(struct ec_node *node);
typedef int (*ec_node_parse_t)(const struct ec_node *node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec);
typedef int (*ec_node_complete_t)(const struct ec_node *node,
struct ec_comp *comp_state,
#include <ecoli_test.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_node_any.h>
};
static int ec_node_any_parse(const struct ec_node *gen_node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec)
{
(void)gen_node;
(void)state;
if (ec_strvec_len(strvec) == 0)
- return EC_PARSED_NOMATCH;
+ return EC_PARSE_NOMATCH;
return 1;
}
#include <ecoli_test.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_node_expr.h>
#include <ecoli_node_str.h>
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;
(void)userctx;
/* get parsed string vector, it should contain only one str */
- vec = ec_parsed_strvec(var);
+ vec = ec_parse_strvec(var);
if (ec_strvec_len(vec) != 1)
return -EINVAL;
str = ec_strvec_val(vec, 0);
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;
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;;
(void)userctx;
/* get parsed string vector, it should contain only one str */
- vec = ec_parsed_strvec(operator);
+ vec = ec_parse_strvec(operator);
if (ec_strvec_len(vec) != 1)
return -EINVAL;
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;
(void)userctx;
/* get parsed string vector, it should contain only one str */
- vec = ec_parsed_strvec(operator);
+ vec = ec_parse_strvec(operator);
if (ec_strvec_len(vec) > 1)
return -EINVAL;
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;
(void)close_paren;
/* get parsed string vector, it should contain only one str */
- vec = ec_parsed_strvec(open_paren);
+ vec = ec_parse_strvec(open_paren);
if (ec_strvec_len(vec) != 1)
return -EINVAL;
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;
goto fail;
ret = -EINVAL;
- if (!ec_parsed_matches(p))
+ if (!ec_parse_matches(p))
goto fail;
- if (!ec_parsed_has_child(p))
+ if (!ec_parse_has_child(p))
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;
return 0;
fail:
- ec_parsed_free(p);
+ ec_parse_free(p);
ec_node_free(expr);
ec_node_free(lex);
ec_node_free(cmd);
}
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;
#include <ecoli_strvec.h>
#include <ecoli_keyval.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_string.h>
#include <ecoli_node_str.h>
static int
ec_node_dynamic_parse(const struct ec_node *gen_node,
- struct ec_parsed *parsed,
+ struct ec_parse *parse,
const struct ec_strvec *strvec)
{
struct ec_node_dynamic *node = (struct ec_node_dynamic *)gen_node;
char key[64];
int ret = -1;
- child = node->build(parsed, node->opaque);
+ child = node->build(parse, node->opaque);
if (child == NULL)
goto fail;
/* add the node pointer in the attributes, so it will be freed
- * when parsed is freed */
+ * when parse is freed */
snprintf(key, sizeof(key), "_dyn_%p", child);
- ret = ec_keyval_set(ec_parsed_get_attrs(parsed), key, child,
+ ret = ec_keyval_set(ec_parse_get_attrs(parse), key, child,
(void *)node_free);
if (ret < 0) {
child = NULL; /* already freed */
goto fail;
}
- return ec_node_parse_child(child, parsed, strvec);
+ return ec_node_parse_child(child, parse, strvec);
fail:
ec_node_free(child);
const struct ec_strvec *strvec)
{
struct ec_node_dynamic *node = (struct ec_node_dynamic *)gen_node;
- struct ec_parsed *parsed;
+ struct ec_parse *parse;
struct ec_node *child = NULL;
void (*node_free)(struct ec_node *) = ec_node_free;
char key[64];
int ret = -1;
- parsed = ec_comp_get_state(comp);
- child = node->build(parsed, node->opaque);
+ parse = ec_comp_get_state(comp);
+ child = node->build(parse, node->opaque);
if (child == NULL)
goto fail;
/* add the node pointer in the attributes, so it will be freed
- * when parsed is freed */
+ * when parse is freed */
snprintf(key, sizeof(key), "_dyn_%p", child);
ret = ec_keyval_set(comp->attrs, key, child,
(void *)node_free);
EC_NODE_TYPE_REGISTER(ec_node_dynamic_type);
static struct ec_node *
-build_counter(struct ec_parsed *parsed, void *opaque)
+build_counter(struct ec_parse *parse, void *opaque)
{
const struct ec_node *node;
- struct ec_parsed *iter;
+ struct ec_parse *iter;
unsigned int count = 0;
char buf[32];
(void)opaque;
- for (iter = ec_parsed_get_root(parsed); iter != NULL;
- iter = ec_parsed_iter_next(iter)) {
- node = ec_parsed_get_node(iter);
+ for (iter = ec_parse_get_root(parse); iter != NULL;
+ iter = ec_parse_iter_next(iter)) {
+ node = ec_parse_get_node(iter);
if (node->id && !strcmp(node->id, "my-id"))
count++;
}
#define ECOLI_NODE_DYNAMIC_
struct ec_node;
-struct ec_parsed;
+struct ec_parse;
/* callback invoked by parse() or complete() to build the dynamic node
* the behavior of the node can depend on what is already parsed */
typedef struct ec_node *(*ec_node_dynamic_build_t)(
- struct ec_parsed *state, void *opaque);
+ struct ec_parse *state, void *opaque);
struct ec_node *ec_node_dynamic(const char *id, ec_node_dynamic_build_t build,
void *opaque);
#include <ecoli_test.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_node_empty.h>
};
static int ec_node_empty_parse(const struct ec_node *gen_node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec)
{
(void)gen_node;
#include <ecoli_test.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_node_seq.h>
#include <ecoli_node_many.h>
};
static int ec_node_expr_parse(const struct ec_node *gen_node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec)
{
struct ec_node_expr *node = (struct ec_node_expr *)gen_node;
struct result {
bool has_val;
void *val;
- const struct ec_parsed *op;
+ const struct ec_parse *op;
enum expr_node_type op_type;
};
void *userctx,
const struct ec_node_expr_eval_ops *ops,
const struct ec_node *expr_gen_node,
- const struct ec_parsed *parsed)
+ const struct ec_parse *parse)
{
- struct ec_parsed *open = NULL, *close = NULL;
+ struct ec_parse *open = NULL, *close = NULL;
struct result child_result;
- struct ec_parsed *child;
+ struct ec_parse *child;
enum expr_node_type type;
int ret;
memset(result, 0, sizeof(*result));
memset(&child_result, 0, sizeof(child_result));
- type = get_node_type(expr_gen_node, ec_parsed_get_node(parsed));
+ type = get_node_type(expr_gen_node, ec_parse_get_node(parse));
if (type == VAL) {
- ret = ops->eval_var(&result->val, userctx, parsed);
+ ret = ops->eval_var(&result->val, userctx, parse);
if (ret < 0)
goto fail;
result->has_val = 1;
} else if (type == PRE_OP || type == POST_OP || type == BIN_OP) {
- result->op = parsed;
+ result->op = parse;
result->op_type = type;
}
- EC_PARSED_FOREACH_CHILD(child, parsed) {
+ EC_PARSE_FOREACH_CHILD(child, parse) {
- type = get_node_type(expr_gen_node, ec_parsed_get_node(child));
+ type = get_node_type(expr_gen_node, ec_parse_get_node(child));
if (type == PAREN_OPEN) {
open = child;
continue;
}
int ec_node_expr_eval(void **user_result, const struct ec_node *node,
- struct ec_parsed *parsed, const struct ec_node_expr_eval_ops *ops,
+ struct ec_parse *parse, const struct ec_node_expr_eval_ops *ops,
void *userctx)
{
struct result result;
if (ret < 0)
return ret;
- if (!ec_parsed_matches(parsed))
+ if (!ec_parse_matches(parse))
return -EINVAL;
- ret = eval_expression(&result, userctx, ops, node, parsed);
+ ret = eval_expression(&result, userctx, ops, node, parse);
if (ret < 0)
return ret;
* A user-defined context passed to all callback functions, which
* can be used to maintain a state or store global information.
* @param var
- * The parsed result referencing the variable.
+ * The parse result referencing the variable.
* @return
* 0 on success (*result must be set), or -errno on error (*result
* is undefined).
*/
typedef int (*ec_node_expr_eval_var_t)(
void **result, void *userctx,
- const struct ec_parsed *var);
+ const struct ec_parse *var);
/**
* Callback function type for evaluating a prefix-operator
* @param operand
* The evaluated expression on which the operation should be applied.
* @param var
- * The parsed result referencing the operator.
+ * The parse result referencing the operator.
* @return
* 0 on success (*result must be set, operand is freed),
* or -errno on error (*result is undefined, operand is not freed).
typedef int (*ec_node_expr_eval_pre_op_t)(
void **result, void *userctx,
void *operand,
- const struct ec_parsed *operator);
+ const struct ec_parse *operator);
typedef int (*ec_node_expr_eval_post_op_t)(
void **result, void *userctx,
void *operand,
- const struct ec_parsed *operator);
+ const struct ec_parse *operator);
typedef int (*ec_node_expr_eval_bin_op_t)(
void **result, void *userctx,
void *operand1,
- const struct ec_parsed *operator,
+ const struct ec_parse *operator,
void *operand2);
typedef int (*ec_node_expr_eval_parenthesis_t)(
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);
typedef void (*ec_node_expr_eval_free_t)(
};
int ec_node_expr_eval(void **result, const struct ec_node *node,
- struct ec_parsed *parsed, const struct ec_node_expr_eval_ops *ops,
+ struct ec_parse *parse, const struct ec_node_expr_eval_ops *ops,
void *userctx);
#endif
#include <ecoli_strvec.h>
#include <ecoli_test.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_node_int.h>
#include <ecoli_node_str.h>
#include <ecoli_node_re_lex.h>
static int
ec_node_expr_test_eval_var(void **result, void *userctx,
- const struct ec_parsed *var)
+ const struct ec_parse *var)
{
const struct ec_strvec *vec;
const struct ec_node *node;
(void)userctx;
/* get parsed string vector, it should contain only one str */
- vec = ec_parsed_strvec(var);
+ vec = ec_parse_strvec(var);
if (ec_strvec_len(vec) != 1)
return -EINVAL;
- node = ec_parsed_get_node(var);
+ node = ec_parse_get_node(var);
if (ec_node_int_getval(node, ec_strvec_val(vec, 0), &val) < 0)
return -EINVAL;
static int
ec_node_expr_test_eval_pre_op(void **result, void *userctx, void *operand,
- const struct ec_parsed *operator)
+ const struct ec_parse *operator)
{
const struct ec_strvec *vec;
struct my_eval_result *eval = operand;;
(void)userctx;
/* get parsed string vector, it should contain only one str */
- vec = ec_parsed_strvec(operator);
+ vec = ec_parse_strvec(operator);
if (ec_strvec_len(vec) != 1)
return -EINVAL;
static int
ec_node_expr_test_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 my_eval_result *eval = operand;;
(void)userctx;
/* get parsed string vector, it should contain only one str */
- vec = ec_parsed_strvec(operator);
+ vec = ec_parse_strvec(operator);
if (ec_strvec_len(vec) != 1)
return -EINVAL;
static int
ec_node_expr_test_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;
(void)userctx;
/* get parsed string vector, it should contain only one str */
- vec = ec_parsed_strvec(operator);
+ vec = ec_parse_strvec(operator);
if (ec_strvec_len(vec) != 1)
return -EINVAL;
static int
ec_node_expr_test_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)
{
(void)userctx;
const struct ec_node *expr_node,
const char *str, int val)
{
- struct ec_parsed *p;
+ struct ec_parse *p;
void *result;
struct my_eval_result *eval;
int ret;
return -1;
ret = ec_node_expr_eval(&result, expr_node, p, &test_ops, NULL);
- ec_parsed_free(p);
+ ec_parse_free(p);
if (ret < 0)
return -1;
- /* the parsed value is an integer */
+ /* the parse value is an integer */
eval = result;
assert(eval != NULL);
#include <ecoli_strvec.h>
#include <ecoli_string.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_node_file.h>
static int
ec_node_file_parse(const struct ec_node *gen_node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec)
{
(void)gen_node;
(void)state;
if (ec_strvec_len(strvec) == 0)
- return EC_PARSED_NOMATCH;
+ return EC_PARSE_NOMATCH;
return 1;
}
#include <ecoli_malloc.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_node_int.h>
#include <ecoli_test.h>
}
static int ec_node_int_uint_parse(const struct ec_node *gen_node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec)
{
struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
(void)state;
if (ec_strvec_len(strvec) == 0)
- return EC_PARSED_NOMATCH;
+ return EC_PARSE_NOMATCH;
str = ec_strvec_val(strvec, 0);
if (node->is_signed) {
if (parse_llint(node, str, &i64) < 0)
- return EC_PARSED_NOMATCH;
+ return EC_PARSE_NOMATCH;
} else {
if (parse_ullint(node, str, &u64) < 0)
- return EC_PARSED_NOMATCH;
+ return EC_PARSE_NOMATCH;
}
return 1;
}
/* LCOV_EXCL_START */
static int ec_node_int_testcase(void)
{
- struct ec_parsed *p;
+ struct ec_parse *p;
struct ec_node *node;
const char *s;
int testres = 0, ret;
testres |= EC_TEST_CHECK_PARSE(node, 1, "0");
p = ec_node_parse(node, "1");
- s = ec_strvec_val(ec_parsed_strvec(p), 0);
+ s = ec_strvec_val(ec_parse_strvec(p), 0);
testres |= EC_TEST_CHECK(s != NULL &&
ec_node_uint_getval(node, s, &u64) == 0 &&
u64 == 1, "bad integer value");
- ec_parsed_free(p);
+ ec_parse_free(p);
p = ec_node_parse(node, "10");
- s = ec_strvec_val(ec_parsed_strvec(p), 0);
+ s = ec_strvec_val(ec_parse_strvec(p), 0);
testres |= EC_TEST_CHECK(s != NULL &&
ec_node_uint_getval(node, s, &u64) == 0 &&
u64 == 10, "bad integer value");
- ec_parsed_free(p);
+ ec_parse_free(p);
ec_node_free(node);
node = ec_node_int(EC_NO_ID, -1, LLONG_MAX, 16);
testres |= EC_TEST_CHECK_PARSE(node, 1, "-2");
p = ec_node_parse(node, "10");
- s = ec_strvec_val(ec_parsed_strvec(p), 0);
+ s = ec_strvec_val(ec_parse_strvec(p), 0);
testres |= EC_TEST_CHECK(s != NULL &&
ec_node_int_getval(node, s, &i64) == 0 &&
i64 == 16, "bad integer value");
- ec_parsed_free(p);
+ ec_parse_free(p);
ec_node_free(node);
node = ec_node_int(EC_NO_ID, LLONG_MIN, 0, 10);
#include <ecoli_test.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_node_str.h>
#include <ecoli_node_option.h>
};
static int ec_node_many_parse(const struct ec_node *gen_node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec)
{
struct ec_node_many *node = (struct ec_node_many *)gen_node;
- struct ec_parsed *child_parsed;
+ struct ec_parse *child_parse;
struct ec_strvec *childvec = NULL;
size_t off = 0, count;
int ret;
ec_strvec_free(childvec);
childvec = NULL;
- if (ret == EC_PARSED_NOMATCH)
+ if (ret == EC_PARSE_NOMATCH)
break;
/* it matches an empty strvec, no need to continue */
if (ret == 0) {
- child_parsed = ec_parsed_get_last_child(state);
- ec_parsed_unlink_child(state, child_parsed);
- ec_parsed_free(child_parsed);
+ child_parse = ec_parse_get_last_child(state);
+ ec_parse_unlink_child(state, child_parse);
+ ec_parse_free(child_parse);
break;
}
}
if (count < node->min) {
- ec_parsed_free_children(state);
- return EC_PARSED_NOMATCH;
+ ec_parse_free_children(state);
+ return EC_PARSE_NOMATCH;
}
return off;
struct ec_comp *comp,
const struct ec_strvec *strvec)
{
- struct ec_parsed *parsed = ec_comp_get_state(comp);
+ struct ec_parse *parse = ec_comp_get_state(comp);
struct ec_strvec *childvec = NULL;
unsigned int i;
int ret;
if (childvec == NULL)
goto fail;
- ret = ec_node_parse_child(node->child, parsed, childvec);
+ ret = ec_node_parse_child(node->child, parse, childvec);
if (ret < 0)
goto fail;
childvec = NULL;
if ((unsigned int)ret != i) {
- if (ret != EC_PARSED_NOMATCH)
- ec_parsed_del_last_child(parsed);
+ if (ret != EC_PARSE_NOMATCH)
+ ec_parse_del_last_child(parse);
continue;
}
childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i);
if (childvec == NULL) {
- ec_parsed_del_last_child(parsed);
+ ec_parse_del_last_child(parse);
goto fail;
}
ret = __ec_node_many_complete(node, max, comp, childvec);
- ec_parsed_del_last_child(parsed);
+ ec_parse_del_last_child(parse);
ec_strvec_free(childvec);
childvec = NULL;
#include <ecoli_test.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_node_none.h>
};
static int ec_node_none_parse(const struct ec_node *gen_node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec)
{
(void)gen_node;
(void)state;
(void)strvec;
- return EC_PARSED_NOMATCH;
+ return EC_PARSE_NOMATCH;
}
static int
#include <ecoli_test.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_node_str.h>
#include <ecoli_node_or.h>
};
static unsigned int
-count_node(struct ec_parsed *parsed, const struct ec_node *node)
+count_node(struct ec_parse *parse, const struct ec_node *node)
{
- struct ec_parsed *child;
+ struct ec_parse *child;
unsigned int count = 0;
- if (parsed == NULL)
+ if (parse == NULL)
return 0;
- if (ec_parsed_get_node(parsed) == node)
+ if (ec_parse_get_node(parse) == node)
count++;
- EC_PARSED_FOREACH_CHILD(child, parsed)
+ EC_PARSE_FOREACH_CHILD(child, parse)
count += count_node(child, node);
return count;
static int
ec_node_once_parse(const struct ec_node *gen_node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec)
{
struct ec_node_once *node = (struct ec_node_once *)gen_node;
/* count the number of occurences of the node: if already parsed,
* do not match
*/
- count = count_node(ec_parsed_get_root(state), node->child);
+ count = count_node(ec_parse_get_root(state), node->child);
if (count > 0)
- return EC_PARSED_NOMATCH;
+ return EC_PARSE_NOMATCH;
return ec_node_parse_child(node->child, state, strvec);
}
const struct ec_strvec *strvec)
{
struct ec_node_once *node = (struct ec_node_once *)gen_node;
- struct ec_parsed *parsed = ec_comp_get_state(comp);
+ struct ec_parse *parse = ec_comp_get_state(comp);
unsigned int count;
int ret;
/* count the number of occurences of the node: if already parsed,
* do not match
*/
- count = count_node(ec_parsed_get_root(parsed), node->child);
+ count = count_node(ec_parse_get_root(parse), node->child);
if (count > 0)
return 0;
#include <ecoli_log.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_node_str.h>
#include <ecoli_test.h>
static int
ec_node_option_parse(const struct ec_node *gen_node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec)
{
struct ec_node_option *node = (struct ec_node_option *)gen_node;
if (ret < 0)
return ret;
- if (ret == EC_PARSED_NOMATCH)
+ if (ret == EC_PARSE_NOMATCH)
return 0;
return ret;
#include <ecoli_log.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_node_or.h>
#include <ecoli_node_str.h>
static int
ec_node_or_parse(const struct ec_node *gen_node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec)
{
struct ec_node_or *node = (struct ec_node_or *)gen_node;
for (i = 0; i < node->len; i++) {
ret = ec_node_parse_child(node->table[i], state, strvec);
- if (ret == EC_PARSED_NOMATCH)
+ if (ret == EC_PARSE_NOMATCH)
continue;
return ret;
}
- return EC_PARSED_NOMATCH;
+ return EC_PARSE_NOMATCH;
}
static int
#include <ecoli_test.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_node_re.h>
static int
ec_node_re_parse(const struct ec_node *gen_node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec)
{
struct ec_node_re *node = (struct ec_node_re *)gen_node;
(void)state;
if (ec_strvec_len(strvec) == 0)
- return EC_PARSED_NOMATCH;
+ return EC_PARSE_NOMATCH;
str = ec_strvec_val(strvec, 0);
if (regexec(&node->re, str, 1, &pos, 0) != 0)
- return EC_PARSED_NOMATCH;
+ return EC_PARSE_NOMATCH;
if (pos.rm_so != 0 || pos.rm_eo != (int)strlen(str))
- return EC_PARSED_NOMATCH;
+ return EC_PARSE_NOMATCH;
return 1;
}
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_complete.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_node_many.h>
#include <ecoli_node_or.h>
#include <ecoli_node_str.h>
static int
ec_node_re_lex_parse(const struct ec_node *gen_node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec)
{
struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
struct ec_strvec *new_vec = NULL;
- struct ec_parsed *child_parsed;
+ struct ec_parse *child_parse;
const char *str;
int ret;
if ((unsigned)ret == ec_strvec_len(new_vec)) {
ret = 1;
- } else if (ret != EC_PARSED_NOMATCH) {
- child_parsed = ec_parsed_get_last_child(state);
- ec_parsed_unlink_child(state, child_parsed);
- ec_parsed_free(child_parsed);
- ret = EC_PARSED_NOMATCH;
+ } else if (ret != EC_PARSE_NOMATCH) {
+ child_parse = ec_parse_get_last_child(state);
+ ec_parse_unlink_child(state, child_parse);
+ ec_parse_free(child_parse);
+ ret = EC_PARSE_NOMATCH;
}
ec_strvec_free(new_vec);
#include <ecoli_test.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_node_str.h>
#include <ecoli_node_option.h>
static int
ec_node_seq_parse(const struct ec_node *gen_node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec)
{
struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
ec_strvec_free(childvec);
childvec = NULL;
- if (ret == EC_PARSED_NOMATCH) {
- ec_parsed_free_children(state);
- return EC_PARSED_NOMATCH;
+ if (ret == EC_PARSE_NOMATCH) {
+ ec_parse_free_children(state);
+ return EC_PARSE_NOMATCH;
}
len += ret;
struct ec_comp *comp,
const struct ec_strvec *strvec)
{
- struct ec_parsed *parsed = ec_comp_get_state(comp);
+ struct ec_parse *parse = ec_comp_get_state(comp);
struct ec_strvec *childvec = NULL;
unsigned int i;
int ret;
if (childvec == NULL)
goto fail;
- ret = ec_node_parse_child(table[0], parsed, childvec);
+ ret = ec_node_parse_child(table[0], parse, childvec);
if (ret < 0)
goto fail;
childvec = NULL;
if ((unsigned int)ret != i) {
- if (ret != EC_PARSED_NOMATCH)
- ec_parsed_del_last_child(parsed);
+ if (ret != EC_PARSE_NOMATCH)
+ ec_parse_del_last_child(parse);
continue;
}
childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i);
if (childvec == NULL) {
- ec_parsed_del_last_child(parsed);
+ ec_parse_del_last_child(parse);
goto fail;
}
ret = __ec_node_seq_complete(&table[1],
table_len - 1,
comp, childvec);
- ec_parsed_del_last_child(parsed);
+ ec_parse_del_last_child(parse);
ec_strvec_free(childvec);
childvec = NULL;
#include <ecoli_test.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_node_seq.h>
#include <ecoli_node_str.h>
static int
ec_node_sh_lex_parse(const struct ec_node *gen_node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec)
{
struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
struct ec_strvec *new_vec = NULL;
- struct ec_parsed *child_parsed;
+ struct ec_parse *child_parse;
const char *str;
int ret;
}
if (new_vec == NULL) {
if (errno == EINVAL)
- ret = EC_PARSED_NOMATCH;
+ ret = EC_PARSE_NOMATCH;
else
ret = -ENOMEM;
goto fail;
if ((unsigned)ret == ec_strvec_len(new_vec)) {
ret = 1;
- } else if (ret != EC_PARSED_NOMATCH) {
- child_parsed = ec_parsed_get_last_child(state);
- ec_parsed_unlink_child(state, child_parsed);
- ec_parsed_free(child_parsed);
- ret = EC_PARSED_NOMATCH;
+ } else if (ret != EC_PARSE_NOMATCH) {
+ child_parse = ec_parse_get_last_child(state);
+ ec_parse_unlink_child(state, child_parse);
+ ec_parse_free(child_parse);
+ ret = EC_PARSE_NOMATCH;
}
ec_strvec_free(new_vec);
#include <ecoli_malloc.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_node_space.h>
static int
ec_node_space_parse(const struct ec_node *gen_node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec)
{
const char *str;
(void)gen_node;
if (ec_strvec_len(strvec) == 0)
- return EC_PARSED_NOMATCH;
+ return EC_PARSE_NOMATCH;
str = ec_strvec_val(strvec, 0);
while (isspace(str[len]))
len++;
if (len == 0 || len != strlen(str))
- return EC_PARSED_NOMATCH;
+ return EC_PARSE_NOMATCH;
return 1;
}
#include <ecoli_test.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_node_str.h>
static int
ec_node_str_parse(const struct ec_node *gen_node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec)
{
struct ec_node_str *node = (struct ec_node_str *)gen_node;
(void)state;
if (ec_strvec_len(strvec) == 0)
- return EC_PARSED_NOMATCH;
+ return EC_PARSE_NOMATCH;
str = ec_strvec_val(strvec, 0);
if (strcmp(str, node->string) != 0)
- return EC_PARSED_NOMATCH;
+ return EC_PARSE_NOMATCH;
return 1;
}
/* no completion */
if (str[n] != '\0')
- return EC_PARSED_NOMATCH;
+ return EC_PARSE_NOMATCH;
if (ec_comp_add_item(comp, gen_node, NULL, EC_COMP_FULL,
str, node->string) < 0)
#include <ecoli_log.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_node_subset.h>
#include <ecoli_node_str.h>
};
struct parse_result {
- size_t parsed_len; /* number of parsed node */
+ size_t parse_len; /* number of parsed nodes */
size_t len; /* consumed strings */
};
* updated accordingly. */
static int
__ec_node_subset_parse(struct parse_result *out, struct ec_node **table,
- size_t table_len, struct ec_parsed *state,
+ size_t table_len, struct ec_parse *state,
const struct ec_strvec *strvec)
{
struct ec_node **child_table;
struct ec_strvec *childvec = NULL;
size_t i, j, len = 0;
struct parse_result best_result, result;
- struct ec_parsed *best_parsed = NULL;
+ struct ec_parse *best_parse = NULL;
int ret;
if (table_len == 0)
if (ret < 0)
goto fail;
- if (ret == EC_PARSED_NOMATCH)
+ if (ret == EC_PARSE_NOMATCH)
continue;
/* build a new table without elt i */
goto fail;
/* if result is not the best, ignore */
- if (result.parsed_len < best_result.parsed_len) {
+ if (result.parse_len < best_result.parse_len) {
memset(&result, 0, sizeof(result));
- ec_parsed_del_last_child(state);
+ ec_parse_del_last_child(state);
continue;
}
/* replace the previous best result */
- ec_parsed_free(best_parsed);
- best_parsed = ec_parsed_get_last_child(state);
- ec_parsed_unlink_child(state, best_parsed);
+ ec_parse_free(best_parse);
+ best_parse = ec_parse_get_last_child(state);
+ ec_parse_unlink_child(state, best_parse);
- best_result.parsed_len = result.parsed_len + 1;
+ best_result.parse_len = result.parse_len + 1;
best_result.len = len + result.len;
memset(&result, 0, sizeof(result));
*out = best_result;
ec_free(child_table);
- if (best_parsed != NULL)
- ec_parsed_link_child(state, best_parsed);
+ if (best_parse != NULL)
+ ec_parse_link_child(state, best_parse);
return 0;
fail:
- ec_parsed_free(best_parsed);
+ ec_parse_free(best_parse);
ec_strvec_free(childvec);
ec_free(child_table);
return ret;
static int
ec_node_subset_parse(const struct ec_node *gen_node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec)
{
struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
- struct ec_parsed *parsed = NULL;
+ struct ec_parse *parse = NULL;
struct parse_result result;
int ret;
goto fail;
/* if no child node matches, return a matching empty strvec */
- if (result.parsed_len == 0)
+ if (result.parse_len == 0)
return 0;
return result.len;
fail:
- ec_parsed_free(parsed);
+ ec_parse_free(parse);
return ret;
}
struct ec_comp *comp,
const struct ec_strvec *strvec)
{
- struct ec_parsed *parsed = ec_comp_get_state(comp);
+ struct ec_parse *parse = ec_comp_get_state(comp);
struct ec_strvec *childvec = NULL;
struct ec_node *save;
size_t i, len;
if (table[i] == NULL)
continue;
- ret = ec_node_parse_child(table[i], parsed, strvec);
+ ret = ec_node_parse_child(table[i], parse, strvec);
if (ret < 0)
goto fail;
- if (ret == EC_PARSED_NOMATCH)
+ if (ret == EC_PARSE_NOMATCH)
continue;
len = ret;
childvec = ec_strvec_ndup(strvec, len,
ec_strvec_len(strvec) - len);
if (childvec == NULL) {
- ec_parsed_del_last_child(parsed);
+ ec_parse_del_last_child(parse);
goto fail;
}
table[i] = save;
ec_strvec_free(childvec);
childvec = NULL;
- ec_parsed_del_last_child(parsed);
+ ec_parse_del_last_child(parse);
if (ret < 0)
goto fail;
#include <ecoli_test.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_node_str.h>
#include <ecoli_node_option.h>
static int
ec_node_weakref_parse(const struct ec_node *gen_node,
- struct ec_parsed *state,
+ struct ec_parse *state,
const struct ec_strvec *strvec)
{
struct ec_node_weakref *node = (struct ec_node_weakref *)gen_node;
--- /dev/null
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+
+#include <ecoli_assert.h>
+#include <ecoli_malloc.h>
+#include <ecoli_strvec.h>
+#include <ecoli_keyval.h>
+#include <ecoli_log.h>
+#include <ecoli_node.h>
+#include <ecoli_parse.h>
+
+TAILQ_HEAD(ec_parse_list, ec_parse);
+
+struct ec_parse {
+ TAILQ_ENTRY(ec_parse) next;
+ struct ec_parse_list children;
+ struct ec_parse *parent;
+ const struct ec_node *node;
+ struct ec_strvec *strvec;
+ struct ec_keyval *attrs;
+};
+
+static int __ec_node_parse_child(const struct ec_node *node,
+ struct ec_parse *state,
+ bool is_root, const struct ec_strvec *strvec)
+{
+ struct ec_strvec *match_strvec;
+ struct ec_parse *child = NULL;
+ int ret;
+
+ if (node->type->parse == NULL)
+ return -ENOTSUP;
+
+ if (!is_root) {
+ child = ec_parse(node);
+ if (child == NULL)
+ return -ENOMEM;
+
+ ec_parse_link_child(state, child);
+ } else {
+ child = state;
+ }
+ ret = node->type->parse(node, child, strvec);
+ if (ret < 0 || ret == EC_PARSE_NOMATCH)
+ goto free;
+
+ match_strvec = ec_strvec_ndup(strvec, 0, ret);
+ if (match_strvec == NULL) {
+ ret = -ENOMEM;
+ goto free;
+ }
+
+ child->strvec = match_strvec;
+
+ return ret;
+
+free:
+ if (!is_root) {
+ ec_parse_unlink_child(state, child);
+ ec_parse_free(child);
+ }
+ return ret;
+}
+
+int ec_node_parse_child(const struct ec_node *node, struct ec_parse *state,
+ const struct ec_strvec *strvec)
+{
+ assert(state != NULL);
+ return __ec_node_parse_child(node, state, false, strvec);
+}
+
+struct ec_parse *ec_node_parse_strvec(const struct ec_node *node,
+ const struct ec_strvec *strvec)
+{
+ struct ec_parse *parse = ec_parse(node);
+ int ret;
+
+ if (parse == NULL)
+ return NULL;
+
+ ret = __ec_node_parse_child(node, parse, true, strvec);
+ if (ret < 0) {
+ ec_parse_free(parse);
+ return NULL;
+ }
+
+ return parse;
+}
+
+struct ec_parse *ec_node_parse(const struct ec_node *node, const char *str)
+{
+ struct ec_strvec *strvec = NULL;
+ struct ec_parse *parse = NULL;
+
+ errno = ENOMEM;
+ strvec = ec_strvec();
+ if (strvec == NULL)
+ goto fail;
+
+ if (ec_strvec_add(strvec, str) < 0)
+ goto fail;
+
+ parse = ec_node_parse_strvec(node, strvec);
+ if (parse == NULL)
+ goto fail;
+
+ ec_strvec_free(strvec);
+ return parse;
+
+ fail:
+ ec_strvec_free(strvec);
+ ec_parse_free(parse);
+ return NULL;
+}
+
+struct ec_parse *ec_parse(const struct ec_node *node)
+{
+ struct ec_parse *parse = NULL;
+
+ parse = ec_calloc(1, sizeof(*parse));
+ if (parse == NULL)
+ goto fail;
+
+ TAILQ_INIT(&parse->children);
+
+ parse->node = node;
+ parse->attrs = ec_keyval();
+ if (parse->attrs == NULL)
+ goto fail;
+
+ return parse;
+
+ fail:
+ if (parse != NULL)
+ ec_keyval_free(parse->attrs);
+ ec_free(parse);
+
+ return NULL;
+}
+
+static struct ec_parse *
+__ec_parse_dup(const struct ec_parse *root, const struct ec_parse *ref,
+ struct ec_parse **new_ref)
+{
+ struct ec_parse *dup = NULL;
+ struct ec_parse *child, *dup_child;
+ struct ec_keyval *attrs = NULL;
+
+ if (root == NULL)
+ return NULL;
+
+ dup = ec_parse(root->node);
+ if (dup == NULL)
+ return NULL;
+
+ if (root == ref)
+ *new_ref = dup;
+
+ attrs = ec_keyval_dup(root->attrs);
+ if (attrs == NULL)
+ goto fail;
+ ec_keyval_free(dup->attrs);
+ dup->attrs = attrs;
+
+ if (root->strvec != NULL) {
+ dup->strvec = ec_strvec_dup(root->strvec);
+ if (dup->strvec == NULL)
+ goto fail;
+ }
+
+ TAILQ_FOREACH(child, &root->children, next) {
+ dup_child = __ec_parse_dup(child, ref, new_ref);
+ if (dup_child == NULL)
+ goto fail;
+ ec_parse_link_child(dup, dup_child);
+ }
+
+ return dup;
+
+fail:
+ ec_parse_free(dup);
+ return NULL;
+}
+
+struct ec_parse *ec_parse_dup(const struct ec_parse *parse)
+{
+ const struct ec_parse *root;
+ struct ec_parse *dup_root, *dup = NULL;
+
+ root = ec_parse_get_root(parse);
+ dup_root = __ec_parse_dup(root, parse, &dup);
+ if (dup_root == NULL)
+ return NULL;
+ assert(dup != NULL);
+
+ return dup;
+}
+
+void ec_parse_free_children(struct ec_parse *parse)
+{
+ struct ec_parse *child;
+
+ if (parse == NULL)
+ return;
+
+ while (!TAILQ_EMPTY(&parse->children)) {
+ child = TAILQ_FIRST(&parse->children);
+ TAILQ_REMOVE(&parse->children, child, next);
+ child->parent = NULL;
+ ec_parse_free(child);
+ }
+}
+
+void ec_parse_free(struct ec_parse *parse)
+{
+ if (parse == NULL)
+ return;
+
+ ec_assert_print(parse->parent == NULL,
+ "parent not NULL in ec_parse_free()");
+
+ ec_parse_free_children(parse);
+ ec_strvec_free(parse->strvec);
+ ec_keyval_free(parse->attrs);
+ ec_free(parse);
+}
+
+static void __ec_parse_dump(FILE *out,
+ const struct ec_parse *parse, size_t indent)
+{
+ struct ec_parse *child;
+ const struct ec_strvec *vec;
+ const char *id, *typename = "none";
+
+ /* node can be null when parsing is incomplete */
+ if (parse->node != NULL) {
+ id = parse->node->id;
+ typename = parse->node->type->name;
+ }
+
+ fprintf(out, "%*s" "type=%s id=%s vec=",
+ (int)indent * 4, "", typename, id);
+ vec = ec_parse_strvec(parse);
+ ec_strvec_dump(out, vec);
+
+ TAILQ_FOREACH(child, &parse->children, next)
+ __ec_parse_dump(out, child, indent + 1);
+}
+
+void ec_parse_dump(FILE *out, const struct ec_parse *parse)
+{
+ fprintf(out, "------------------- parse dump:\n");
+
+ if (parse == NULL) {
+ fprintf(out, "parse is NULL\n");
+ return;
+ }
+
+ /* only exist if it does not match (strvec == NULL) and if it
+ * does not have children: an incomplete parse, like those
+ * generated by complete() don't match but have children that
+ * may match. */
+ if (!ec_parse_matches(parse) && TAILQ_EMPTY(&parse->children)) {
+ fprintf(out, "no match\n");
+ return;
+ }
+
+ __ec_parse_dump(out, parse, 0);
+}
+
+void ec_parse_link_child(struct ec_parse *parse,
+ struct ec_parse *child)
+{
+ TAILQ_INSERT_TAIL(&parse->children, child, next);
+ child->parent = parse;
+}
+
+void ec_parse_unlink_child(struct ec_parse *parse,
+ struct ec_parse *child)
+{
+ TAILQ_REMOVE(&parse->children, child, next);
+ child->parent = NULL;
+}
+
+struct ec_parse *
+ec_parse_get_first_child(const struct ec_parse *parse)
+{
+ return TAILQ_FIRST(&parse->children);
+}
+
+struct ec_parse *
+ec_parse_get_last_child(const struct ec_parse *parse)
+{
+ return TAILQ_LAST(&parse->children, ec_parse_list);
+}
+
+struct ec_parse *ec_parse_get_next(const struct ec_parse *parse)
+{
+ return TAILQ_NEXT(parse, next);
+}
+
+bool ec_parse_has_child(const struct ec_parse *parse)
+{
+ return !TAILQ_EMPTY(&parse->children);
+}
+
+const struct ec_node *ec_parse_get_node(const struct ec_parse *parse)
+{
+ return parse->node;
+}
+
+void ec_parse_del_last_child(struct ec_parse *parse)
+{
+ struct ec_parse *child;
+
+ child = ec_parse_get_last_child(parse);
+ ec_parse_unlink_child(parse, child);
+ ec_parse_free(child);
+}
+
+struct ec_parse *__ec_parse_get_root(struct ec_parse *parse)
+{
+ if (parse == NULL)
+ return NULL;
+
+ while (parse->parent != NULL)
+ parse = parse->parent;
+
+ return parse;
+}
+
+struct ec_parse *ec_parse_get_parent(const struct ec_parse *parse)
+{
+ if (parse == NULL)
+ return NULL;
+
+ return parse->parent;
+}
+
+struct ec_parse *ec_parse_iter_next(struct ec_parse *parse)
+{
+ struct ec_parse *child, *parent, *next;
+
+ child = TAILQ_FIRST(&parse->children);
+ if (child != NULL)
+ return child;
+ parent = parse->parent;
+ while (parent != NULL) {
+ next = TAILQ_NEXT(parse, next);
+ if (next != NULL)
+ return next;
+ parse = parent;
+ parent = parse->parent;
+ }
+ return NULL;
+}
+
+struct ec_parse *ec_parse_find_first(struct ec_parse *parse,
+ const char *id)
+{
+ struct ec_parse *child, *ret;
+
+ if (parse == NULL)
+ return NULL;
+
+ if (parse->node != NULL &&
+ parse->node->id != NULL &&
+ !strcmp(parse->node->id, id))
+ return parse;
+
+ TAILQ_FOREACH(child, &parse->children, next) {
+ ret = ec_parse_find_first(child, id);
+ if (ret != NULL)
+ return ret;
+ }
+
+ return NULL;
+}
+
+struct ec_keyval *
+ec_parse_get_attrs(struct ec_parse *parse)
+{
+ if (parse == NULL)
+ return NULL;
+
+ return parse->attrs;
+}
+
+const struct ec_strvec *ec_parse_strvec(const struct ec_parse *parse)
+{
+ if (parse == NULL || parse->strvec == NULL)
+ return NULL;
+
+ return parse->strvec;
+}
+
+/* number of strings in the parse vector */
+size_t ec_parse_len(const struct ec_parse *parse)
+{
+ if (parse == NULL || parse->strvec == NULL)
+ return 0;
+
+ return ec_strvec_len(parse->strvec);
+}
+
+size_t ec_parse_matches(const struct ec_parse *parse)
+{
+ if (parse == NULL)
+ return 0;
+
+ if (parse->strvec == NULL)
+ return 0;
+
+ return 1;
+}
--- /dev/null
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
+ */
+
+/**
+ * Node parse API.
+ *
+ * The parse operation is to check if an input (a string or vector of
+ * strings) matches the node tree. On success, the result is stored in a
+ * tree that describes which part of the input matches which node.
+ */
+
+#ifndef ECOLI_PARSE_
+#define ECOLI_PARSE_
+
+#include <sys/queue.h>
+#include <sys/types.h>
+#include <limits.h>
+#include <stdio.h>
+
+struct ec_node;
+struct ec_parse;
+
+/**
+ * Create an empty parse tree.
+ *
+ * @return
+ * The empty parse tree.
+ */
+struct ec_parse *ec_parse(const struct ec_node *node);
+
+/**
+ *
+ *
+ *
+ */
+void ec_parse_free(struct ec_parse *parse);
+
+/**
+ *
+ *
+ *
+ */
+void ec_parse_free_children(struct ec_parse *parse);
+
+/**
+ *
+ *
+ *
+ */
+struct ec_parse *ec_parse_dup(const struct ec_parse *parse);
+
+/**
+ *
+ *
+ *
+ */
+const struct ec_strvec *ec_parse_strvec(const struct ec_parse *parse);
+
+/* a NULL return value is an error, with errno set
+ ENOTSUP: no ->parse() operation
+*/
+/**
+ *
+ *
+ *
+ */
+struct ec_parse *ec_node_parse(const struct ec_node *node, const char *str);
+
+/**
+ *
+ *
+ *
+ */
+struct ec_parse *ec_node_parse_strvec(const struct ec_node *node,
+ const struct ec_strvec *strvec);
+
+/**
+ *
+ *
+ *
+ */
+#define EC_PARSE_NOMATCH INT_MAX
+
+/* internal: used by nodes
+ *
+ * state is the current parse tree, which is built piece by piece while
+ * parsing the node tree: ec_node_parse_child() creates a new child in
+ * this state parse tree, and calls the parse() method for the child
+ * node, with state pointing to this new child. If it does not match,
+ * the child is removed in the state, else it is kept, with its
+ * possible descendants.
+ *
+ * return:
+ * EC_PARSE_NOMATCH (positive) if it does not match
+ * any other negative value (-errno) for other errors
+ * the number of matched strings in strvec
+ */
+int ec_node_parse_child(const struct ec_node *node,
+ struct ec_parse *state,
+ const struct ec_strvec *strvec);
+
+/**
+ *
+ *
+ *
+ */
+void ec_parse_link_child(struct ec_parse *parse,
+ struct ec_parse *child);
+/**
+ *
+ *
+ *
+ */
+void ec_parse_unlink_child(struct ec_parse *parse,
+ struct ec_parse *child);
+
+/* keep the const */
+#define ec_parse_get_root(parse) ({ \
+ const struct ec_parse *p_ = parse; /* check type */ \
+ struct ec_parse *parse_ = (struct ec_parse *)parse; \
+ typeof(parse) res_; \
+ (void)p_; \
+ res_ = __ec_parse_get_root(parse_); \
+ res_; \
+})
+
+/**
+ *
+ *
+ *
+ */
+struct ec_parse *__ec_parse_get_root(struct ec_parse *parse);
+
+/**
+ *
+ *
+ *
+ */
+struct ec_parse *ec_parse_get_parent(const struct ec_parse *parse);
+
+/**
+ * Get the first child of a tree.
+ *
+ */
+struct ec_parse *ec_parse_get_first_child(const struct ec_parse *parse);
+
+/**
+ *
+ *
+ *
+ */
+struct ec_parse *ec_parse_get_last_child(const struct ec_parse *parse);
+
+/**
+ *
+ *
+ *
+ */
+struct ec_parse *ec_parse_get_next(const struct ec_parse *parse);
+
+/**
+ *
+ *
+ *
+ */
+#define EC_PARSE_FOREACH_CHILD(child, parse) \
+ for (child = ec_parse_get_first_child(parse); \
+ child != NULL; \
+ child = ec_parse_get_next(child)) \
+
+/**
+ *
+ *
+ *
+ */
+bool ec_parse_has_child(const struct ec_parse *parse);
+
+/**
+ *
+ *
+ *
+ */
+const struct ec_node *ec_parse_get_node(const struct ec_parse *parse);
+
+/**
+ *
+ *
+ *
+ */
+void ec_parse_del_last_child(struct ec_parse *parse);
+
+/**
+ *
+ *
+ *
+ */
+struct ec_keyval *ec_parse_get_attrs(struct ec_parse *parse);
+
+/**
+ *
+ *
+ *
+ */
+void ec_parse_dump(FILE *out, const struct ec_parse *parse);
+
+/**
+ *
+ *
+ *
+ */
+struct ec_parse *ec_parse_find_first(struct ec_parse *parse,
+ const char *id);
+
+/**
+ * Iterate among parse tree
+ *
+ * Use it with:
+ * for (iter = state; iter != NULL; iter = ec_parse_iter_next(iter))
+ */
+struct ec_parse *ec_parse_iter_next(struct ec_parse *parse);
+
+/**
+ *
+ *
+ *
+ */
+size_t ec_parse_len(const struct ec_parse *parse);
+
+/**
+ *
+ *
+ *
+ */
+size_t ec_parse_matches(const struct ec_parse *parse);
+
+#endif
+++ /dev/null
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <string.h>
-#include <assert.h>
-#include <errno.h>
-
-#include <ecoli_assert.h>
-#include <ecoli_malloc.h>
-#include <ecoli_strvec.h>
-#include <ecoli_keyval.h>
-#include <ecoli_log.h>
-#include <ecoli_node.h>
-#include <ecoli_parsed.h>
-
-TAILQ_HEAD(ec_parsed_list, ec_parsed);
-
-struct ec_parsed {
- TAILQ_ENTRY(ec_parsed) next;
- struct ec_parsed_list children;
- struct ec_parsed *parent;
- const struct ec_node *node;
- struct ec_strvec *strvec;
- struct ec_keyval *attrs;
-};
-
-static int __ec_node_parse_child(const struct ec_node *node,
- struct ec_parsed *state,
- bool is_root, const struct ec_strvec *strvec)
-{
- struct ec_strvec *match_strvec;
- struct ec_parsed *child = NULL;
- int ret;
-
- if (node->type->parse == NULL)
- return -ENOTSUP;
-
- if (!is_root) {
- child = ec_parsed(node);
- if (child == NULL)
- return -ENOMEM;
-
- ec_parsed_link_child(state, child);
- } else {
- child = state;
- }
- ret = node->type->parse(node, child, strvec);
- if (ret < 0 || ret == EC_PARSED_NOMATCH)
- goto free;
-
- match_strvec = ec_strvec_ndup(strvec, 0, ret);
- if (match_strvec == NULL) {
- ret = -ENOMEM;
- goto free;
- }
-
- child->strvec = match_strvec;
-
- return ret;
-
-free:
- if (!is_root) {
- ec_parsed_unlink_child(state, child);
- ec_parsed_free(child);
- }
- return ret;
-}
-
-int ec_node_parse_child(const struct ec_node *node, struct ec_parsed *state,
- const struct ec_strvec *strvec)
-{
- assert(state != NULL);
- return __ec_node_parse_child(node, state, false, strvec);
-}
-
-struct ec_parsed *ec_node_parse_strvec(const struct ec_node *node,
- const struct ec_strvec *strvec)
-{
- struct ec_parsed *parsed = ec_parsed(node);
- int ret;
-
- if (parsed == NULL)
- return NULL;
-
- ret = __ec_node_parse_child(node, parsed, true, strvec);
- if (ret < 0) {
- ec_parsed_free(parsed);
- return NULL;
- }
-
- return parsed;
-}
-
-struct ec_parsed *ec_node_parse(const struct ec_node *node, const char *str)
-{
- struct ec_strvec *strvec = NULL;
- struct ec_parsed *parsed = NULL;
-
- errno = ENOMEM;
- strvec = ec_strvec();
- if (strvec == NULL)
- goto fail;
-
- if (ec_strvec_add(strvec, str) < 0)
- goto fail;
-
- parsed = ec_node_parse_strvec(node, strvec);
- if (parsed == NULL)
- goto fail;
-
- ec_strvec_free(strvec);
- return parsed;
-
- fail:
- ec_strvec_free(strvec);
- ec_parsed_free(parsed);
- return NULL;
-}
-
-struct ec_parsed *ec_parsed(const struct ec_node *node)
-{
- struct ec_parsed *parsed = NULL;
-
- parsed = ec_calloc(1, sizeof(*parsed));
- if (parsed == NULL)
- goto fail;
-
- TAILQ_INIT(&parsed->children);
-
- parsed->node = node;
- parsed->attrs = ec_keyval();
- if (parsed->attrs == NULL)
- goto fail;
-
- return parsed;
-
- fail:
- if (parsed != NULL)
- ec_keyval_free(parsed->attrs);
- ec_free(parsed);
-
- return NULL;
-}
-
-static struct ec_parsed *
-__ec_parsed_dup(const struct ec_parsed *root, const struct ec_parsed *ref,
- struct ec_parsed **new_ref)
-{
- struct ec_parsed *dup = NULL;
- struct ec_parsed *child, *dup_child;
- struct ec_keyval *attrs = NULL;
-
- if (root == NULL)
- return NULL;
-
- dup = ec_parsed(root->node);
- if (dup == NULL)
- return NULL;
-
- if (root == ref)
- *new_ref = dup;
-
- attrs = ec_keyval_dup(root->attrs);
- if (attrs == NULL)
- goto fail;
- ec_keyval_free(dup->attrs);
- dup->attrs = attrs;
-
- if (root->strvec != NULL) {
- dup->strvec = ec_strvec_dup(root->strvec);
- if (dup->strvec == NULL)
- goto fail;
- }
-
- TAILQ_FOREACH(child, &root->children, next) {
- dup_child = __ec_parsed_dup(child, ref, new_ref);
- if (dup_child == NULL)
- goto fail;
- ec_parsed_link_child(dup, dup_child);
- }
-
- return dup;
-
-fail:
- ec_parsed_free(dup);
- return NULL;
-}
-
-struct ec_parsed *ec_parsed_dup(const struct ec_parsed *parsed)
-{
- const struct ec_parsed *root;
- struct ec_parsed *dup_root, *dup = NULL;
-
- root = ec_parsed_get_root(parsed);
- dup_root = __ec_parsed_dup(root, parsed, &dup);
- if (dup_root == NULL)
- return NULL;
- assert(dup != NULL);
-
- return dup;
-}
-
-void ec_parsed_free_children(struct ec_parsed *parsed)
-{
- struct ec_parsed *child;
-
- if (parsed == NULL)
- return;
-
- while (!TAILQ_EMPTY(&parsed->children)) {
- child = TAILQ_FIRST(&parsed->children);
- TAILQ_REMOVE(&parsed->children, child, next);
- child->parent = NULL;
- ec_parsed_free(child);
- }
-}
-
-void ec_parsed_free(struct ec_parsed *parsed)
-{
- if (parsed == NULL)
- return;
-
- ec_assert_print(parsed->parent == NULL,
- "parent not NULL in ec_parsed_free()");
-
- ec_parsed_free_children(parsed);
- ec_strvec_free(parsed->strvec);
- ec_keyval_free(parsed->attrs);
- ec_free(parsed);
-}
-
-static void __ec_parsed_dump(FILE *out,
- const struct ec_parsed *parsed, size_t indent)
-{
- struct ec_parsed *child;
- const struct ec_strvec *vec;
- const char *id, *typename = "none";
-
- /* node can be null when parsing is incomplete */
- if (parsed->node != NULL) {
- id = parsed->node->id;
- typename = parsed->node->type->name;
- }
-
- fprintf(out, "%*s" "type=%s id=%s vec=",
- (int)indent * 4, "", typename, id);
- vec = ec_parsed_strvec(parsed);
- ec_strvec_dump(out, vec);
-
- TAILQ_FOREACH(child, &parsed->children, next)
- __ec_parsed_dump(out, child, indent + 1);
-}
-
-void ec_parsed_dump(FILE *out, const struct ec_parsed *parsed)
-{
- fprintf(out, "------------------- parsed dump:\n");
-
- if (parsed == NULL) {
- fprintf(out, "parsed is NULL, error in parse\n");
- return;
- }
-
- /* only exist if it does not match (strvec == NULL) and if it
- * does not have children: an incomplete parse, like those
- * generated by complete() don't match but have children that
- * may match. */
- if (!ec_parsed_matches(parsed) && TAILQ_EMPTY(&parsed->children)) {
- fprintf(out, "no match\n");
- return;
- }
-
- __ec_parsed_dump(out, parsed, 0);
-}
-
-void ec_parsed_link_child(struct ec_parsed *parsed,
- struct ec_parsed *child)
-{
- TAILQ_INSERT_TAIL(&parsed->children, child, next);
- child->parent = parsed;
-}
-
-void ec_parsed_unlink_child(struct ec_parsed *parsed,
- struct ec_parsed *child)
-{
- TAILQ_REMOVE(&parsed->children, child, next);
- child->parent = NULL;
-}
-
-struct ec_parsed *
-ec_parsed_get_first_child(const struct ec_parsed *parsed)
-{
- return TAILQ_FIRST(&parsed->children);
-}
-
-struct ec_parsed *
-ec_parsed_get_last_child(const struct ec_parsed *parsed)
-{
- return TAILQ_LAST(&parsed->children, ec_parsed_list);
-}
-
-struct ec_parsed *ec_parsed_get_next(const struct ec_parsed *parsed)
-{
- return TAILQ_NEXT(parsed, next);
-}
-
-bool ec_parsed_has_child(const struct ec_parsed *parsed)
-{
- return !TAILQ_EMPTY(&parsed->children);
-}
-
-const struct ec_node *ec_parsed_get_node(const struct ec_parsed *parsed)
-{
- return parsed->node;
-}
-
-void ec_parsed_del_last_child(struct ec_parsed *parsed)
-{
- struct ec_parsed *child;
-
- child = ec_parsed_get_last_child(parsed);
- ec_parsed_unlink_child(parsed, child);
- ec_parsed_free(child);
-}
-
-struct ec_parsed *__ec_parsed_get_root(struct ec_parsed *parsed)
-{
- if (parsed == NULL)
- return NULL;
-
- while (parsed->parent != NULL)
- parsed = parsed->parent;
-
- return parsed;
-}
-
-struct ec_parsed *ec_parsed_get_parent(const struct ec_parsed *parsed)
-{
- if (parsed == NULL)
- return NULL;
-
- return parsed->parent;
-}
-
-struct ec_parsed *ec_parsed_iter_next(struct ec_parsed *parsed)
-{
- struct ec_parsed *child, *parent, *next;
-
- child = TAILQ_FIRST(&parsed->children);
- if (child != NULL)
- return child;
- parent = parsed->parent;
- while (parent != NULL) {
- next = TAILQ_NEXT(parsed, next);
- if (next != NULL)
- return next;
- parsed = parent;
- parent = parsed->parent;
- }
- return NULL;
-}
-
-struct ec_parsed *ec_parsed_find_first(struct ec_parsed *parsed,
- const char *id)
-{
- struct ec_parsed *child, *ret;
-
- if (parsed == NULL)
- return NULL;
-
- if (parsed->node != NULL &&
- parsed->node->id != NULL &&
- !strcmp(parsed->node->id, id))
- return parsed;
-
- TAILQ_FOREACH(child, &parsed->children, next) {
- ret = ec_parsed_find_first(child, id);
- if (ret != NULL)
- return ret;
- }
-
- return NULL;
-}
-
-struct ec_keyval *
-ec_parsed_get_attrs(struct ec_parsed *parsed)
-{
- if (parsed == NULL)
- return NULL;
-
- return parsed->attrs;
-}
-
-const struct ec_strvec *ec_parsed_strvec(const struct ec_parsed *parsed)
-{
- if (parsed == NULL || parsed->strvec == NULL)
- return NULL;
-
- return parsed->strvec;
-}
-
-/* number of strings in the parsed vector */
-size_t ec_parsed_len(const struct ec_parsed *parsed)
-{
- if (parsed == NULL || parsed->strvec == NULL)
- return 0;
-
- return ec_strvec_len(parsed->strvec);
-}
-
-size_t ec_parsed_matches(const struct ec_parsed *parsed)
-{
- if (parsed == NULL)
- return 0;
-
- if (parsed->strvec == NULL)
- return 0;
-
- return 1;
-}
+++ /dev/null
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
- */
-
-/**
- * Node parse API.
- *
- * The parse operation is to check if an input (a string or vector of
- * strings) matches the node tree. On success, the result is stored in a
- * tree that describes which part of the input matches which node.
- */
-
-#ifndef ECOLI_PARSED_
-#define ECOLI_PARSED_
-
-#include <sys/queue.h>
-#include <sys/types.h>
-#include <limits.h>
-#include <stdio.h>
-
-struct ec_node;
-struct ec_parsed;
-
-/**
- * Create an empty parse tree.
- *
- * @return
- * The empty parse tree.
- */
-struct ec_parsed *ec_parsed(const struct ec_node *node);
-
-/**
- *
- *
- *
- */
-void ec_parsed_free(struct ec_parsed *parsed);
-
-/**
- *
- *
- *
- */
-void ec_parsed_free_children(struct ec_parsed *parsed);
-
-/**
- *
- *
- *
- */
-struct ec_parsed *ec_parsed_dup(const struct ec_parsed *parsed);
-
-/**
- *
- *
- *
- */
-const struct ec_strvec *ec_parsed_strvec(const struct ec_parsed *parsed);
-
-/* a NULL return value is an error, with errno set
- ENOTSUP: no ->parse() operation
-*/
-/**
- *
- *
- *
- */
-struct ec_parsed *ec_node_parse(const struct ec_node *node, const char *str);
-
-/**
- *
- *
- *
- */
-struct ec_parsed *ec_node_parse_strvec(const struct ec_node *node,
- const struct ec_strvec *strvec);
-
-/**
- *
- *
- *
- */
-#define EC_PARSED_NOMATCH INT_MAX
-
-/* internal: used by nodes
- *
- * state is the current parse tree, which is built piece by piece while
- * parsing the node tree: ec_node_parse_child() creates a new child in
- * this state parse tree, and calls the parse() method for the child
- * node, with state pointing to this new child. If it does not match,
- * the child is removed in the state, else it is kept, with its
- * possible descendants.
- *
- * return:
- * EC_PARSED_NOMATCH (positive) if it does not match
- * any other negative value (-errno) for other errors
- * the number of matched strings in strvec
- */
-int ec_node_parse_child(const struct ec_node *node,
- struct ec_parsed *state,
- const struct ec_strvec *strvec);
-
-/**
- *
- *
- *
- */
-void ec_parsed_link_child(struct ec_parsed *parsed,
- struct ec_parsed *child);
-/**
- *
- *
- *
- */
-void ec_parsed_unlink_child(struct ec_parsed *parsed,
- struct ec_parsed *child);
-
-/* keep the const */
-#define ec_parsed_get_root(parsed) ({ \
- const struct ec_parsed *p_ = parsed; /* check type */ \
- struct ec_parsed *parsed_ = (struct ec_parsed *)parsed; \
- typeof(parsed) res_; \
- (void)p_; \
- res_ = __ec_parsed_get_root(parsed_); \
- res_; \
-})
-
-/**
- *
- *
- *
- */
-struct ec_parsed *__ec_parsed_get_root(struct ec_parsed *parsed);
-
-/**
- *
- *
- *
- */
-struct ec_parsed *ec_parsed_get_parent(const struct ec_parsed *parsed);
-
-/**
- * Get the first child of a tree.
- *
- */
-struct ec_parsed *ec_parsed_get_first_child(const struct ec_parsed *parsed);
-
-/**
- *
- *
- *
- */
-struct ec_parsed *ec_parsed_get_last_child(const struct ec_parsed *parsed);
-
-/**
- *
- *
- *
- */
-struct ec_parsed *ec_parsed_get_next(const struct ec_parsed *parsed);
-
-/**
- *
- *
- *
- */
-#define EC_PARSED_FOREACH_CHILD(child, parsed) \
- for (child = ec_parsed_get_first_child(parsed); \
- child != NULL; \
- child = ec_parsed_get_next(child)) \
-
-/**
- *
- *
- *
- */
-bool ec_parsed_has_child(const struct ec_parsed *parsed);
-
-/**
- *
- *
- *
- */
-const struct ec_node *ec_parsed_get_node(const struct ec_parsed *parsed);
-
-/**
- *
- *
- *
- */
-void ec_parsed_del_last_child(struct ec_parsed *parsed);
-
-/**
- *
- *
- *
- */
-struct ec_keyval *ec_parsed_get_attrs(struct ec_parsed *parsed);
-
-/**
- *
- *
- *
- */
-void ec_parsed_dump(FILE *out, const struct ec_parsed *parsed);
-
-/**
- *
- *
- *
- */
-struct ec_parsed *ec_parsed_find_first(struct ec_parsed *parsed,
- const char *id);
-
-/**
- * Iterate among parsed tree
- *
- * Use it with:
- * for (iter = state; iter != NULL; iter = ec_parsed_iter_next(iter))
- */
-struct ec_parsed *ec_parsed_iter_next(struct ec_parsed *parsed);
-
-/**
- *
- *
- *
- */
-size_t ec_parsed_len(const struct ec_parsed *parsed);
-
-/**
- *
- *
- *
- */
-size_t ec_parsed_matches(const struct ec_parsed *parsed);
-
-#endif
#include <ecoli_test.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
static struct ec_test_list test_list = TAILQ_HEAD_INITIALIZER(test_list);
int ec_test_check_parse(struct ec_node *tk, int expected, ...)
{
- struct ec_parsed *p;
+ struct ec_parse *p;
struct ec_strvec *vec = NULL;
const char *s;
int ret = -1, match;
p = ec_node_parse_strvec(tk, vec);
if (p == NULL) {
- EC_LOG(EC_LOG_ERR, "parsed is NULL\n");
+ EC_LOG(EC_LOG_ERR, "parse is NULL\n");
}
- if (ec_parsed_matches(p))
- match = ec_parsed_len(p);
+ if (ec_parse_matches(p))
+ match = ec_parse_len(p);
else
match = -1;
if (expected == match) {
ret = 0;
} else {
EC_LOG(EC_LOG_ERR,
- "tk parsed len (%d) does not match expected (%d)\n",
+ "parse len (%d) does not match expected (%d)\n",
match, expected);
}
- ec_parsed_free(p);
+ ec_parse_free(p);
out:
ec_strvec_free(vec);
#include <ecoli_init.h>
#include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
#include <ecoli_complete.h>
#include <ecoli_keyval.h>
#include <ecoli_node_str.h>
static char *get_node_help(const struct ec_comp_item *item)
{
const struct ec_comp_group *grp;
- const struct ec_parsed *state;
+ const struct ec_parse *state;
const struct ec_node *node;
char *help = NULL;
const char *node_help = NULL;
grp = ec_comp_item_get_grp(item);
state = grp->state;
for (state = grp->state; state != NULL;
- state = ec_parsed_get_parent(state)) {
- node = ec_parsed_get_node(state);
+ state = ec_parse_get_parent(state)) {
+ node = ec_parse_get_node(state);
if (node_help == NULL)
node_help = ec_keyval_get(ec_node_attrs(node), "help");
if (node_desc == NULL)
const struct ec_comp_group *grp, *prev_grp = NULL;
const struct ec_comp_item *item;
struct ec_comp *c;
- struct ec_parsed *p;
+ struct ec_parse *p;
char *line = NULL;
unsigned int count;
char **helps = NULL;
/* check if the current line matches */
p = ec_node_parse(commands, line);
- if (ec_parsed_matches(p))
+ if (ec_parse_matches(p))
match = 1;
- ec_parsed_free(p);
+ ec_parse_free(p);
p = NULL;
/* complete at current cursor position */
fail:
ec_comp_iter_free(iter);
- ec_parsed_free(p);
+ ec_parse_free(p);
free(line);
ec_comp_free(c);
if (helps != NULL) {
int main(void)
{
- struct ec_parsed *p;
+ struct ec_parse *p;
char *line;
if (ec_init() < 0) {
break;
p = ec_node_parse(commands, line);
- ec_parsed_dump(stdout, p);
+ ec_parse_dump(stdout, p);
add_history(line);
- ec_parsed_free(p);
+ ec_parse_free(p);
}