mirror of
https://github.com/systemd/systemd-stable.git
synced 2024-12-23 17:34:00 +03:00
basic/extract-word: rename flag
The flag enables "relaxed mode" for all kinds of unescaping, not just c-unescaping.
This commit is contained in:
parent
76c4e48ee6
commit
3141089f53
@ -69,14 +69,14 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
|
||||
return -ENOMEM;
|
||||
|
||||
if (c == 0) {
|
||||
if ((flags & EXTRACT_CUNESCAPE_RELAX) &&
|
||||
if ((flags & EXTRACT_UNESCAPE_RELAX) &&
|
||||
(quote == 0 || flags & EXTRACT_RELAX)) {
|
||||
/* If we find an unquoted trailing backslash and we're in
|
||||
* EXTRACT_CUNESCAPE_RELAX mode, keep it verbatim in the
|
||||
* EXTRACT_UNESCAPE_RELAX mode, keep it verbatim in the
|
||||
* output.
|
||||
*
|
||||
* Unbalanced quotes will only be allowed in EXTRACT_RELAX
|
||||
* mode, EXTRACT_CUNESCAPE_RELAX mode does not allow them.
|
||||
* mode, EXTRACT_UNESCAPE_RELAX mode does not allow them.
|
||||
*/
|
||||
s[sz++] = '\\';
|
||||
goto finish_force_terminate;
|
||||
@ -105,7 +105,7 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
|
||||
(strchr(separators, **p) || **p == '\\'))
|
||||
/* An escaped separator char or the escape char itself */
|
||||
s[sz++] = c;
|
||||
else if (flags & EXTRACT_CUNESCAPE_RELAX) {
|
||||
else if (flags & EXTRACT_UNESCAPE_RELAX) {
|
||||
s[sz++] = '\\';
|
||||
s[sz++] = c;
|
||||
} else
|
||||
@ -196,7 +196,7 @@ int extract_first_word_and_warn(
|
||||
const char *rvalue) {
|
||||
|
||||
/* Try to unquote it, if it fails, warn about it and try again
|
||||
* but this time using EXTRACT_CUNESCAPE_RELAX to keep the
|
||||
* but this time using EXTRACT_UNESCAPE_RELAX to keep the
|
||||
* backslashes verbatim in invalid escape sequences. */
|
||||
|
||||
const char *save;
|
||||
@ -207,11 +207,11 @@ int extract_first_word_and_warn(
|
||||
if (r >= 0)
|
||||
return r;
|
||||
|
||||
if (r == -EINVAL && !(flags & EXTRACT_CUNESCAPE_RELAX)) {
|
||||
if (r == -EINVAL && !(flags & EXTRACT_UNESCAPE_RELAX)) {
|
||||
|
||||
/* Retry it with EXTRACT_CUNESCAPE_RELAX. */
|
||||
/* Retry it with EXTRACT_UNESCAPE_RELAX. */
|
||||
*p = save;
|
||||
r = extract_first_word(p, ret, separators, flags|EXTRACT_CUNESCAPE_RELAX);
|
||||
r = extract_first_word(p, ret, separators, flags|EXTRACT_UNESCAPE_RELAX);
|
||||
if (r >= 0) {
|
||||
/* It worked this time, hence it must have been an invalid escape sequence. */
|
||||
log_syntax(unit, LOG_WARNING, filename, line, EINVAL, "Ignoring unknown escape sequences: \"%s\"", *ret);
|
||||
|
@ -6,7 +6,7 @@
|
||||
typedef enum ExtractFlags {
|
||||
EXTRACT_RELAX = 1 << 0, /* Allow unbalanced quote and eat up trailing backslash. */
|
||||
EXTRACT_CUNESCAPE = 1 << 1, /* Unescape known escape sequences. */
|
||||
EXTRACT_CUNESCAPE_RELAX = 1 << 2, /* Allow and keep unknown escape sequences, allow and keep trailing backslash. */
|
||||
EXTRACT_UNESCAPE_RELAX = 1 << 2, /* Allow and keep unknown escape sequences, allow and keep trailing backslash. */
|
||||
EXTRACT_UNESCAPE_SEPARATORS = 1 << 3, /* Unescape separators (those specified, or whitespace by default). */
|
||||
EXTRACT_UNQUOTE = 1 << 4, /* Remove quoting with "" and ''. */
|
||||
EXTRACT_DONT_COALESCE_SEPARATORS = 1 << 5, /* Don't treat multiple adjacent separators as one */
|
||||
|
@ -348,7 +348,7 @@ int config_parse_dnssd_txt(
|
||||
int r;
|
||||
|
||||
r = extract_first_word(&rvalue, &word, NULL,
|
||||
EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX);
|
||||
EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_RELAX);
|
||||
if (r == 0)
|
||||
break;
|
||||
if (r == -ENOMEM)
|
||||
|
@ -172,19 +172,19 @@ static void test_extract_first_word(void) {
|
||||
assert_se(isempty(p));
|
||||
|
||||
p = original = "fooo\\";
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX) > 0);
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNESCAPE_RELAX) > 0);
|
||||
assert_se(streq(t, "fooo\\"));
|
||||
free(t);
|
||||
assert_se(isempty(p));
|
||||
|
||||
p = original = "fooo\\";
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNESCAPE_RELAX|EXTRACT_RELAX) > 0);
|
||||
assert_se(streq(t, "fooo\\"));
|
||||
free(t);
|
||||
assert_se(isempty(p));
|
||||
|
||||
p = original = "fooo\\";
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_RELAX) > 0);
|
||||
assert_se(streq(t, "fooo\\"));
|
||||
free(t);
|
||||
assert_se(isempty(p));
|
||||
@ -230,17 +230,17 @@ static void test_extract_first_word(void) {
|
||||
assert_se(isempty(p));
|
||||
|
||||
p = original = "\"foo\\";
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE_RELAX) == -EINVAL);
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_UNESCAPE_RELAX) == -EINVAL);
|
||||
assert_se(p == original + 5);
|
||||
|
||||
p = original = "\"foo\\";
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_UNESCAPE_RELAX|EXTRACT_RELAX) > 0);
|
||||
assert_se(streq(t, "foo\\"));
|
||||
free(t);
|
||||
assert_se(isempty(p));
|
||||
|
||||
p = original = "\"foo\\";
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_RELAX|EXTRACT_RELAX) > 0);
|
||||
assert_se(streq(t, "foo\\"));
|
||||
free(t);
|
||||
assert_se(isempty(p));
|
||||
@ -252,13 +252,13 @@ static void test_extract_first_word(void) {
|
||||
assert_se(p == original + 10);
|
||||
|
||||
p = original = "fooo\\ bar quux";
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX) > 0);
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNESCAPE_RELAX) > 0);
|
||||
assert_se(streq(t, "fooo bar"));
|
||||
free(t);
|
||||
assert_se(p == original + 10);
|
||||
|
||||
p = original = "fooo\\ bar quux";
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNESCAPE_RELAX|EXTRACT_RELAX) > 0);
|
||||
assert_se(streq(t, "fooo bar"));
|
||||
free(t);
|
||||
assert_se(p == original + 10);
|
||||
@ -268,7 +268,7 @@ static void test_extract_first_word(void) {
|
||||
assert_se(p == original + 5);
|
||||
|
||||
p = original = "fooo\\ bar quux";
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_RELAX) > 0);
|
||||
assert_se(streq(t, "fooo\\ bar"));
|
||||
free(t);
|
||||
assert_se(p == original + 10);
|
||||
@ -278,13 +278,13 @@ static void test_extract_first_word(void) {
|
||||
assert_se(p == original + 1);
|
||||
|
||||
p = original = "\\w+@\\K[\\d.]+";
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_RELAX) > 0);
|
||||
assert_se(streq(t, "\\w+@\\K[\\d.]+"));
|
||||
free(t);
|
||||
assert_se(isempty(p));
|
||||
|
||||
p = original = "\\w+\\b";
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
|
||||
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_RELAX) > 0);
|
||||
assert_se(streq(t, "\\w+\b"));
|
||||
free(t);
|
||||
assert_se(isempty(p));
|
||||
|
@ -333,12 +333,12 @@ static void test_strv_split(void) {
|
||||
l = strv_free_erase(l);
|
||||
|
||||
assert_se(strv_split_full(&l, " 'one' \" two\t three \"' four five", NULL,
|
||||
EXTRACT_UNQUOTE | EXTRACT_RELAX | EXTRACT_CUNESCAPE_RELAX) == 2);
|
||||
EXTRACT_UNQUOTE | EXTRACT_RELAX | EXTRACT_UNESCAPE_RELAX) == 2);
|
||||
assert_se(strv_equal(l, (char**) input_table_quoted_joined));
|
||||
|
||||
l = strv_free_erase(l);
|
||||
|
||||
assert_se(strv_split_full(&l, "\\", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX | EXTRACT_CUNESCAPE_RELAX) == 1);
|
||||
assert_se(strv_split_full(&l, "\\", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX | EXTRACT_UNESCAPE_RELAX) == 1);
|
||||
assert_se(strv_equal(l, STRV_MAKE("\\")));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user