util: new function unquote()

Refactor to reduce code duplication

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2025-07-02 14:28:10 +02:00
parent ecb690c9ec
commit 6f0ba22a0f
4 changed files with 26 additions and 28 deletions
+3 -12
View File
@@ -89,18 +89,9 @@ static void cgset(const char *path, char *ctrl, char *prop)
*val++ = 0;
/* unquote value, if quoted */
if (val[0] == '"' || val[0] == '\'') {
char q = val[0];
char *end;
end = strchr(&val[1], q);
if (!end) {
errx(1, "Syntax error, unterminated quote in %s/%s.%s=%s", path, ctrl, prop, val);
return;
}
val++;
*end = 0;
if (unquote(&val, NULL)) {
errx(1, "Syntax error, unterminated quote in %s/%s.%s=%s", path, ctrl, prop, val);
return;
}
/* disallow sneaky relative paths */
+1 -8
View File
@@ -527,14 +527,7 @@ static void parse_env(char *line)
val++;
/* unquote value, if quoted */
if (val[0] == '"' || val[0] == '\'') {
char q = val[0];
if (*end == q) {
val = &val[1];
*end = 0;
}
}
unquote(&val, end);
/* find end of key */
end = key;
+1 -8
View File
@@ -460,14 +460,7 @@ static void source_env(svc_t *svc)
value++;
/* unquote value, if quoted */
if (value[0] == '"' || value[0] == '\'') {
char q = value[0];
if (*end == q) {
value = &value[1];
*end = 0;
}
}
unquote(&value, end);
/* find end of key */
end = key;
+21
View File
@@ -165,6 +165,27 @@ static inline void hexdump(const char *buf, size_t len)
}
}
/* drop quotes from a string at point, if any */
static inline int unquote(char **str, char *end)
{
char *ptr = *str;
/* unquote value, if quoted */
if (ptr[0] == '"' || ptr[0] == '\'') {
char q = ptr[0];
if (!end && !(end = strchr(&ptr[1], q)))
return -1;
if (*end == q) {
*str = &ptr[1];
*end = 0;
}
}
return 0;
}
#endif /* FINIT_UTIL_H_ */
/**