[augeas-devel] augeas: master - New primitive lens 'value'

David Lutterkort lutter at fedoraproject.org
Sun Jan 17 22:23:53 UTC 2010


Gitweb:        http://git.fedorahosted.org/git/augeas.git?p=augeas.git;a=commitdiff;h=911e52f8f851e483dc03d8d75be7bed3ac667a75
Commit:        911e52f8f851e483dc03d8d75be7bed3ac667a75
Parent:        884dc9eb33d78286877c1d71220abb8e4c47bd35
Author:        David Lutterkort <lutter at redhat.com>
AuthorDate:    Tue Mar 17 12:22:46 2009 -0700
Committer:     David Lutterkort <lutter at redhat.com>
CommitterDate: Fri Jan 15 16:34:41 2010 -0800

New primitive lens 'value'

In analogy to 'label', this lens allows setting the value of a tree node to
a fixed string
---
 src/builtin.c                      |    7 +++++++
 src/get.c                          |   18 ++++++++++++++++++
 src/lens.c                         |   11 ++++++++---
 src/lens.h                         |    3 ++-
 src/put.c                          |    4 ++++
 tests/modules/pass_label_value.aug |   27 +++++++++++++++++++++++++++
 6 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/src/builtin.c b/src/builtin.c
index 1d61a7a..78593fe 100644
--- a/src/builtin.c
+++ b/src/builtin.c
@@ -56,6 +56,12 @@ static struct value *lns_store(struct info *info, struct value *rxp) {
     return lns_make_prim(L_STORE, ref(info), ref(rxp->regexp), NULL);
 }
 
+/* V_STRING -> V_LENS */
+static struct value *lns_value(struct info *info, struct value *str) {
+    assert(str->tag == V_STRING);
+    return lns_make_prim(L_VALUE, ref(info), NULL, ref(str->string));
+}
+
 /* V_REGEXP -> V_LENS */
 static struct value *lns_key(struct info *info, struct value *rxp) {
     assert(rxp->tag == V_REGEXP);
@@ -456,6 +462,7 @@ struct module *builtin_init(struct error *error) {
     /* Primitive lenses */
     DEFINE_NATIVE(modl, "del",     2, lns_del, T_REGEXP, T_STRING, T_LENS);
     DEFINE_NATIVE(modl, "store",   1, lns_store, T_REGEXP, T_LENS);
+    DEFINE_NATIVE(modl, "value",   1, lns_value, T_STRING, T_LENS);
     DEFINE_NATIVE(modl, "key",     1, lns_key, T_REGEXP, T_LENS);
     DEFINE_NATIVE(modl, "label",   1, lns_label, T_STRING, T_LENS);
     DEFINE_NATIVE(modl, "seq",     1, lns_seq, T_STRING, T_LENS);
diff --git a/src/get.c b/src/get.c
index cdddd7a..1d51f94 100644
--- a/src/get.c
+++ b/src/get.c
@@ -420,6 +420,18 @@ static struct skel *parse_store(struct lens *lens,
     return make_skel(lens);
 }
 
+static struct tree *get_value(struct lens *lens, struct state *state) {
+    assert(lens->tag == L_VALUE);
+    state->value = strdup(lens->string->str);
+    return NULL;
+}
+
+static struct skel *parse_value(struct lens *lens,
+                                ATTRIBUTE_UNUSED struct state *state) {
+    assert(lens->tag == L_VALUE);
+    return make_skel(lens);
+}
+
 static struct tree *get_key(struct lens *lens, struct state *state) {
     assert(lens->tag == L_KEY);
     if (! REG_MATCHED(state))
@@ -1043,6 +1055,9 @@ static struct tree *get_lens(struct lens *lens, struct state *state) {
     case L_STORE:
         tree = get_store(lens, state);
         break;
+    case L_VALUE:
+        tree = get_value(lens, state);
+        break;
     case L_KEY:
         tree = get_key(lens, state);
         break;
@@ -1169,6 +1184,9 @@ static struct skel *parse_lens(struct lens *lens, struct state *state,
     case L_STORE:
         skel = parse_store(lens, state);
         break;
+    case L_VALUE:
+        skel = parse_value(lens, state);
+        break;
     case L_KEY:
         skel = parse_key(lens, state);
         break;
diff --git a/src/lens.c b/src/lens.c
index 85d3932..f65bb80 100644
--- a/src/lens.c
+++ b/src/lens.c
@@ -472,14 +472,15 @@ struct value *lns_make_prim(enum lens_tag tag, struct info *info,
     lens->regexp = regexp;
     lens->string = string;
     lens->key = (tag == L_KEY || tag == L_LABEL || tag == L_SEQ);
-    lens->value = (tag == L_STORE);
-    lens->consumes_value = (tag == L_STORE);
+    lens->value = (tag == L_STORE || tag == L_VALUE);
+    lens->consumes_value = (tag == L_STORE || tag == L_VALUE);
     lens->atype = regexp_make_empty(info);
     /* Set the ctype */
     if (tag == L_DEL || tag == L_STORE || tag == L_KEY) {
         lens->ctype = ref(regexp);
         lens->ctype_nullable = regexp_matches_empty(lens->ctype);
-    } else if (tag == L_LABEL || tag == L_SEQ || tag == L_COUNTER) {
+    } else if (tag == L_LABEL || tag == L_VALUE
+               || tag == L_SEQ || tag == L_COUNTER) {
         lens->ctype = regexp_make_empty(info);
         lens->ctype_nullable = 1;
     } else {
@@ -504,6 +505,8 @@ struct value *lns_make_prim(enum lens_tag tag, struct info *info,
     /* Set the vtype */
     if (tag == L_STORE) {
         lens->vtype = restrict_regexp(lens->regexp);
+    } else if (tag == L_VALUE) {
+        lens->vtype = make_regexp_literal(info, lens->string->str);
     }
 
     return make_lens_value(lens);
@@ -761,6 +764,7 @@ void free_lens(struct lens *lens) {
     case L_LABEL:
     case L_SEQ:
     case L_COUNTER:
+    case L_VALUE:
         unref(lens->string, string);
         break;
     case L_SUBTREE:
@@ -1027,6 +1031,7 @@ int lns_format_atype(struct lens *l, char **buf) {
     case L_STORE:
     case L_KEY:
     case L_LABEL:
+    case L_VALUE:
     case L_SEQ:
     case L_COUNTER:
         *buf = strdup("");
diff --git a/src/lens.h b/src/lens.h
index e5a59bd..9742c87 100644
--- a/src/lens.h
+++ b/src/lens.h
@@ -30,6 +30,7 @@
 enum lens_tag {
     L_DEL = 42,    /* Shift tag values so we fail fast(er) on bad pointers */
     L_STORE,
+    L_VALUE,
     L_KEY,
     L_LABEL,
     L_SEQ,
@@ -84,7 +85,7 @@ struct lens {
         /* Primitive lenses */
         struct {                   /* L_DEL uses both */
             struct regexp *regexp; /* L_STORE, L_KEY */
-            struct string *string; /* L_LABEL, L_SEQ, L_COUNTER */
+            struct string *string; /* L_VALUE, L_LABEL, L_SEQ, L_COUNTER */
         };
         /* Combinators */
         struct lens *child;         /* L_SUBTREE, L_STAR, L_MAYBE */
diff --git a/src/put.c b/src/put.c
index 16026cd..35bcd2e 100644
--- a/src/put.c
+++ b/src/put.c
@@ -370,6 +370,8 @@ static int skel_instance_of(struct lens *lens, struct skel *skel) {
         return skel->tag == L_KEY;
     case L_LABEL:
         return skel->tag == L_LABEL;
+    case L_VALUE:
+        return skel->tag == L_VALUE;
     case L_SEQ:
         return skel->tag == L_SEQ;
     case L_COUNTER:
@@ -575,6 +577,7 @@ static void put_lens(struct lens *lens, struct state *state) {
         fprintf(state->out, "%s", state->key);
         break;
     case L_LABEL:
+    case L_VALUE:
         /* Nothing to do */
         break;
     case L_SEQ:
@@ -694,6 +697,7 @@ static void create_lens(struct lens *lens, struct state *state) {
         fprintf(state->out, "%s", state->key);
         break;
     case L_LABEL:
+    case L_VALUE:
         /* Nothing to do */
         break;
     case L_SEQ:
diff --git a/tests/modules/pass_label_value.aug b/tests/modules/pass_label_value.aug
new file mode 100644
index 0000000..4df5832
--- /dev/null
+++ b/tests/modules/pass_label_value.aug
@@ -0,0 +1,27 @@
+module Pass_label_value =
+
+let l = [ label "label" . value "value" ]
+
+test l get "" = { "label" = "value" }
+
+test l put "" after rm "/foo" = ""
+
+let word = /[^ \t\n]+/
+let ws = del /[ \t]+/ " "
+let chain = [ key "RewriteCond" . ws .
+              [ label "eq" . store word ] . ws . store word .
+              ([ label "chain_as" . ws . del "[OR]" "[OR]" . value "or"]
+              |[ label "chain_as" . value "and" ]) ]
+
+test chain get "RewriteCond %{var} val [OR]" =
+  { "RewriteCond" = "val"
+    { "eq" = "%{var}" }
+    { "chain_as" = "or" } }
+
+test chain get "RewriteCond %{var} lue" =
+  { "RewriteCond" = "lue"
+    { "eq" = "%{var}" }
+    { "chain_as" = "and" } }
+
+test chain put "RewriteCond %{var} val [OR]" after
+  set "/RewriteCond/chain_as" "and" = "RewriteCond %{var} val"




More information about the augeas-devel mailing list