]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: introduce virBufferEscapeRegex
authorPavel Hrdina <phrdina@redhat.com>
Fri, 12 May 2017 12:26:09 +0000 (14:26 +0200)
committerPavel Hrdina <phrdina@redhat.com>
Fri, 12 May 2017 14:54:33 +0000 (16:54 +0200)
Add a helper to escape all possible meta-characters used for
POSIX extended regular expressions.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
src/libvirt_private.syms
src/util/virbuffer.c
src/util/virbuffer.h
tests/virbuftest.c

index d32c6e75491a36ae742a9701f7ec7cddabf344cb..bbe283529b617cf7c87fe11dd0cbea26e61f7630 100644 (file)
@@ -1312,6 +1312,7 @@ virBufferCurrentContent;
 virBufferError;
 virBufferEscape;
 virBufferEscapeN;
+virBufferEscapeRegex;
 virBufferEscapeSexpr;
 virBufferEscapeShell;
 virBufferEscapeString;
index 80c8e289d45785855c3c6d6f39759a166a588e78..f07b119c0fb95d98b590fbcbe9bd645762fafaf5 100644 (file)
@@ -555,6 +555,25 @@ virBufferEscapeSexpr(virBufferPtr buf,
     virBufferEscape(buf, '\\', "\\'", format, str);
 }
 
+/**
+ * virBufferEscapeRegex:
+ * @buf: the buffer to append to
+ * @format: a printf like format string but with only one %s parameter
+ * @str: the string argument which needs to be escaped
+ *
+ * Do a formatted print with a single string to a buffer.  The @str is
+ * escaped to avoid using POSIX extended regular expression meta-characters.
+ * Escaping is not applied to characters specified in @format. Auto
+ * indentation may be applied.
+ */
+void
+virBufferEscapeRegex(virBufferPtr buf,
+                     const char *format,
+                     const char *str)
+{
+    virBufferEscape(buf, '\\', "^$.|?*+()[]{}\\", format, str);
+}
+
 /**
  * virBufferEscape:
  * @buf: the buffer to append to
index d1b64ca3a3decb565c1af33eb66fed62fd22227e..7a7014aa70ab4f61f048e8846fa3a5aa1f2c5962 100644 (file)
@@ -88,6 +88,9 @@ void virBufferEscapeString(virBufferPtr buf, const char *format,
                            const char *str);
 void virBufferEscapeSexpr(virBufferPtr buf, const char *format,
                           const char *str);
+void virBufferEscapeRegex(virBufferPtr buf,
+                          const char *format,
+                          const char *str);
 void virBufferEscapeShell(virBufferPtr buf, const char *str);
 void virBufferURIEncodeString(virBufferPtr buf, const char *str);
 
index 5905fee7d41d28697abdc6f3713155c02f812c25..940e4c1c617d4f463bee212d5ea0b29e0fcd869d 100644 (file)
@@ -404,6 +404,35 @@ testBufEscapeN(const void *opaque)
 }
 
 
+static int
+testBufEscapeRegex(const void *opaque)
+{
+    const struct testBufAddStrData *data = opaque;
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    char *actual;
+    int ret = -1;
+
+    virBufferEscapeRegex(&buf, "%s", data->data);
+
+    if (!(actual = virBufferContentAndReset(&buf))) {
+        VIR_TEST_DEBUG("testBufEscapeN: buf is empty");
+        goto cleanup;
+    }
+
+    if (STRNEQ_NULLABLE(actual, data->expect)) {
+        VIR_TEST_DEBUG("testBufEscapeN: Strings don't match:\n");
+        virTestDifference(stderr, data->expect, actual);
+        goto cleanup;
+    }
+
+    ret = 0;
+
+ cleanup:
+    VIR_FREE(actual);
+    return ret;
+}
+
+
 static int
 testBufSetIndent(const void *opaque ATTRIBUTE_UNUSED)
 {
@@ -492,6 +521,17 @@ mymain(void)
     DO_TEST_ESCAPEN("equal=escape", "equal\\=escape");
     DO_TEST_ESCAPEN("comma,equal=escape", "comma,,equal\\=escape");
 
+#define DO_TEST_ESCAPE_REGEX(data, expect)                                  \
+    do {                                                                    \
+        struct testBufAddStrData info = { data, expect };                   \
+        if (virTestRun("Buf: EscapeRegex", testBufEscapeRegex, &info) < 0)  \
+            ret = -1;                                                       \
+    } while (0)
+
+    DO_TEST_ESCAPE_REGEX("noescape", "noescape");
+    DO_TEST_ESCAPE_REGEX("^$.|?*+()[]{}\\",
+                         "\\^\\$\\.\\|\\?\\*\\+\\(\\)\\[\\]\\{\\}\\\\");
+
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }