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:
parent
1c092b62db
commit
1104d11429
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user