[augeas-devel] [PATCH] Posix ERE compliant escaping

David Lutterkort dlutter at redhat.com
Mon Aug 11 21:26:18 UTC 2008


2 files changed, 9 insertions(+), 19 deletions(-)
src/fa.c       |   26 +++++++-------------------
tests/fatest.c |    2 ++


# HG changeset patch
# User David Lutterkort <dlutter at redhat.com>
# Date 1218489925 25200
# Node ID 524fbc9f65e95d5f837f80a994d845893e0cabe5
# Parent  4c27b26fe4e256f0da5c15a56d638ec00385a428
Posix ERE compliant escaping

Posix extended regular expressions treat a backslash outside of a character
class always as an escape character, with the character sequence '\\x'
being identical to 'x', even if 'x' is a special char like '['.

Inside character classes, the backslash does not escape anything, and is
treated as an ordianry character.

See regex(7) or
http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html#tag_09_04

diff -r 4c27b26fe4e2 -r 524fbc9f65e9 src/fa.c
--- a/src/fa.c	Sat Aug 09 21:50:15 2008 -0700
+++ b/src/fa.c	Mon Aug 11 14:25:25 2008 -0700
@@ -2515,22 +2515,10 @@
     return c;
 }
 
-static char parse_char(const char **regexp, const char *special) {
-    if (match(regexp, '\\')) {
-        char c = next(regexp);
-        if (special != NULL) {
-            char *f = strchr(special, c);
-            if (f != NULL)
-                return c;
-        }
-        if (c == 'n')
-            return '\n';
-        else if (c == 't')
-            return '\t';
-        else if (c == 'r')
-            return '\r';
-        else
-            return c;
+static char parse_char(const char **regexp, int quoted) {
+    if (quoted && **regexp == '\\') {
+        next(regexp);
+        return next(regexp);
     } else {
         return next(regexp);
     }
@@ -2548,7 +2536,7 @@
         *error = REG_EBRACK;
         goto error;
     }
-    char from = parse_char(regexp, NULL);
+    char from = parse_char(regexp, 0);
     char to = from;
     if (match(regexp, '-')) {
         if (! more(regexp)) {
@@ -2560,7 +2548,7 @@
             add_re_char(re, '-', '-');
             return;
         } else {
-            to = parse_char(regexp, NULL);
+            to = parse_char(regexp, 0);
         }
     }
     add_re_char(re, from, to);
@@ -2606,7 +2594,7 @@
         add_re_char(re, '\n', '\n');
     } else {
         if (more(regexp)) {
-            char c = parse_char(regexp, special_chars);
+            char c = parse_char(regexp, 1);
             re = make_re_char(c);
         }
     }
diff -r 4c27b26fe4e2 -r 524fbc9f65e9 tests/fatest.c
--- a/tests/fatest.c	Sat Aug 09 21:50:15 2008 -0700
+++ b/tests/fatest.c	Mon Aug 11 14:25:25 2008 -0700
@@ -321,6 +321,8 @@
     assertExample(tc, "u+|[0-9]", "u");
     assertExample(tc, "vu+|[0-9]", "0");
     assertExample(tc, "vu{2}|[0-9]", "0");
+    assertExample(tc, "\\[", "[");
+    assertExample(tc, "[\\]", "\\");
 
     assertExample(tc, "\001((\002.)*\001)+\002", "\001\001\002");
     assertExample(tc, "\001((\001.)*\002)+\002", "\001\002\002");




More information about the augeas-devel mailing list