From 4034a06ddb82ec9868cd52496fef2f5faa25575f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 23 Mar 2015 18:55:36 +0700 Subject: util: rework word parsing and c unescaping code When parsing words from input files, optionally automatically unescape the passed strings, controllable via a new flags parameter. Make use of this in tmpfiles, and port everything else over, too. This improves parsing quite a bit, since we no longer have to process the same string multiple times with different calls, where an earlier call might corrupt the input for a later call. --- src/test/test-strv.c | 6 +++--- src/test/test-util.c | 50 +++++++++++++++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 22 deletions(-) (limited to 'src/test') diff --git a/src/test/test-strv.c b/src/test/test-strv.c index 5ae929c3f8..d5ea2b3fab 100644 --- a/src/test/test-strv.c +++ b/src/test/test-strv.c @@ -165,7 +165,7 @@ static void test_strv_quote_unquote(const char* const *split, const char *quoted assert_se(p); assert_se(streq(p, quoted)); - r = strv_split_quoted(&s, quoted, false); + r = strv_split_quoted(&s, quoted, 0); assert_se(r == 0); assert_se(s); STRV_FOREACH(t, s) { @@ -182,7 +182,7 @@ static void test_strv_unquote(const char *quoted, char **list) { char **t; int r; - r = strv_split_quoted(&s, quoted, false); + r = strv_split_quoted(&s, quoted, 0); assert_se(r == 0); assert_se(s); j = strv_join(s, " | "); @@ -199,7 +199,7 @@ static void test_invalid_unquote(const char *quoted) { char **s = NULL; int r; - r = strv_split_quoted(&s, quoted, false); + r = strv_split_quoted(&s, quoted, 0); assert_se(s == NULL); assert_se(r == -EINVAL); } diff --git a/src/test/test-util.c b/src/test/test-util.c index 9515a8cbf1..d32ddd3a68 100644 --- a/src/test/test-util.c +++ b/src/test/test-util.c @@ -1283,64 +1283,76 @@ static void test_unquote_first_word(void) { char *t; p = original = "foobar waldo"; - assert_se(unquote_first_word(&p, &t, false) > 0); + assert_se(unquote_first_word(&p, &t, 0) > 0); assert_se(streq(t, "foobar")); free(t); assert_se(p == original + 7); - assert_se(unquote_first_word(&p, &t, false) > 0); + assert_se(unquote_first_word(&p, &t, 0) > 0); assert_se(streq(t, "waldo")); free(t); assert_se(p == original + 12); - assert_se(unquote_first_word(&p, &t, false) == 0); + assert_se(unquote_first_word(&p, &t, 0) == 0); assert_se(!t); assert_se(p == original + 12); p = original = "\"foobar\" \'waldo\'"; - assert_se(unquote_first_word(&p, &t, false) > 0); + assert_se(unquote_first_word(&p, &t, 0) > 0); assert_se(streq(t, "foobar")); free(t); assert_se(p == original + 9); - assert_se(unquote_first_word(&p, &t, false) > 0); + assert_se(unquote_first_word(&p, &t, 0) > 0); assert_se(streq(t, "waldo")); free(t); assert_se(p == original + 16); - assert_se(unquote_first_word(&p, &t, false) == 0); + assert_se(unquote_first_word(&p, &t, 0) == 0); assert_se(!t); assert_se(p == original + 16); p = original = "\""; - assert_se(unquote_first_word(&p, &t, false) == -EINVAL); + assert_se(unquote_first_word(&p, &t, 0) == -EINVAL); assert_se(p == original + 1); p = original = "\'"; - assert_se(unquote_first_word(&p, &t, false) == -EINVAL); + assert_se(unquote_first_word(&p, &t, 0) == -EINVAL); assert_se(p == original + 1); p = original = "\'fooo"; - assert_se(unquote_first_word(&p, &t, false) == -EINVAL); + assert_se(unquote_first_word(&p, &t, 0) == -EINVAL); assert_se(p == original + 5); p = original = "\'fooo"; - assert_se(unquote_first_word(&p, &t, true) > 0); + assert_se(unquote_first_word(&p, &t, UNQUOTE_RELAX) > 0); assert_se(streq(t, "fooo")); free(t); assert_se(p == original + 5); p = original = "yay\'foo\'bar"; - assert_se(unquote_first_word(&p, &t, false) > 0); + assert_se(unquote_first_word(&p, &t, 0) > 0); assert_se(streq(t, "yayfoobar")); free(t); assert_se(p == original + 11); p = original = " foobar "; - assert_se(unquote_first_word(&p, &t, false) > 0); + assert_se(unquote_first_word(&p, &t, 0) > 0); assert_se(streq(t, "foobar")); free(t); assert_se(p == original + 12); + + p = original = " foo\\ba\\x6ar "; + assert_se(unquote_first_word(&p, &t, UNQUOTE_CUNESCAPE) > 0); + assert_se(streq(t, "foo\ba\x6ar")); + free(t); + assert_se(p == original + 13); + + p = original = " foo\\ba\\x6ar "; + assert_se(unquote_first_word(&p, &t, 0) > 0); + assert_se(streq(t, "foobax6ar")); + free(t); + assert_se(p == original + 13); } static void test_unquote_many_words(void) { @@ -1348,7 +1360,7 @@ static void test_unquote_many_words(void) { char *a, *b, *c; p = original = "foobar waldi piep"; - assert_se(unquote_many_words(&p, &a, &b, &c, NULL) == 3); + assert_se(unquote_many_words(&p, 0, &a, &b, &c, NULL) == 3); assert_se(p == original + 17); assert_se(streq_ptr(a, "foobar")); assert_se(streq_ptr(b, "waldi")); @@ -1358,7 +1370,7 @@ static void test_unquote_many_words(void) { free(c); p = original = "'foobar' wa\"ld\"i "; - assert_se(unquote_many_words(&p, &a, &b, &c, NULL) == 2); + assert_se(unquote_many_words(&p, 0, &a, &b, &c, NULL) == 2); assert_se(p == original + 19); assert_se(streq_ptr(a, "foobar")); assert_se(streq_ptr(b, "waldi")); @@ -1367,31 +1379,31 @@ static void test_unquote_many_words(void) { free(b); p = original = ""; - assert_se(unquote_many_words(&p, &a, &b, &c, NULL) == 0); + assert_se(unquote_many_words(&p, 0, &a, &b, &c, NULL) == 0); assert_se(p == original); assert_se(streq_ptr(a, NULL)); assert_se(streq_ptr(b, NULL)); assert_se(streq_ptr(c, NULL)); p = original = " "; - assert_se(unquote_many_words(&p, &a, &b, &c, NULL) == 0); + assert_se(unquote_many_words(&p, 0, &a, &b, &c, NULL) == 0); assert_se(p == original+2); assert_se(streq_ptr(a, NULL)); assert_se(streq_ptr(b, NULL)); assert_se(streq_ptr(c, NULL)); p = original = "foobar"; - assert_se(unquote_many_words(&p, NULL) == 0); + assert_se(unquote_many_words(&p, 0, NULL) == 0); assert_se(p == original); p = original = "foobar waldi"; - assert_se(unquote_many_words(&p, &a, NULL) == 1); + assert_se(unquote_many_words(&p, 0, &a, NULL) == 1); assert_se(p == original+7); assert_se(streq_ptr(a, "foobar")); free(a); p = original = " foobar "; - assert_se(unquote_many_words(&p, &a, NULL) == 1); + assert_se(unquote_many_words(&p, 0, &a, NULL) == 1); assert_se(p == original+15); assert_se(streq_ptr(a, "foobar")); free(a); -- cgit v1.2.3