utils: Move functions from debug.* to u_debug.*

Add unit tests for debug_get_bool_option and debug_get_num_option
Merge env_var_as_boolean and debug_get_bool_option and implement
 env_var_as_boolean with debug_get_bool_option in a stricter side.
Merge env_var_as_unsigned and debug_get_num_option and implement
 env_var_as_unsigned with debug_get_num_option in a stricter side.
Move debug_control, parse_debug_string, parse_enable_string,
 comma_separated_list_contains from debug.* to u_debug.*

Main changes:
os_get_option() is used instead of getenv() for env_var_as_boolean
 and env_var_as_unsigned;
also debug_get_bool_option() has logic like "true" always if not "false";
env_var_as_boolean() now uses different logic:
 if env variable is neither "true" nor "false" returns the default value,
 we left the second one; but if you want the behavior to be the same as in
 the old version of debug_get_bool_option() use dfault=true

Signed-off-by: Illia Abernikhin <illia.abernikhin@globallogic.com>
Reviewed-by: Yonggang Luo <luoyonggang@gmail.com>
Reviewed-by: Rob Clark <robdclark@chromium.org>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/19336>
This commit is contained in:
Illia Abernikhin
2022-11-01 05:59:26 +08:00
committed by Marge Bot
parent d375a0ff8a
commit 0e47171abe
6 changed files with 279 additions and 135 deletions
+106 -9
View File
@@ -154,6 +154,12 @@ debug_get_option(const char *name, const char *dfault)
}
/**
* Reads an environment variable and interprets its value as a boolean.
* Recognizes 0/n/no/f/false case insensitive as false.
* Recognizes 1/y/yes/t/true case insensitive as true.
* Other values result in the default value.
*/
bool
debug_get_bool_option(const char *name, bool dfault)
{
@@ -162,22 +168,28 @@ debug_get_bool_option(const char *name, bool dfault)
if (str == NULL)
result = dfault;
else if (!strcmp(str, "n"))
result = false;
else if (!strcmp(str, "no"))
result = false;
else if (!strcmp(str, "0"))
result = false;
else if (!strcmp(str, "f"))
else if (!strcasecmp(str, "n"))
result = false;
else if (!strcmp(str, "F"))
else if (!strcasecmp(str, "no"))
result = false;
else if (!strcmp(str, "false"))
else if (!strcasecmp(str, "f"))
result = false;
else if (!strcmp(str, "FALSE"))
else if (!strcasecmp(str, "false"))
result = false;
else
else if (!strcmp(str, "1"))
result = true;
else if (!strcasecmp(str, "y"))
result = true;
else if (!strcasecmp(str, "yes"))
result = true;
else if (!strcasecmp(str, "t"))
result = true;
else if (!strcasecmp(str, "true"))
result = true;
else
result = dfault;
if (debug_get_option_should_print())
debug_printf("%s: %s = %s\n", __FUNCTION__, name,
@@ -408,6 +420,91 @@ debug_dump_flags(const struct debug_named_value *names, unsigned long value)
}
uint64_t
parse_debug_string(const char *debug,
const struct debug_control *control)
{
uint64_t flag = 0;
if (debug != NULL) {
for (; control->string != NULL; control++) {
if (!strcmp(debug, "all")) {
flag |= control->flag;
} else {
const char *s = debug;
unsigned n;
for (; n = strcspn(s, ", "), *s; s += MAX2(1, n)) {
if (strlen(control->string) == n &&
!strncmp(control->string, s, n))
flag |= control->flag;
}
}
}
}
return flag;
}
uint64_t
parse_enable_string(const char *debug,
uint64_t default_value,
const struct debug_control *control)
{
uint64_t flag = default_value;
if (debug != NULL) {
for (; control->string != NULL; control++) {
if (!strcmp(debug, "all")) {
flag |= control->flag;
} else {
const char *s = debug;
unsigned n;
for (; n = strcspn(s, ", "), *s; s += MAX2(1, n)) {
bool enable;
if (s[0] == '+') {
enable = true;
s++; n--;
} else if (s[0] == '-') {
enable = false;
s++; n--;
} else {
enable = true;
}
if (strlen(control->string) == n &&
!strncmp(control->string, s, n)) {
if (enable)
flag |= control->flag;
else
flag &= ~control->flag;
}
}
}
}
}
return flag;
}
bool
comma_separated_list_contains(const char *list, const char *s)
{
assert(list);
const size_t len = strlen(s);
for (unsigned n; n = strcspn(list, ","), *list; list += MAX2(1, n)) {
if (n == len && !strncmp(list, s, n))
return true;
}
return false;
}
#ifdef DEBUG
int fl_indent = 0;