mirror of
https://github.com/troglobit/finit.git
synced 2026-10-01 05:22:48 +07:00
util: new function unquote()
Refactor to reduce code duplication Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
+3
-12
@@ -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
@@ -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
@@ -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
@@ -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_ */
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user