parse-util: add parse_tristate() and use it everywhere

We parse tristates all the time, let's add an explicit parser for them.
This commit is contained in:
Lennart Poettering
2023-11-01 16:48:42 +01:00
committed by Yu Watanabe
parent 6a4d0efa00
commit b71a721fbc
11 changed files with 47 additions and 84 deletions

View File

@@ -44,6 +44,24 @@ int parse_boolean(const char *v) {
return -EINVAL;
}
int parse_tristate_full(const char *v, const char *third, int *ret) {
int r;
if (isempty(v) || streq_ptr(v, third)) { /* Empty string is always taken as the third/invalid/auto state */
if (ret)
*ret = -1;
} else {
r = parse_boolean(v);
if (r < 0)
return r;
if (ret)
*ret = r;
}
return 0;
}
int parse_pid(const char *s, pid_t* ret_pid) {
unsigned long ul = 0;
pid_t pid;