mirror of
https://github.com/troglobit/finit.git
synced 2026-11-03 13:28:27 +00:00
util: new function unquote()
Refactor to reduce code duplication Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
4 files changed
+26
-28
No files matched your search
+3
-12
@@ -89,18 +89,9 @@ static void cgset(const char *path, char *ctrl, char *prop)
|
|||||||
*val++ = 0;
|
*val++ = 0;
|
||||||
|
|
||||||
/* unquote value, if quoted */
|
/* unquote value, if quoted */
|
||||||
if (val[0] == '"' || val[0] == '\'') {
|
if (unquote(&val, NULL)) {
|
||||||
char q = val[0];
|
errx(1, "Syntax error, unterminated quote in %s/%s.%s=%s", path, ctrl, prop, val);
|
||||||
char *end;
|
return;
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* disallow sneaky relative paths */
|
/* disallow sneaky relative paths */
|
||||||
|
|||||||
+1
-8
@@ -527,14 +527,7 @@ static void parse_env(char *line)
|
|||||||
val++;
|
val++;
|
||||||
|
|
||||||
/* unquote value, if quoted */
|
/* unquote value, if quoted */
|
||||||
if (val[0] == '"' || val[0] == '\'') {
|
unquote(&val, end);
|
||||||
char q = val[0];
|
|
||||||
|
|
||||||
if (*end == q) {
|
|
||||||
val = &val[1];
|
|
||||||
*end = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* find end of key */
|
/* find end of key */
|
||||||
end = key;
|
end = key;
|
||||||
|
|||||||
+1
-8
@@ -460,14 +460,7 @@ static void source_env(svc_t *svc)
|
|||||||
value++;
|
value++;
|
||||||
|
|
||||||
/* unquote value, if quoted */
|
/* unquote value, if quoted */
|
||||||
if (value[0] == '"' || value[0] == '\'') {
|
unquote(&value, end);
|
||||||
char q = value[0];
|
|
||||||
|
|
||||||
if (*end == q) {
|
|
||||||
value = &value[1];
|
|
||||||
*end = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* find end of key */
|
/* find end of key */
|
||||||
end = 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_ */
|
#endif /* FINIT_UTIL_H_ */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user