O ?= build/
# XXX -O0
-CFLAGS = -g -O0 -Wall -Werror -W -Wextra -fPIC -Wmissing-prototypes
+CFLAGS = -g -O3 -Wall -Werror -W -Wextra -fPIC -Wmissing-prototypes
CFLAGS += -I.
# XXX coverage
/**
* Assert API
*
- * Helpers to check at runtime if a condition is true, and abort
- * (exit) otherwise.
+ * Helpers to check at runtime if a condition is true, or otherwise
+ * either abort (exit program) or return an error.
*/
#ifndef ECOLI_ASSERT_
void __ec_assert_print(bool expr, const char *expr_str,
const char *format, ...);
+/**
+ * Check a condition or return.
+ *
+ * If the condition is true, do nothing. If it is false, set
+ * errno and return the specified value.
+ *
+ * @param cond
+ * The condition to test.
+ * @param ret
+ * The value to return.
+ * @param err
+ * The errno to set.
+ */
+#define EC_CHECK_ARG(cond, ret, err) do { \
+ if (!(cond)) { \
+ errno = err; \
+ return ret; \
+ } \
+ } while(0)
+
#endif
struct ec_comp_group *cur_group;
int ret;
- if (ec_node_type(node)->complete == NULL)
- return -ENOTSUP;
+ if (ec_node_type(node)->complete == NULL) {
+ errno = ENOTSUP;
+ return -1;
+ }
/* save previous parse state, prepare child state */
cur_state = comp->cur_state;
child_state = ec_parse(node);
if (child_state == NULL)
- return -ENOMEM;
+ return -1;
if (cur_state != NULL)
ec_parse_link_child(cur_state, child_state);
comp->cur_group = cur_group;
if (ret < 0)
- return ret;
+ return -1;
return 0;
}
const char *display)
{
char *display_copy = NULL;
- int ret = 0;
if (item == NULL || display == NULL ||
- item->type == EC_COMP_UNKNOWN)
- return -EINVAL;
+ item->type == EC_COMP_UNKNOWN) {
+ errno = EINVAL;
+ return -1;
+ }
display_copy = ec_strdup(display);
if (display_copy == NULL)
fail:
ec_free(display_copy);
- return ret;
+ return -1;
}
int
const char *completion)
{
char *completion_copy = NULL;
- int ret = 0;
if (item == NULL || completion == NULL ||
- item->type == EC_COMP_UNKNOWN)
- return -EINVAL;
+ item->type == EC_COMP_UNKNOWN) {
+ errno = EINVAL;
+ return -1;
+ }
- ret = -ENOMEM;
completion_copy = ec_strdup(completion);
if (completion_copy == NULL)
goto fail;
fail:
ec_free(completion_copy);
- return ret;
+ return -1;
}
int
const char *str)
{
char *str_copy = NULL;
- int ret = 0;
if (item == NULL || str == NULL ||
- item->type == EC_COMP_UNKNOWN)
- return -EINVAL;
+ item->type == EC_COMP_UNKNOWN) {
+ errno = EINVAL;
+ return -1;
+ }
- ret = -ENOMEM;
str_copy = ec_strdup(str);
if (str_copy == NULL)
goto fail;
fail:
ec_free(str_copy);
- return ret;
+ return -1;
}
static int
ec_comp_item_add(struct ec_comp *comp, const struct ec_node *node,
struct ec_comp_item *item)
{
- if (comp == NULL || item == NULL)
- return -EINVAL;
+ if (comp == NULL || item == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
switch (item->type) {
case EC_COMP_UNKNOWN:
comp->count_partial++;
break;
default:
- return -EINVAL;
+ errno = EINVAL;
+ return -1;
}
if (comp->cur_group == NULL) {
grp = ec_comp_group(node, comp->cur_state);
if (grp == NULL)
- return -ENOMEM;
+ return -1;
TAILQ_INSERT_TAIL(&comp->groups, grp, next);
comp->cur_group = grp;
}
return type;
}
+ errno = ENOENT;
return NULL;
}
int ec_node_type_register(struct ec_node_type *type)
{
- if (ec_node_type_lookup(type->name) != NULL)
- return -EEXIST;
- if (type->size < sizeof(struct ec_node))
- return -EINVAL;
+ EC_CHECK_ARG(type->size >= sizeof(struct ec_node), -1, EINVAL);
+
+ if (ec_node_type_lookup(type->name) != NULL) {
+ errno = EEXIST;
+ return -1;
+ }
TAILQ_INSERT_TAIL(&node_type_list, type, next);
fail:
ec_free(children);
+ assert(errno != 0);
return -1;
}
errno = EINVAL;
return -1;
}
+
return 0;
}
fclose(f);
free(buf);
+ assert(errno != 0);
return -1;
}
/* LCOV_EXCL_STOP */
/* get parsed string vector, it should contain only one str */
vec = ec_parse_strvec(var);
- if (ec_strvec_len(vec) != 1)
- return -EINVAL;
+ if (ec_strvec_len(vec) != 1) {
+ errno = EINVAL;
+ return -1;
+ }
str = ec_strvec_val(vec, 0);
for (i = 0; i < node->len; i++) {
/* 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;
}
if (eval == NULL) {
eval = ec_node_str(EC_NO_ID, str);
if (eval == NULL)
- return -ENOMEM;
+ return -1;
}
*result = eval;
(void)operand;
(void)operator;
- return -EINVAL;
+ errno = EINVAL;
+ return -1;
}
static int
/* get parsed string vector, it should contain only one str */
vec = ec_parse_strvec(operator);
- if (ec_strvec_len(vec) != 1)
- return -EINVAL;
+ 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;
/* get parsed string vector, it should contain only one str */
vec = ec_parse_strvec(operator);
- if (ec_strvec_len(vec) > 1)
- return -EINVAL;
+ 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;
} 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;
} 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;
/* get parsed string vector, it should contain only one str */
vec = ec_parse_strvec(open_paren);
- if (ec_strvec_len(vec) != 1)
- return -EINVAL;
+ 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;
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, ","));
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;
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_parse_matches(p))
+ if (!ec_parse_matches(p)) {
+ errno = EINVAL;
goto fail;
- if (!ec_parse_has_child(p))
+ }
+ if (!ec_parse_has_child(p)) {
+ errno = EINVAL;
goto fail;
+ }
ret = ec_node_expr_eval(&result, expr, ec_parse_get_first_child(p),
&test_ops, node);
ec_node_free(expr);
ec_node_free(lex);
ec_node_free(cmd);
- return ret;
+
+ return -1;
}
static int
{
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);
}
{
struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
- if (node->cmd == NULL)
- return -ENOENT;
return ec_node_complete_child(node->cmd, comp, strvec);
}
{
struct ec_node_expr *node = (struct ec_node_expr *)gen_node;
- if (node->child == NULL)
- return -ENOENT;
+ if (node->child == NULL) {
+ errno = ENOENT;
+ return -1;
+ }
+
return ec_node_parse_child(node->child, state, strvec);
}
{
struct ec_node_expr *node = (struct ec_node_expr *)gen_node;
- if (node->child == NULL)
- return -ENOENT;
+ if (node->child == NULL) {
+ errno = ENOENT;
+ return -1;
+ }
+
return ec_node_complete_child(node->child, comp, strvec);
}
*pre_op = NULL, *post_op = NULL,
*post = NULL, *weak = NULL;
unsigned int i;
- int ret;
ec_node_free(node->child);
node->child = NULL;
- if (node->val_node == NULL)
- return -EINVAL;
+ if (node->val_node == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
if (node->bin_ops_len == 0 && node->pre_ops_len == 0 &&
- node->post_ops_len == 0)
- return -EINVAL;
+ node->post_ops_len == 0) {
+ errno = EINVAL;
+ return -1;
+ }
/*
* Example of created grammar:
/* create the object, we will initialize it later: this is
* needed because we have a circular dependency */
- ret = -ENOMEM;
weak = ec_node("weakref", "weak");
if (weak == NULL)
return -1;
ec_node_free(post);
ec_node_free(weak);
- return ret;
+ return -1;
}
static struct ec_node_type ec_node_expr_type = {
int ec_node_expr_set_val_node(struct ec_node *gen_node, struct ec_node *val_node)
{
struct ec_node_expr *node = (struct ec_node_expr *)gen_node;
- int ret;
- ret = ec_node_check_type(gen_node, &ec_node_expr_type);
- if (ret < 0)
- return ret;
+ if (ec_node_check_type(gen_node, &ec_node_expr_type) < 0)
+ goto fail;
- ret = -EINVAL;
- if (val_node == NULL)
+ if (val_node == NULL) {
+ errno = EINVAL;
goto fail;
+ }
ec_node_free(node->val_node);
node->val_node = val_node;
fail:
ec_node_free(val_node);
- return ret;
+ return -1;
}
/* add a binary operator */
{
struct ec_node_expr *node = (struct ec_node_expr *)gen_node;
struct ec_node **bin_ops;
- int ret;
- ret = ec_node_check_type(gen_node, &ec_node_expr_type);
- if (ret < 0)
- return ret;
+ if (ec_node_check_type(gen_node, &ec_node_expr_type) < 0)
+ goto fail;
- ret = -EINVAL;
- if (node == NULL || op == NULL)
+ if (node == NULL || op == NULL) {
+ errno = EINVAL;
goto fail;
+ }
- ret = -ENOMEM;
bin_ops = ec_realloc(node->bin_ops,
(node->bin_ops_len + 1) * sizeof(*node->bin_ops));
if (bin_ops == NULL)
fail:
ec_node_free(op);
- return ret;
+ return -1;
}
/* add a unary pre-operator */
{
struct ec_node_expr *node = (struct ec_node_expr *)gen_node;
struct ec_node **pre_ops;
- int ret;
- ret = ec_node_check_type(gen_node, &ec_node_expr_type);
- if (ret < 0)
- return ret;
+ if (ec_node_check_type(gen_node, &ec_node_expr_type) < 0)
+ goto fail;
- ret = -EINVAL;
- if (node == NULL || op == NULL)
+ if (node == NULL || op == NULL) {
+ errno = EINVAL;
goto fail;
+ }
- ret = -ENOMEM;
pre_ops = ec_realloc(node->pre_ops,
(node->pre_ops_len + 1) * sizeof(*node->pre_ops));
if (pre_ops == NULL)
fail:
ec_node_free(op);
- return ret;
+ return -1;
}
/* add a unary post-operator */
{
struct ec_node_expr *node = (struct ec_node_expr *)gen_node;
struct ec_node **post_ops;
- int ret;
- ret = ec_node_check_type(gen_node, &ec_node_expr_type);
- if (ret < 0)
- return ret;
+ if (ec_node_check_type(gen_node, &ec_node_expr_type) < 0)
+ goto fail;
- ret = -EINVAL;
- if (node == NULL || op == NULL)
+ if (node == NULL || op == NULL) {
+ errno = EINVAL;
goto fail;
+ }
- ret = -ENOMEM;
post_ops = ec_realloc(node->post_ops,
(node->post_ops_len + 1) * sizeof(*node->post_ops));
if (post_ops == NULL)
fail:
ec_node_free(op);
- return ret;
+ return -1;
}
/* add parenthesis symbols */
{
struct ec_node_expr *node = (struct ec_node_expr *)gen_node;
struct ec_node **open_ops, **close_ops;
- int ret;
- ret = ec_node_check_type(gen_node, &ec_node_expr_type);
- if (ret < 0)
- return ret;
+ if (ec_node_check_type(gen_node, &ec_node_expr_type) < 0)
+ goto fail;
- ret = -EINVAL;
- if (node == NULL || open == NULL || close == NULL)
+ if (node == NULL || open == NULL || close == NULL) {
+ errno = EINVAL;
goto fail;
+ }
- ret = -ENOMEM;
open_ops = ec_realloc(node->open_ops,
(node->paren_len + 1) * sizeof(*node->open_ops));
if (open_ops == NULL)
fail:
ec_node_free(open);
ec_node_free(close);
- return ret;
+ return -1;
}
enum expr_node_type {
const struct ec_node_expr_eval_ops *ops,
struct result *x, const struct result *y)
{
- int ret;
-
if (y->has_val == 0 && y->op == NULL)
return 0;
if (x->has_val == 0 && x->op == NULL) {
if (x->has_val && y->has_val && y->op != NULL) {
if (y->op_type == BIN_OP) {
- ret = ops->eval_bin_op(&x->val, userctx, x->val,
- y->op, y->val);
- if (ret < 0)
- return ret;
+ if (ops->eval_bin_op(&x->val, userctx, x->val,
+ y->op, y->val) < 0)
+ return -1;
return 0;
}
if (x->has_val == 0 && x->op != NULL && y->has_val && y->op == NULL) {
if (x->op_type == PRE_OP) {
- ret = ops->eval_pre_op(&x->val, userctx, y->val, x->op);
- if (ret < 0)
- return ret;
+ if (ops->eval_pre_op(&x->val, userctx, y->val,
+ x->op) < 0)
+ return -1;
x->has_val = true;
x->op_type = NONE;
x->op = NULL;
}
if (x->has_val && x->op == NULL && y->has_val == 0 && y->op != NULL) {
- ret = ops->eval_post_op(&x->val, userctx, x->val, y->op);
- if (ret < 0)
- return ret;
+ if (ops->eval_post_op(&x->val, userctx, x->val, y->op) < 0)
+ return -1;
return 0;
}
assert(false); /* we should not get here */
- return -EINVAL;
+ return -1;
}
static int eval_expression(struct result *result,
struct result child_result;
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_parse_get_node(parse));
if (type == VAL) {
- ret = ops->eval_var(&result->val, userctx, parse);
- if (ret < 0)
+ if (ops->eval_var(&result->val, userctx, parse) < 0)
goto fail;
result->has_val = 1;
} else if (type == PRE_OP || type == POST_OP || type == BIN_OP) {
continue;
}
- ret = eval_expression(&child_result, userctx, ops,
- expr_gen_node, child);
- if (ret < 0)
+ if (eval_expression(&child_result, userctx, ops,
+ expr_gen_node, child) < 0)
goto fail;
- ret = merge_results(userctx, ops, result, &child_result);
- if (ret < 0)
+ if (merge_results(userctx, ops, result, &child_result) < 0)
goto fail;
memset(&child_result, 0, sizeof(child_result));
}
if (open != NULL && close != NULL) {
- ret = ops->eval_parenthesis(&result->val, userctx, open, close,
- result->val);
- if (ret < 0)
+ if (ops->eval_parenthesis(&result->val, userctx, open, close,
+ result->val) < 0)
goto fail;
}
ops->eval_free(child_result.val, userctx);
memset(result, 0, sizeof(*result));
- return ret;
+ return -1;
}
int ec_node_expr_eval(void **user_result, const struct ec_node *node,
void *userctx)
{
struct result result;
- int ret;
if (ops == NULL || ops->eval_var == NULL || ops->eval_pre_op == NULL ||
ops->eval_post_op == NULL || ops->eval_bin_op == NULL ||
- ops->eval_parenthesis == NULL || ops->eval_free == NULL)
- return -EINVAL;
+ ops->eval_parenthesis == NULL ||
+ ops->eval_free == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
- ret = ec_node_check_type(node, &ec_node_expr_type);
- if (ret < 0)
- return ret;
+ if (ec_node_check_type(node, &ec_node_expr_type) < 0)
+ return -1;
- if (!ec_parse_matches(parse))
- return -EINVAL;
+ if (!ec_parse_matches(parse)) {
+ errno = EINVAL;
+ return -1;
+ }
- ret = eval_expression(&result, userctx, ops, node, parse);
- if (ret < 0)
- return ret;
+ if (eval_expression(&result, userctx, ops, node, parse) < 0)
+ return -1;
assert(result.has_val);
assert(result.op == NULL);
/* get parsed string vector, it should contain only one str */
vec = ec_parse_strvec(var);
- if (ec_strvec_len(vec) != 1)
- return -EINVAL;
+ if (ec_strvec_len(vec) != 1) {
+ errno = EINVAL;
+ return -1;
+ }
node = ec_parse_get_node(var);
if (ec_node_int_getval(node, ec_strvec_val(vec, 0), &val) < 0)
- return -EINVAL;
+ return -1;
eval = ec_malloc(sizeof(*eval));
if (eval == NULL)
- return -ENOMEM;
+ return -1;
eval->val = val;
EC_LOG(EC_LOG_DEBUG, "eval var %d\n", eval->val);
/* get parsed string vector, it should contain only one str */
vec = ec_parse_strvec(operator);
- if (ec_strvec_len(vec) != 1)
- return -EINVAL;
+ if (ec_strvec_len(vec) != 1) {
+ errno = EINVAL;
+ return -1;
+ }
- if (!strcmp(ec_strvec_val(vec, 0), "!"))
+ if (!strcmp(ec_strvec_val(vec, 0), "!")) {
eval->val = !eval->val;
- else
- return -EINVAL;
+ } else {
+ errno = EINVAL;
+ return -1;
+ }
+
EC_LOG(EC_LOG_DEBUG, "eval pre_op %d\n", eval->val);
*result = eval;
/* get parsed string vector, it should contain only one str */
vec = ec_parse_strvec(operator);
- if (ec_strvec_len(vec) != 1)
- return -EINVAL;
+ if (ec_strvec_len(vec) != 1) {
+ errno = EINVAL;
+ return -1;
+ }
- if (!strcmp(ec_strvec_val(vec, 0), "^"))
+ if (!strcmp(ec_strvec_val(vec, 0), "^")) {
eval->val = eval->val * eval->val;
- else
- return -EINVAL;
+ } else {
+ errno = EINVAL;
+ return -1;
+ }
EC_LOG(EC_LOG_DEBUG, "eval post_op %d\n", eval->val);
*result = eval;
/* get parsed string vector, it should contain only one str */
vec = ec_parse_strvec(operator);
- if (ec_strvec_len(vec) != 1)
- return -EINVAL;
+ if (ec_strvec_len(vec) != 1) {
+ errno = EINVAL;
+ return -1;
+ }
- if (!strcmp(ec_strvec_val(vec, 0), "+"))
+ if (!strcmp(ec_strvec_val(vec, 0), "+")) {
eval1->val = eval1->val + eval2->val;
- else if (!strcmp(ec_strvec_val(vec, 0), "*"))
+ } else if (!strcmp(ec_strvec_val(vec, 0), "*")) {
eval1->val = eval1->val * eval2->val;
- else
- return -EINVAL;
+ } else {
+ errno = EINVAL;
+ return -1;
+ }
EC_LOG(EC_LOG_DEBUG, "eval bin_op %d\n", eval1->val);
ec_free(eval2);
(errno != 0 && *val == 0))
return -1;
- if (node->check_min && *val < node->min)
+ if (node->check_min && *val < node->min) {
+ errno = ERANGE;
return -1;
+ }
- if (node->check_max && *val > node->max)
+ if (node->check_max && *val > node->max) {
+ errno = ERANGE;
return -1;
+ }
- if (*endptr != 0)
+ if (*endptr != 0) {
+ errno = EINVAL;
return -1;
+ }
return 0;
}
for (count = 0; node->max == 0 || count < node->max; count++) {
childvec = ec_strvec_ndup(strvec, off,
ec_strvec_len(strvec) - off);
- if (childvec == NULL) {
- ret = -ENOMEM;
+ if (childvec == NULL)
goto fail;
- }
ret = ec_node_parse_child(node->child, state, childvec);
if (ret < 0)
fail:
ec_strvec_free(childvec);
- return ret;
+ return -1;
}
static int
regex_t re;
int ret;
- if (str == NULL)
- return -EINVAL;
+ EC_CHECK_ARG(str != NULL, -1, EINVAL);
- ret = -ENOMEM;
str_copy = ec_strdup(str);
if (str_copy == NULL)
goto fail;
- ret = -EINVAL;
- if (regcomp(&re, str_copy, REG_EXTENDED) != 0)
+ ret = regcomp(&re, str_copy, REG_EXTENDED);
+ if (ret != 0) {
+ if (ret == REG_ESPACE)
+ errno = ENOMEM;
+ else
+ errno = EINVAL;
goto fail;
+ }
if (node->re_str != NULL) {
ec_free(node->re_str);
fail:
ec_free(str_copy);
- return ret;
+ return -1;
}
struct ec_node *ec_node_re(const char *id, const char *re_str)
str = ec_strvec_val(strvec, 0);
new_vec = tokenize(node->table, node->len, str);
}
- if (new_vec == NULL) {
- ret = -ENOMEM;
+ if (new_vec == NULL)
goto fail;
- }
ret = ec_node_parse_child(node->child, state, new_vec);
if (ret < 0)
fail:
ec_strvec_free(new_vec);
- return ret;
+ return -1;
}
static void ec_node_re_lex_free_priv(struct ec_node *gen_node)
int ret;
char *pat_dup = NULL;
- ret = -ENOMEM;
pat_dup = ec_strdup(pattern);
if (pat_dup == NULL)
goto fail;
- ret = -ENOMEM;
table = ec_realloc(node->table, sizeof(*table) * (node->len + 1));
if (table == NULL)
goto fail;
"Regular expression <%s> compilation failed: %d\n",
pattern, ret);
if (ret == REG_ESPACE)
- ret = -ENOMEM;
+ errno = ENOMEM;
else
- ret = -EINVAL;
+ errno = EINVAL;
goto fail;
}
fail:
ec_free(pat_dup);
- return ret;
+ return -1;
}
struct ec_node *ec_node_re_lex(const char *id, struct ec_node *child)
for (i = 0; i < node->len; i++) {
childvec = ec_strvec_ndup(strvec, len,
ec_strvec_len(strvec) - len);
- if (childvec == NULL) {
- ret = -ENOMEM;
+ if (childvec == NULL)
goto fail;
- }
ret = ec_node_parse_child(node->table[i], state, childvec);
if (ret < 0)
fail:
ec_strvec_free(childvec);
- return ret;
+ return -1;
}
static int
*missing_quote = str[0];
if (allow_missing_quote == 0) {
ec_free(dst);
- errno = EINVAL;
+ errno = EBADMSG;
return NULL;
}
}
str = ec_strvec_val(strvec, 0);
new_vec = tokenize(str, 0, 0, NULL);
}
- if (new_vec == NULL) {
- if (errno == EINVAL)
- ret = EC_PARSE_NOMATCH;
- else
- ret = -ENOMEM;
+ if (new_vec == NULL && errno == EBADMSG) /* quotes not closed */
+ return EC_PARSE_NOMATCH;
+ if (new_vec == NULL)
goto fail;
- }
ret = ec_node_parse_child(node->child, state, new_vec);
if (ret < 0)
fail:
ec_strvec_free(new_vec);
- return ret;
+ return -1;
}
static int
int ec_node_str_set_str(struct ec_node *gen_node, const char *str)
{
struct ec_node_str *node = (struct ec_node_str *)gen_node;
+ char *s = NULL;
int ret;
ret = ec_node_check_type(gen_node, &ec_node_str_type);
if (ret < 0)
return ret;
- if (str == NULL)
- return -EINVAL;
- ec_free(node->string);
- node->string = ec_strdup(str);
- if (node->string == NULL)
- return -ENOMEM;
+ if (str == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+ s = ec_strdup(str);
+ if (s == NULL)
+ return -1;
+
+ ec_free(node->string);
+ node->string = s;
node->len = strlen(node->string);
return 0;
memset(&best_result, 0, sizeof(best_result));
child_table = ec_calloc(table_len - 1, sizeof(*child_table));
- if (child_table == NULL) {
- ret = -ENOMEM;
+ if (child_table == NULL)
goto fail;
- }
for (i = 0; i < table_len; i++) {
/* try to parse elt i */
len = ret;
childvec = ec_strvec_ndup(strvec, len,
ec_strvec_len(strvec) - len);
- if (childvec == NULL) {
- ret = -ENOMEM;
+ if (childvec == NULL)
goto fail;
- }
memset(&result, 0, sizeof(result));
ret = __ec_node_subset_parse(&result, child_table,
ec_parse_free(best_parse);
ec_strvec_free(childvec);
ec_free(child_table);
- return ret;
+ return -1;
}
static int
struct ec_parse *child = NULL;
int ret;
- if (ec_node_type(node)->parse == NULL)
- return -ENOTSUP;
+ if (ec_node_type(node)->parse == NULL) {
+ errno = ENOTSUP;
+ return -1;
+ }
if (!is_root) {
child = ec_parse(node);
if (child == NULL)
- return -ENOMEM;
+ return -1;
ec_parse_link_child(state, child);
} else {
child = state;
}
ret = ec_node_type(node)->parse(node, child, strvec);
- if (ret < 0 || ret == EC_PARSE_NOMATCH)
- goto free;
+ if (ret < 0)
+ goto fail;
- match_strvec = ec_strvec_ndup(strvec, 0, ret);
- if (match_strvec == NULL) {
- ret = -ENOMEM;
- goto free;
+ if (ret == EC_PARSE_NOMATCH) {
+ if (!is_root) {
+ ec_parse_unlink_child(state, child);
+ ec_parse_free(child);
+ }
+ return ret;
}
+ match_strvec = ec_strvec_ndup(strvec, 0, ret);
+ if (match_strvec == NULL)
+ goto fail;
+
child->strvec = match_strvec;
return ret;
-free:
+fail:
if (!is_root) {
ec_parse_unlink_child(state, child);
ec_parse_free(child);
}
- return ret;
+ return -1;
}
int ec_node_parse_child(const struct ec_node *node, struct ec_parse *state,
{
struct ec_parse *child;
const struct ec_strvec *vec;
- const char *id, *typename = "none";
+ const char *id = "none", *typename = "none";
/* node can be null when parsing is incomplete */
if (parse->node != NULL) {
* possible descendants.
*
* return:
+ * the number of matched strings in strvec on success
* EC_PARSE_NOMATCH (positive) if it does not match
- * any other negative value (-errno) for other errors
- * the number of matched strings in strvec
+ * -1 on error, and errno is set
*/
int ec_node_parse_child(const struct ec_node *node,
struct ec_parse *state,
new_vec = ec_realloc(strvec->vec,
sizeof(*strvec->vec) * (strvec->len + 1));
if (new_vec == NULL)
- return -ENOMEM;
+ return -1;
strvec->vec = new_vec;
elt = ec_malloc(sizeof(*elt));
if (elt == NULL)
- return -ENOMEM;
+ return -1;
elt->str = ec_strdup(s);
if (elt->str == NULL) {
ec_free(elt);
- return -ENOMEM;
+ return -1;
}
elt->refcnt = 1;
size_t i;
strvec = ec_strvec();
+ if (strvec == NULL)
+ goto fail;
+
for (i = 0; i < n; i++) {
if (ec_strvec_add(strvec, strarr[i]) < 0)
goto fail;
free(buf);
buf = NULL;
+ ec_strvec_del_last(strvec);
+ strvec2 = EC_STRVEC("0");
+ if (strvec2 == NULL) {
+ EC_TEST_ERR("cannot create strvec from array\n");
+ goto fail;
+ }
+ testres |= EC_TEST_CHECK(ec_strvec_cmp(strvec, strvec2) == 0,
+ "strvec and strvec2 should be equal\n");
+ ec_strvec_free(strvec2);
+
f = open_memstream(&buf, &buflen);
if (f == NULL)
goto fail;
#include <errno.h>
#include <assert.h>
+#include <ecoli_assert.h>
#include <ecoli_malloc.h>
#include <ecoli_log.h>
#include <ecoli_test.h>
if (vec->len + 1 > vec->size) {
new_vec = ec_realloc(vec->vec, vec->elt_size * (vec->len + 1));
if (new_vec == NULL)
- return -ENOMEM;
+ return -1;
vec->size = vec->len + 1;
+ vec->vec = new_vec;
}
- vec->vec = new_vec;
memcpy(get_obj(vec, vec->len), ptr, vec->elt_size);
vec->len++;
int ec_vec_add_ptr(struct ec_vec *vec, void *elt)
{
- if (vec->elt_size != sizeof(elt))
- return -EINVAL;
+ EC_CHECK_ARG(vec->elt_size == sizeof(elt), -1, EINVAL);
return ec_vec_add_by_ref(vec, &elt);
}
int ec_vec_add_u8(struct ec_vec *vec, uint8_t elt)
{
- if (vec->elt_size != sizeof(elt))
- return -EINVAL;
+ EC_CHECK_ARG(vec->elt_size == sizeof(elt), -1, EINVAL);
return ec_vec_add_by_ref(vec, &elt);
}
int ec_vec_add_u16(struct ec_vec *vec, uint16_t elt)
{
- if (vec->elt_size != sizeof(elt))
- return -EINVAL;
+ EC_CHECK_ARG(vec->elt_size == sizeof(elt), -1, EINVAL);
return ec_vec_add_by_ref(vec, &elt);
}
int ec_vec_add_u32(struct ec_vec *vec, uint32_t elt)
{
- if (vec->elt_size != sizeof(elt))
- return -EINVAL;
+ EC_CHECK_ARG(vec->elt_size == sizeof(elt), -1, EINVAL);
return ec_vec_add_by_ref(vec, &elt);
}
int ec_vec_add_u64(struct ec_vec *vec, uint64_t elt)
{
- if (vec->elt_size != sizeof(elt))
- return -EINVAL;
+ EC_CHECK_ARG(vec->elt_size == sizeof(elt), -1, EINVAL);
return ec_vec_add_by_ref(vec, &elt);
}
static int show_help(int ignore, int invoking_key)
{
- struct ec_comp_iter *iter;
+ struct ec_comp_iter *iter = NULL;
const struct ec_comp_group *grp, *prev_grp = NULL;
const struct ec_comp_item *item;
- struct ec_comp *c;
+ struct ec_comp *c = NULL;
struct ec_parse *p;
char *line = NULL;
unsigned int count;
- anything better than weakref?
- add ec_node_defaults.[ch] providing usual implementations of node methods
X use vec for strvec
+- ELOOP in case of loop
+- remove weakref?
dependencies
============