del -> unlink + const macro
authorOlivier Matz <zer0@droids-corp.org>
Mon, 12 Mar 2018 20:31:57 +0000 (21:31 +0100)
committerOlivier Matz <zer0@droids-corp.org>
Mon, 12 Mar 2018 20:31:57 +0000 (21:31 +0100)
lib/ecoli_completed.c
lib/ecoli_node.h
lib/ecoli_node_expr.h
lib/ecoli_node_many.c
lib/ecoli_node_re_lex.c
lib/ecoli_node_sh_lex.c
lib/ecoli_node_subset.c
lib/ecoli_parsed.c
lib/ecoli_parsed.h
lib/main-readline.c

index 179ae45..a1dbe16 100644 (file)
@@ -79,7 +79,7 @@ ec_node_complete_child(const struct ec_node *node,
                return -ENOMEM;
 
        if (cur_state != NULL)
-               ec_parsed_add_child(cur_state, child_state);
+               ec_parsed_link_child(cur_state, child_state);
        completed->cur_state = child_state;
        cur_group = completed->cur_group;
        completed->cur_group = NULL;
@@ -89,7 +89,7 @@ ec_node_complete_child(const struct ec_node *node,
 
        /* restore parent parse state */
        if (cur_state != NULL) {
-               ec_parsed_del_child(cur_state, child_state);
+               ec_parsed_unlink_child(cur_state, child_state);
                assert(!ec_parsed_has_child(child_state));
        }
        ec_parsed_free(child_state);
index 1ed896f..c880324 100644 (file)
@@ -137,7 +137,7 @@ struct ec_node {
  * code */
 struct ec_node *__ec_node(const struct ec_node_type *type, const char *id);
 
-/* create a_new node node */
+/* create a new node */
 struct ec_node *ec_node(const char *typename, const char *id);
 
 struct ec_node *ec_node_clone(struct ec_node *node);
index 6e36957..7188f3f 100644 (file)
@@ -7,8 +7,6 @@
 
 #include <ecoli_node.h>
 
-/* XXX remove the _new for all other nodes */
-
 /**
  * Callback function type for evaluating a variable
  *
index ab0a0d3..7d38a89 100644 (file)
@@ -60,7 +60,7 @@ static int ec_node_many_parse(const struct ec_node *gen_node,
                /* it matches an empty strvec, no need to continue */
                if (ret == 0) {
                        child_parsed = ec_parsed_get_last_child(state);
-                       ec_parsed_del_child(state, child_parsed);
+                       ec_parsed_unlink_child(state, child_parsed);
                        ec_parsed_free(child_parsed);
                        break;
                }
index 252ebfd..e0220e6 100644 (file)
@@ -125,7 +125,7 @@ ec_node_re_lex_parse(const struct ec_node *gen_node,
                ret = 1;
        } else if (ret != EC_PARSED_NOMATCH) {
                child_parsed = ec_parsed_get_last_child(state);
-               ec_parsed_del_child(state, child_parsed);
+               ec_parsed_unlink_child(state, child_parsed);
                ec_parsed_free(child_parsed);
                ret = EC_PARSED_NOMATCH;
        }
index 1ad151f..d5c86eb 100644 (file)
@@ -245,7 +245,7 @@ ec_node_sh_lex_parse(const struct ec_node *gen_node,
                ret = 1;
        } else if (ret != EC_PARSED_NOMATCH) {
                child_parsed = ec_parsed_get_last_child(state);
-               ec_parsed_del_child(state, child_parsed);
+               ec_parsed_unlink_child(state, child_parsed);
                ec_parsed_free(child_parsed);
                ret = EC_PARSED_NOMATCH;
        }
@@ -272,7 +272,7 @@ ec_node_sh_lex_complete(const struct ec_node *gen_node,
        struct ec_completed_item *item = NULL;
        char *new_str = NULL;
        const char *str;
-       char missing_quote;
+       char missing_quote = '\0';
        int ret;
 
        if (ec_strvec_len(strvec) != 1)
index 2f6b9e5..3ec44dc 100644 (file)
@@ -103,7 +103,7 @@ __ec_node_subset_parse(struct parse_result *out, struct ec_node **table,
                /* replace the previous best result */
                ec_parsed_free(best_parsed);
                best_parsed = ec_parsed_get_last_child(state);
-               ec_parsed_del_child(state, best_parsed);
+               ec_parsed_unlink_child(state, best_parsed);
 
                best_result.parsed_len = result.parsed_len + 1;
                best_result.len = len + result.len;
@@ -114,7 +114,7 @@ __ec_node_subset_parse(struct parse_result *out, struct ec_node **table,
        *out = best_result;
        ec_free(child_table);
        if (best_parsed != NULL)
-               ec_parsed_add_child(state, best_parsed);
+               ec_parsed_link_child(state, best_parsed);
 
        return 0;
 
index 035ec13..4816222 100644 (file)
@@ -44,7 +44,7 @@ static int __ec_node_parse_child(const struct ec_node *node,
                if (child == NULL)
                        return -ENOMEM;
 
-               ec_parsed_add_child(state, child);
+               ec_parsed_link_child(state, child);
        } else {
                child = state;
        }
@@ -64,7 +64,7 @@ static int __ec_node_parse_child(const struct ec_node *node,
 
 free:
        if (!is_root) {
-               ec_parsed_del_child(state, child);
+               ec_parsed_unlink_child(state, child);
                ec_parsed_free(child);
        }
        return ret;
@@ -180,7 +180,7 @@ __ec_parsed_dup(const struct ec_parsed *root, const struct ec_parsed *ref,
                dup_child = __ec_parsed_dup(child, ref, new_ref);
                if (dup_child == NULL)
                        goto fail;
-               ec_parsed_add_child(dup, dup_child);
+               ec_parsed_link_child(dup, dup_child);
        }
 
        return dup;
@@ -190,9 +190,10 @@ fail:
        return NULL;
 }
 
-struct ec_parsed *ec_parsed_dup(struct ec_parsed *parsed) //XXX const
+struct ec_parsed *ec_parsed_dup(const struct ec_parsed *parsed)
 {
-       struct ec_parsed *root, *dup_root, *dup = NULL;
+       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);
@@ -275,15 +276,14 @@ void ec_parsed_dump(FILE *out, const struct ec_parsed *parsed)
        __ec_parsed_dump(out, parsed, 0);
 }
 
-void ec_parsed_add_child(struct ec_parsed *parsed,
+void ec_parsed_link_child(struct ec_parsed *parsed,
        struct ec_parsed *child)
 {
        TAILQ_INSERT_TAIL(&parsed->children, child, next);
        child->parent = parsed;
 }
 
-// XXX we can remove the first arg ? parsed == child->parent ?
-void ec_parsed_del_child(struct ec_parsed *parsed, // XXX rename del in unlink?
+void ec_parsed_unlink_child(struct ec_parsed *parsed,
        struct ec_parsed *child)
 {
        TAILQ_REMOVE(&parsed->children, child, next);
@@ -317,16 +317,16 @@ 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) // rename in free
+void ec_parsed_del_last_child(struct ec_parsed *parsed)
 {
        struct ec_parsed *child;
 
        child = ec_parsed_get_last_child(parsed);
-       ec_parsed_del_child(parsed, child);
+       ec_parsed_unlink_child(parsed, child);
        ec_parsed_free(child);
 }
 
-struct ec_parsed *ec_parsed_get_root(struct ec_parsed *parsed)
+struct ec_parsed *__ec_parsed_get_root(struct ec_parsed *parsed)
 {
        if (parsed == NULL)
                return NULL;
@@ -337,7 +337,7 @@ struct ec_parsed *ec_parsed_get_root(struct ec_parsed *parsed)
        return parsed;
 }
 
-struct ec_parsed *ec_parsed_get_parent(struct ec_parsed *parsed)
+struct ec_parsed *ec_parsed_get_parent(const struct ec_parsed *parsed)
 {
        if (parsed == NULL)
                return NULL;
index 735e4d5..9f7a698 100644 (file)
@@ -48,7 +48,7 @@ void ec_parsed_free_children(struct ec_parsed *parsed);
  *
  *
  */
-struct ec_parsed *ec_parsed_dup(struct ec_parsed *parsed);
+struct ec_parsed *ec_parsed_dup(const struct ec_parsed *parsed);
 
 /**
  *
@@ -105,29 +105,39 @@ int ec_node_parse_child(const struct ec_node *node,
  *
  *
  */
-void ec_parsed_add_child(struct ec_parsed *parsed,
+void ec_parsed_link_child(struct ec_parsed *parsed,
                        struct ec_parsed *child);
 /**
  *
  *
  *
  */
-void ec_parsed_del_child(struct ec_parsed *parsed,
+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_root(struct ec_parsed *parsed);
 
 /**
  *
  *
  *
  */
-struct ec_parsed *ec_parsed_get_parent(struct ec_parsed *parsed);
+struct ec_parsed *ec_parsed_get_parent(const struct ec_parsed *parsed);
 
 /**
  * Get the first child of a tree.
index 80e91c9..8b8a094 100644 (file)
@@ -106,7 +106,7 @@ static char **my_attempted_completion(const char *text, int start, int end)
 static char *get_node_help(const struct ec_completed_item *item)
 {
        const struct ec_completed_group *grp;
-       struct ec_parsed *state; // XXX keep const with macro
+       const struct ec_parsed *state;
        const struct ec_node *node;
        char *help = NULL;
        const char *node_help = NULL;