1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-26 14:04:03 +03:00

extract-word: introduce EXTRACT_KEEP_QUOTE flag

This commit is contained in:
Yu Watanabe 2021-06-21 21:01:54 +09:00
parent 1c092b62db
commit 1104d11429
2 changed files with 26 additions and 16 deletions

View File

@ -27,6 +27,7 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
assert(p);
assert(ret);
assert(!FLAGS_SET(flags, EXTRACT_KEEP_QUOTE | EXTRACT_UNQUOTE));
/* Bail early if called after last value or with no input */
if (!*p)
@ -123,25 +124,30 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
return -EINVAL;
} else if (c == quote) { /* found the end quote */
quote = 0;
break;
if (flags & EXTRACT_UNQUOTE)
break;
} else if (c == '\\' && !(flags & EXTRACT_RETAIN_ESCAPE)) {
backslash = true;
break;
} else {
if (!GREEDY_REALLOC(s, sz+2))
return -ENOMEM;
s[sz++] = c;
}
if (!GREEDY_REALLOC(s, sz+2))
return -ENOMEM;
s[sz++] = c;
if (quote == 0)
break;
}
} else {
for (;; (*p)++, c = **p) {
if (c == 0)
goto finish_force_terminate;
else if (IN_SET(c, '\'', '"') && (flags & EXTRACT_UNQUOTE)) {
else if (IN_SET(c, '\'', '"') && (flags & (EXTRACT_KEEP_QUOTE | EXTRACT_UNQUOTE))) {
quote = c;
break;
if (flags & EXTRACT_UNQUOTE)
break;
} else if (c == '\\' && !(flags & EXTRACT_RETAIN_ESCAPE)) {
backslash = true;
break;
@ -159,12 +165,15 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
}
goto finish;
} else {
if (!GREEDY_REALLOC(s, sz+2))
return -ENOMEM;
s[sz++] = c;
}
if (!GREEDY_REALLOC(s, sz+2))
return -ENOMEM;
s[sz++] = c;
if (quote != 0)
break;
}
}
}

View File

@ -8,9 +8,10 @@ typedef enum ExtractFlags {
EXTRACT_CUNESCAPE = 1 << 1, /* Unescape known escape sequences. */
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 */
EXTRACT_RETAIN_ESCAPE = 1 << 6, /* Treat escape character '\' as any other character without special meaning */
EXTRACT_KEEP_QUOTE = 1 << 4, /* Ignore separators in quoting with "" and ''. */
EXTRACT_UNQUOTE = 1 << 5, /* Ignore separators in quoting with "" and '', and remove the quotes. */
EXTRACT_DONT_COALESCE_SEPARATORS = 1 << 6, /* Don't treat multiple adjacent separators as one */
EXTRACT_RETAIN_ESCAPE = 1 << 7, /* Treat escape character '\' as any other character without special meaning */
/* Note that if no flags are specified, escaped escape characters will be silently stripped. */
} ExtractFlags;