/*1* Copyright © 2015 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include <errno.h>24#include <string.h>25#include "debug.h"26#include "u_string.h"2728uint64_t29parse_debug_string(const char *debug,30const struct debug_control *control)31{32uint64_t flag = 0;3334if (debug != NULL) {35for (; control->string != NULL; control++) {36if (!strcmp(debug, "all")) {37flag |= control->flag;3839} else {40const char *s = debug;41unsigned n;4243for (; n = strcspn(s, ", "), *s; s += MAX2(1, n)) {44if (strlen(control->string) == n &&45!strncmp(control->string, s, n))46flag |= control->flag;47}48}49}50}5152return flag;53}5455bool56comma_separated_list_contains(const char *list, const char *s)57{58assert(list);59const size_t len = strlen(s);6061for (unsigned n; n = strcspn(list, ","), *list; list += MAX2(1, n)) {62if (n == len && !strncmp(list, s, n))63return true;64}6566return false;67}6869/**70* Reads an environment variable and interprets its value as a boolean.71*72* Recognizes 0/false/no and 1/true/yes. Other values result in the default value.73*/74bool75env_var_as_boolean(const char *var_name, bool default_value)76{77const char *str = getenv(var_name);78if (str == NULL)79return default_value;8081if (strcmp(str, "1") == 0 ||82strcasecmp(str, "true") == 0 ||83strcasecmp(str, "y") == 0 ||84strcasecmp(str, "yes") == 0) {85return true;86} else if (strcmp(str, "0") == 0 ||87strcasecmp(str, "false") == 0 ||88strcasecmp(str, "n") == 0 ||89strcasecmp(str, "no") == 0) {90return false;91} else {92return default_value;93}94}9596/**97* Reads an environment variable and interprets its value as a unsigned.98*/99unsigned100env_var_as_unsigned(const char *var_name, unsigned default_value)101{102char *str = getenv(var_name);103if (str) {104char *end;105unsigned long result;106107errno = 0;108result = strtoul(str, &end, 0);109if (errno == 0 && end != str && *end == '\0')110return result;111}112return default_value;113}114115116