module/script: parse booleans correctly

User is expected to send ‘false’ or ‘true’. But we were parsing the
value using `strtol()`. This caused all bools to be false, since
`strtol()` would always return 0.
This commit is contained in:
Daniel Eklöf 2020-10-30 16:28:48 +01:00
parent 1e5a1d0341
commit f0a34d0055
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F

View file

@ -67,6 +67,16 @@ content(struct module *mod)
return e; return e;
} }
static bool
str_to_bool(const char *s)
{
return strcasecmp(s, "on") == 0 ||
strcasecmp(s, "true") == 0 ||
strcasecmp(s, "yes") == 0 ||
strtoul(s, NULL, 0) > 0;
}
static struct tag * static struct tag *
process_line(struct module *mod, const char *line, size_t len) process_line(struct module *mod, const char *line, size_t len)
{ {
@ -112,7 +122,7 @@ process_line(struct module *mod, const char *line, size_t len)
tag = tag_new_int(mod, name, strtol(value, NULL, 0)); tag = tag_new_int(mod, name, strtol(value, NULL, 0));
else if (type_len == 4 && memcmp(type, "bool", 4) == 0) else if (type_len == 4 && memcmp(type, "bool", 4) == 0)
tag = tag_new_bool(mod, name, strtol(value, NULL, 0)); tag = tag_new_bool(mod, name, str_to_bool(value));
else if (type_len == 5 && memcmp(type, "float", 5) == 0) else if (type_len == 5 && memcmp(type, "float", 5) == 0)
tag = tag_new_float(mod, name, strtod(value, NULL)); tag = tag_new_float(mod, name, strtod(value, NULL));