[augeas-devel] [PATCH 6/8] path expressions: add 'glob' function that constructs regexps from globs

lutter at redhat.com lutter at redhat.com
Sat May 7 01:12:54 UTC 2011


From: David Lutterkort <lutter at redhat.com>

---
 src/pathx.c       |   33 +++++++++++++++++++++++++++------
 tests/xpath.tests |   12 ++++++++++++
 2 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/src/pathx.c b/src/pathx.c
index a2371f8..f49dd7b 100644
--- a/src/pathx.c
+++ b/src/pathx.c
@@ -276,6 +276,7 @@ static void func_position(struct state *state);
 static void func_count(struct state *state);
 static void func_label(struct state *state);
 static void func_regexp(struct state *state);
+static void func_glob(struct state *state);
 
 static const enum type const arg_types_nodeset[] = { T_NODESET };
 static const enum type const arg_types_string[] = { T_STRING };
@@ -295,7 +296,13 @@ static const struct func builtin_funcs[] = {
       .impl = func_regexp },
     { .name = "regexp", .arity = 1, .type = T_REGEXP,
       .arg_types = arg_types_nodeset,
-      .impl = func_regexp }
+      .impl = func_regexp },
+    { .name = "glob", .arity = 1, .type = T_REGEXP,
+      .arg_types = arg_types_string,
+      .impl = func_glob },
+    { .name = "glob", .arity = 1, .type = T_REGEXP,
+      .arg_types = arg_types_nodeset,
+      .impl = func_glob }
 };
 
 #define CHECK_ERROR                                                     \
@@ -633,7 +640,7 @@ static void func_label(struct state *state) {
 }
 
 static struct regexp *
-nodeset_as_regexp(struct info *info, struct nodeset *ns) {
+nodeset_as_regexp(struct info *info, struct nodeset *ns, int glob) {
     struct regexp *result = NULL;
     struct regexp **rx = NULL;
     int used = 0;
@@ -652,7 +659,10 @@ nodeset_as_regexp(struct info *info, struct nodeset *ns) {
             if (ns->nodes[i]->value == NULL)
                 continue;
 
-            rx[i] = make_regexp_dup(info, ns->nodes[i]->value, 0);
+            if (glob)
+                rx[i] = make_regexp_from_glob(info, ns->nodes[i]->value);
+            else
+                rx[i] = make_regexp_dup(info, ns->nodes[i]->value, 0);
             if (rx[i] == NULL)
                 goto error;
         }
@@ -668,7 +678,7 @@ nodeset_as_regexp(struct info *info, struct nodeset *ns) {
     return result;
 }
 
-static void func_regexp(struct state *state) {
+static void func_regexp_or_glob(struct state *state, int glob) {
     value_ind_t vind = make_value(T_REGEXP, state);
     int r;
 
@@ -678,9 +688,12 @@ static void func_regexp(struct state *state) {
     struct regexp *rx = NULL;
 
     if (v->tag == T_STRING) {
-        rx = make_regexp_dup(state->error->info, v->string, 0);
+        if (glob)
+            rx = make_regexp_from_glob(state->error->info, v->string);
+        else
+            rx = make_regexp_dup(state->error->info, v->string, 0);
     } else if (v->tag == T_NODESET) {
-        rx = nodeset_as_regexp(state->error->info, v->nodeset);
+        rx = nodeset_as_regexp(state->error->info, v->nodeset, glob);
     } else {
         assert(0);
     }
@@ -702,6 +715,14 @@ static void func_regexp(struct state *state) {
     push_value(vind, state);
 }
 
+static void func_regexp(struct state *state) {
+    func_regexp_or_glob(state, 0);
+}
+
+static void func_glob(struct state *state) {
+    func_regexp_or_glob(state, 1);
+}
+
 static bool coerce_to_bool(struct value *v) {
     switch (v->tag) {
     case T_NODESET:
diff --git a/tests/xpath.tests b/tests/xpath.tests
index 6f5e090..5bb0a23 100644
--- a/tests/xpath.tests
+++ b/tests/xpath.tests
@@ -233,6 +233,18 @@ test regexp3 /files/etc/hosts/*[ipaddr =~ regexp(/files/etc/hosts/*/ipaddr)]
 # Check that we don't crash when the nodeset contains all NULL's
 test regexp4 /files/etc/hosts/*[ipaddr =~ regexp(/files/etc/hosts/*[ipaddr])]
 
+test glob1 /files[ 'axxa' =~ glob('a*a') ]
+     /files
+
+test glob2 /files[ 'axxa' =~ glob('a?[a-z]a') ]
+     /files
+
+test glob3 /files[ '^a' =~ glob('^a') ]
+     /files
+
+test glob4 /augeas/load/*[ '/etc/hosts' =~ glob(incl) ]
+     /augeas/load/Hosts
+
 # Union of nodesets
 test union (/files/etc/yum.conf | /files/etc/yum.repos.d/*)/*/gpgcheck
      /files/etc/yum.conf/main/gpgcheck = 1
-- 
1.7.4.4




More information about the augeas-devel mailing list