X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Futil%2Fdebug.c;h=89ae61310746455ecb037a6a0dda837eb320c223;hb=e8cdf125112934b589d9682239e46bf196bd9de1;hp=3729ce85670a41186a2105a38106f1b05a23301e;hpb=fc2a66cfcddea34af0e93dd2221ae1fd3fdd9e87;p=mesa.git diff --git a/src/util/debug.c b/src/util/debug.c index 3729ce85670..89ae6131074 100644 --- a/src/util/debug.c +++ b/src/util/debug.c @@ -21,9 +21,10 @@ * IN THE SOFTWARE. */ +#include #include -#include "main/macros.h" #include "debug.h" +#include "u_string.h" uint64_t parse_debug_string(const char *debug, @@ -51,3 +52,63 @@ parse_debug_string(const char *debug, 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; +} + +/** + * Reads an environment variable and interprets its value as a boolean. + * + * Recognizes 0/false/no and 1/true/yes. Other values result in the default value. + */ +bool +env_var_as_boolean(const char *var_name, bool default_value) +{ + const char *str = getenv(var_name); + if (str == NULL) + return default_value; + + if (strcmp(str, "1") == 0 || + strcasecmp(str, "true") == 0 || + strcasecmp(str, "y") == 0 || + strcasecmp(str, "yes") == 0) { + return true; + } else if (strcmp(str, "0") == 0 || + strcasecmp(str, "false") == 0 || + strcasecmp(str, "n") == 0 || + strcasecmp(str, "no") == 0) { + return false; + } else { + return default_value; + } +} + +/** + * Reads an environment variable and interprets its value as a unsigned. + */ +unsigned +env_var_as_unsigned(const char *var_name, unsigned default_value) +{ + char *str = getenv(var_name); + if (str) { + char *end; + unsigned long result; + + errno = 0; + result = strtoul(str, &end, 0); + if (errno == 0 && end != str && *end == '\0') + return result; + } + return default_value; +}