[augeas-devel] [PATCH 1/2] Rename struct path to struct pathx

David Lutterkort lutter at redhat.com
Mon Dec 22 23:21:11 UTC 2008


Also rename path_* functions to pathx_*; rename make_pathx to pathx_parse
and change its signature to allow cleaner error reporting
---
 src/augeas.c   |   91 +++++++++++++++++++++++++++----------------------------
 src/internal.h |   16 +++++-----
 src/pathx.c    |   51 +++++++++++++++----------------
 3 files changed, 78 insertions(+), 80 deletions(-)

diff --git a/src/augeas.c b/src/augeas.c
index 4d61ffc..c6fe908 100644
--- a/src/augeas.c
+++ b/src/augeas.c
@@ -219,36 +219,36 @@ struct augeas *aug_init(const char *root, const char *loadpath,
 }
 
 int aug_get(const struct augeas *aug, const char *path, const char **value) {
-    struct path *p = make_path(aug->origin->children, path);
+    struct pathx *p;
     struct tree *match;
     int r;
 
-    if (p == NULL)
+    if (pathx_parse(aug->origin->children, path, &p) != 0)
         return -1;
 
     if (value != NULL)
         *value = NULL;
 
-    r = path_find_one(p, &match);
+    r = pathx_find_one(p, &match);
     if (r == 1 && value != NULL)
         *value = match->value;
-    free_path(p);
+    free_pathx(p);
 
     return r;
 }
 
 struct tree *tree_set(struct tree *root, const char *path, const char *value) {
     struct tree *tree;
-    struct path *p = make_path(root, path);
+    struct pathx *p;
     int r;
 
-    if (p == NULL)
+    if (pathx_parse(root, path, &p) != 0)
         goto error;
 
-    r = path_expand_tree(p, &tree);
+    r = pathx_expand_tree(p, &tree);
     if (r == -1)
         goto error;
-    free_path(p);
+    free_pathx(p);
 
     if (tree->value != NULL) {
         free(tree->value);
@@ -262,7 +262,7 @@ struct tree *tree_set(struct tree *root, const char *path, const char *value) {
     tree->dirty = 1;
     return tree;
  error:
-    free_path(p);
+    free_pathx(p);
     return NULL;
 }
 
@@ -274,17 +274,16 @@ int tree_insert(struct tree *origin, const char *path, const char *label,
                 int before) {
     assert(origin->parent == origin);
 
-    struct path *p = NULL;
+    struct pathx *p = NULL;
     struct tree *new = NULL, *match;
 
     if (strchr(label, SEP) != NULL)
         return -1;
 
-    p = make_path(origin->children, path);
-    if (p == NULL)
+    if (pathx_parse(origin->children, path, &p) != 0)
         goto error;
 
-    if (path_find_one(p, &match) != 1)
+    if (pathx_find_one(p, &match) != 1)
         goto error;
 
     new = make_tree(strdup(label), NULL, match->parent, NULL);
@@ -297,11 +296,11 @@ int tree_insert(struct tree *origin, const char *path, const char *label,
         new->next = match->next;
         match->next = new;
     }
-    free_path(p);
+    free_pathx(p);
     return 0;
  error:
     free_tree(new);
-    free_path(p);
+    free_pathx(p);
     return -1;
 }
 
@@ -367,22 +366,21 @@ int tree_rm(struct tree *origin, const char *path) {
     assert(origin->parent == origin);
     assert(origin->next == NULL);
 
-    struct path *p = NULL;
+    struct pathx *p = NULL;
     struct tree *root = origin->children;
     struct tree *tree, **del;
     int cnt = 0, ndel = 0, i;
 
-    p = make_path(root, path);
-    if (p == NULL)
+    if (pathx_parse(root, path, &p) != 0)
         return -1;
 
-    for (tree = path_first(p); tree != NULL; tree = path_next(p, tree)) {
+    for (tree = pathx_first(p); tree != NULL; tree = pathx_next(p, tree)) {
         if (! TREE_HIDDEN(tree))
             ndel += 1;
     }
 
     if (ndel == 0) {
-        free_path(p);
+        free_pathx(p);
         return 0;
     }
 
@@ -391,13 +389,13 @@ int tree_rm(struct tree *origin, const char *path) {
         return -1;
     }
 
-    for (i = 0, tree = path_first(p); tree != NULL; tree = path_next(p, tree)) {
+    for (i = 0, tree = pathx_first(p); tree != NULL; tree = pathx_next(p, tree)) {
         if (TREE_HIDDEN(tree))
             continue;
         del[i] = tree;
         i += 1;
     }
-    free_path(p);
+    free_pathx(p);
 
     for (i = 0; i < ndel; i++) {
         assert (del[i]->parent != NULL);
@@ -438,20 +436,19 @@ int aug_tree_replace(struct augeas *aug, const char *path, struct tree *sub) {
 
 int aug_mv(struct augeas *aug, const char *src, const char *dst) {
     struct tree *root = aug->origin->children;
-    struct path *s = make_path(root, src);
-    struct path *d = make_path(root, dst);
+    struct pathx *s, *d;
     struct tree *ts, *td, *t;
     int r, ret;
 
     ret = -1;
-    if (s == NULL || d == NULL)
+    if (pathx_parse(root, src, &s) != 0 || pathx_parse(root, dst, &d) != 0)
         goto done;
 
-    r = path_find_one(s, &ts);
+    r = pathx_find_one(s, &ts);
     if (r != 1)
         goto done;
 
-    r = path_expand_tree(d, &td);
+    r = pathx_expand_tree(d, &td);
     if (r == -1)
         goto done;
 
@@ -485,13 +482,13 @@ int aug_mv(struct augeas *aug, const char *src, const char *dst) {
 
     ret = 0;
  done:
-    free_path(s);
-    free_path(d);
+    free_pathx(s);
+    free_pathx(d);
     return ret;
 }
 
 int aug_match(const struct augeas *aug, const char *pathin, char ***matches) {
-    struct path *p = NULL;
+    struct pathx *p = NULL;
     struct tree *tree;
     int cnt = 0;
 
@@ -502,15 +499,14 @@ int aug_match(const struct augeas *aug, const char *pathin, char ***matches) {
         pathin = "/*";
     }
 
-    p = make_path(aug->origin->children, pathin);
-    if (p == NULL)
+    if (pathx_parse(aug->origin->children, pathin, &p) != 0)
         return -1;
 
-    for (tree = path_first(p); tree != NULL; tree = path_next(p, tree)) {
+    for (tree = pathx_first(p); tree != NULL; tree = pathx_next(p, tree)) {
         if (! TREE_HIDDEN(tree))
             cnt += 1;
     }
-    free_path(p);
+    free_pathx(p);
     p = NULL;
 
     if (matches == NULL)
@@ -520,9 +516,10 @@ int aug_match(const struct augeas *aug, const char *pathin, char ***matches) {
     if (*matches == NULL)
         goto error;
 
-    p = make_path(aug->origin->children, pathin);
+    pathx_parse(aug->origin->children, pathin, &p);
+
     int i = 0;
-    for (tree = path_first(p); tree != NULL; tree = path_next(p, tree)) {
+    for (tree = pathx_first(p); tree != NULL; tree = pathx_next(p, tree)) {
         if (TREE_HIDDEN(tree))
             continue;
         (*matches)[i] = format_path(tree);
@@ -531,7 +528,7 @@ int aug_match(const struct augeas *aug, const char *pathin, char ***matches) {
         }
         i += 1;
     }
-    free_path(p);
+    free_pathx(p);
     return cnt;
 
  error:
@@ -542,7 +539,7 @@ int aug_match(const struct augeas *aug, const char *pathin, char ***matches) {
             free(*matches);
         }
     }
-    free_path(p);
+    free_pathx(p);
     return -1;
 }
 
@@ -605,13 +602,15 @@ static int tree_save(struct augeas *aug, struct tree *tree, const char *path,
 int aug_save(struct augeas *aug) {
     int ret = 0;
     struct tree *files;
-    struct path *p = make_path(aug->origin->children, AUGEAS_FILES_TREE);
+    struct pathx *p;
 
-    if (p == NULL || path_find_one(p, &files) != 1) {
-        free_path(p);
+    if (pathx_parse(aug->origin->children, AUGEAS_FILES_TREE, &p) != 0)
+        return -1;
+    if (pathx_find_one(p, &files) != 1) {
+        free_pathx(p);
         return -1;
     }
-    free_path(p);
+    free_pathx(p);
 
     list_for_each(t, aug->origin->children) {
         tree_propagate_dirty(t);
@@ -678,15 +677,15 @@ static int print_rec(FILE *out, struct tree *start, const char *ppath,
 int print_tree(const struct tree *start, FILE *out, const char *pathin,
                int pr_hidden) {
 
-    struct path *p = make_path(start, pathin);
+    struct pathx *p;
     char *path = NULL;
     struct tree *tree;
     int r;
 
-    if (p == NULL)
+    if (pathx_parse(start, pathin, &p) != 0)
         return -1;
 
-    for (tree = path_first(p); tree != NULL; tree = path_next(p, tree)) {
+    for (tree = pathx_first(p); tree != NULL; tree = pathx_next(p, tree)) {
         if (TREE_HIDDEN(tree) && ! pr_hidden)
             continue;
 
@@ -702,7 +701,7 @@ int print_tree(const struct tree *start, FILE *out, const char *pathin,
         free(path);
         path = NULL;
     }
-    free_path(p);
+    free_pathx(p);
     return 0;
  error:
     free(path);
diff --git a/src/internal.h b/src/internal.h
index da0a501..0094147 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -334,14 +334,14 @@ int close_memstream(struct memstream *ms);
  * Path expressions
  */
 
-struct path;
-
-struct path *make_path(const struct tree *root, const char *path);
-struct tree *path_first(struct path *path);
-struct tree *path_next(struct path *path, struct tree *cur);
-int path_find_one(struct path *path, struct tree **match);
-int path_expand_tree(struct path *path, struct tree **tree);
-void free_path(struct path *path);
+struct pathx;
+
+int pathx_parse(const struct tree *root, const char *path, struct pathx **px);
+struct tree *pathx_first(struct pathx *path);
+struct tree *pathx_next(struct pathx *path, struct tree *cur);
+int pathx_find_one(struct pathx *path, struct tree **match);
+int pathx_expand_tree(struct pathx *path, struct tree **tree);
+void free_pathx(struct pathx *path);
 
 #endif
 
diff --git a/src/pathx.c b/src/pathx.c
index 90c5504..171f706 100644
--- a/src/pathx.c
+++ b/src/pathx.c
@@ -38,14 +38,14 @@ struct segment {
     char        *label;
 };
 
-struct path {
+struct pathx {
     size_t      nsegments;
     struct segment *segments;
     struct tree    *root;
 };
 
 #define for_each_segment(seg, path)                                 \
-    for (typeof(path->segments) (seg) = (path)->segments;           \
+    for (typeof((path)->segments) (seg) = (path)->segments;         \
          (seg) < (path)->segments + (path)->nsegments;              \
          (seg)++)
 
@@ -80,7 +80,7 @@ static int sibling_index(struct tree *tree) {
     return indx;
 }
 
-void free_path(struct path *path) {
+void free_pathx(struct pathx *path) {
     if (path == NULL)
         return;
 
@@ -102,32 +102,31 @@ void free_path(struct path *path) {
  * SEGMENT = STRING ('[' N ']') ? | '*'
  * where STRING is any string not containing '/' and N is a positive number
  */
-struct path *make_path(const struct tree *root, const char *path) {
-    struct path *result;
+int pathx_parse(const struct tree *root, const char *path,
+                struct pathx **px) {
 
-    CALLOC(result, 1);
-    if (result == NULL)
-        return NULL;
-    result->root = (struct tree *) root;
+    if (ALLOC(*px) < 0)
+        return -1;
+    (*px)->root = (struct tree *) root;
 
     if (*path != SEP)
-        result->nsegments = 1;
+        (*px)->nsegments = 1;
     for (const char *p = path; *p != '\0'; p++) {
         if (*p == SEP) {
             while (*p == SEP) p++;
             if (*p == '\0')
                 break;
-            result->nsegments++;
+            (*px)->nsegments++;
         }
     }
-    if (result->nsegments == 0)
+    if ((*px)->nsegments == 0)
         goto error;
 
-    CALLOC(result->segments, result->nsegments);
-    if (result->segments == NULL)
+    CALLOC((*px)->segments, (*px)->nsegments);
+    if ((*px)->segments == NULL)
         goto error;
 
-    for_each_segment(seg, result) {
+    for_each_segment(seg, *px) {
         const char *next, *brack;
 
         while (*path == SEP)
@@ -162,11 +161,11 @@ struct path *make_path(const struct tree *root, const char *path) {
         path = next;
         while (*path == SEP) path += 1;
     }
-    return result;
+    return 0;
 
  error:
-    free_path(result);
-    return NULL;
+    free_pathx(*px);
+    return -1;
 }
 
 static void calc_last_index(struct segment *seg, struct tree *tree) {
@@ -205,7 +204,7 @@ static int tree_next(struct tree **tree, int *depth, int mindepth) {
 
 /* Given a node TREE that should match the segment SEGNR in PATH,
    find the next node that matches all of PATH, or return NULL */
-static struct tree *complete_path(struct path *path, int segnr,
+static struct tree *complete_path(struct pathx *path, int segnr,
                                   struct tree *tree) {
     int cur = segnr;
 
@@ -230,7 +229,7 @@ static struct tree *complete_path(struct path *path, int segnr,
     }
 }
 
-struct tree *path_next(struct path *path, struct tree *cur) {
+struct tree *pathx_next(struct pathx *path, struct tree *cur) {
     int segnr = path->nsegments - 1;
 
     if (tree_next(&cur, &segnr, -1) < 0)
@@ -240,7 +239,7 @@ struct tree *path_next(struct path *path, struct tree *cur) {
 }
 
 /* Find the first node in TREE matching PATH. */
-struct tree *path_first(struct path *path) {
+struct tree *pathx_first(struct pathx *path) {
     return complete_path(path, 0, path->root);
 }
 
@@ -253,7 +252,7 @@ struct tree *path_first(struct path *path) {
  * number of the segment in PATH where MATCH matched. If no node matches,
  * MATCH will be NULL, and SEGNR -1
  */
-static int path_search(struct path *path, struct tree **match, int *segnr) {
+static int path_search(struct pathx *path, struct tree **match, int *segnr) {
     struct tree **matches = NULL;
     int *nmatches = NULL;
     int result;
@@ -323,7 +322,7 @@ static int path_search(struct path *path, struct tree **match, int *segnr) {
  * Return the first segment that was created by this operation, or NULL on
  * error.
  */
-int path_expand_tree(struct path *path, struct tree **tree) {
+int pathx_expand_tree(struct pathx *path, struct tree **tree) {
     int r, segnr;
 
     *tree = path->root;
@@ -364,12 +363,12 @@ int path_expand_tree(struct path *path, struct tree **tree) {
     return -1;
 }
 
-int path_find_one(struct path *path, struct tree **tree) {
-    *tree = path_first(path);
+int pathx_find_one(struct pathx *path, struct tree **tree) {
+    *tree = pathx_first(path);
     if (*tree == NULL)
         return 0;
 
-    if (path_next(path, *tree) != NULL) {
+    if (pathx_next(path, *tree) != NULL) {
         *tree = NULL;
         return -1;
     }
-- 
1.6.0.4




More information about the augeas-devel mailing list