[augeas-devel] [PATCH] Basic support for case insensitive square lens v2

Francis Giraldeau francis.giraldeau at gmail.com
Thu Feb 17 04:33:34 UTC 2011


Square lens must match open and close tag. In some situations, for instance
httpd, open and close tags case is allowed to be different. If the square
regexp is case insensitive, we allow case mismatch between open and close tag.
The default case for the close tag is the one from the key in the create
function, and in the put function, the original case is preserved.

As test case shows, some serious errors occurs when the lens regexp is set to
nocase. Here is the error message while running pass_square.aug:

lt-augparse: put.c:236: split_concat: Assertion `regs.start[reg] != -1' failed.

New in v2:
  * Use STRCASEEQ macro
  * No white space cleanup
  * Never set regexp->info to NULL to avoid undefined behavior
---
 src/get.c                     |   19 ++++++++++++++++---
 src/put.c                     |    5 -----
 tests/modules/pass_square.aug |   40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+), 8 deletions(-)

diff --git a/src/get.c b/src/get.c
index 9c58ac8..fd9d09b 100644
--- a/src/get.c
+++ b/src/get.c
@@ -692,6 +692,7 @@ static struct tree *get_square(struct lens *lens, struct state *state) {
 
     struct tree *tree = NULL;
     char *key = NULL, *square = NULL;
+    int res;
 
     // get the child lens
     tree = get_concat(lens->child, state);
@@ -701,7 +702,13 @@ static struct tree *get_square(struct lens *lens, struct state *state) {
     ensure0(key != NULL, state->info);
     ensure0(square != NULL, state->info);
 
-    if (strcmp(key, square) != 0) {
+    if (lens->ktype->nocase) {
+        res = STRCASEEQ(key, square);
+    } else {
+        res = STREQ(key, square);
+    }
+
+    if (res == 0) {
         get_error(state, lens, "%s \"%s\" %s \"%s\"",
                 "Parse error: mismatched key in square lens, expecting", key,
                 "but got", square);
@@ -1010,6 +1017,7 @@ static void visit_exit(struct lens *lens,
     } else if (lens->tag == L_SQUARE) {
         if (rec_state->mode == M_GET) {
             char *key, *square;
+            int res;
 
             key = top_frame(rec_state)->key;
             square = top_frame(rec_state)->square;
@@ -1017,8 +1025,13 @@ static void visit_exit(struct lens *lens,
             ensure(key != NULL, state->info);
             ensure(square != NULL, state->info);
 
-            // raise syntax error if they are not equals
-            if (strcmp(key, square) != 0){
+            if (lens->ktype->nocase) {
+                res = STRCASEEQ(key, square);
+            } else {
+                res = STREQ(key, square);
+            }
+
+            if (res == 0) {
                 get_error(state, lens, "%s \"%s\" %s \"%s\"",
                                 "Parse error: mismatched key in square lens, expecting", key,
                                 "but got", square);
diff --git a/src/put.c b/src/put.c
index 32618c4..4f90e24 100644
--- a/src/put.c
+++ b/src/put.c
@@ -468,12 +468,7 @@ static void put_del(ATTRIBUTE_UNUSED struct lens *lens, struct state *state) {
     assert(lens->tag == L_DEL);
     assert(state->skel != NULL);
     assert(state->skel->tag == L_DEL);
-    if (lens->string != NULL) {
     fprintf(state->out, "%s", state->skel->text);
-    } else {
-    /* L_DEL with NULL string: replicate the current key */
-        fprintf(state->out, "%s", state->key);
-    }
 }
 
 static void put_union(struct lens *lens, struct state *state) {
diff --git a/tests/modules/pass_square.aug b/tests/modules/pass_square.aug
index 0185031..08ad014 100644
--- a/tests/modules/pass_square.aug
+++ b/tests/modules/pass_square.aug
@@ -112,3 +112,43 @@ test sqr3 put input3 after clear "/x[1]" = input3
 let b4 = del "x" "x"
 let rec sqr4 = [ del /[a]+/ "a" . square /[b]|[c]/ (b4|sqr4) ]
 test sqr4 put "aabaaacxcb" after rm "x" = "aabaaacxcb"
+
+(* test case insensitive match *)
+let b5 = del "x" "x"
+let sqr5 = [ square /[a]+/i b5 . b5 ]*
+test sqr5 put "axaxAxAxaxAxAxax" after rm "/x" = "axaxAxAxaxAxAxax"
+test sqr5 put "" after clear "/A" = "AxAx"
+test sqr5 put "" after clear "/a" = "axax"
+
+(* Basic element *)
+let xml_element_i (body:lens) =
+    let g = del ">" ">" . body . del "</" "</" in
+        [ del "<" "<" . square /[a-z]+/i g . del ">" ">" ] *
+
+let rec xml_rec_i = xml_element_i xml_rec_i
+let doc_i = "<a><B><c><D><e></E></d></c></b></A>"
+test xml_rec_i get doc_i =
+  { "a"
+    { "B"
+      { "c"
+        { "D"
+          { "e" }
+        }
+      }
+    }
+  }
+
+let rec rec_i = [ key /[a]/i . rec_i ]?
+test rec_i get "aA" =
+  { "a"
+    { "A" }
+  }
+
+(* test case sensitive: error raised *)
+let b7 = del "x" "x"
+let sqr7 = [ square /[a]+/ b5 . b5 ]*
+test sqr7 get "axA" = *
+
+(* lt-augparse: put.c:236: split_concat:
+   Assertion `regs.start[reg] != -1' failed. *)
+test xml_rec_i put doc_i after rm "x" = ?
-- 
1.7.1




More information about the augeas-devel mailing list