101e04c3fSmrg/* 201e04c3fSmrg * Copyright © 2015 Intel Corporation 301e04c3fSmrg * 401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a 501e04c3fSmrg * copy of this software and associated documentation files (the "Software"), 601e04c3fSmrg * to deal in the Software without restriction, including without limitation 701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the 901e04c3fSmrg * Software is furnished to do so, subject to the following conditions: 1001e04c3fSmrg * 1101e04c3fSmrg * The above copyright notice and this permission notice (including the next 1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the 1301e04c3fSmrg * Software. 1401e04c3fSmrg * 1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 2101e04c3fSmrg * IN THE SOFTWARE. 2201e04c3fSmrg */ 2301e04c3fSmrg 248a1362adSmaya#include <errno.h> 2501e04c3fSmrg#include <string.h> 2601e04c3fSmrg#include "debug.h" 277ec681f3Smrg#include "u_string.h" 2801e04c3fSmrg 2901e04c3fSmrguint64_t 3001e04c3fSmrgparse_debug_string(const char *debug, 3101e04c3fSmrg const struct debug_control *control) 3201e04c3fSmrg{ 3301e04c3fSmrg uint64_t flag = 0; 3401e04c3fSmrg 3501e04c3fSmrg if (debug != NULL) { 3601e04c3fSmrg for (; control->string != NULL; control++) { 3701e04c3fSmrg if (!strcmp(debug, "all")) { 3801e04c3fSmrg flag |= control->flag; 3901e04c3fSmrg 4001e04c3fSmrg } else { 4101e04c3fSmrg const char *s = debug; 4201e04c3fSmrg unsigned n; 4301e04c3fSmrg 4401e04c3fSmrg for (; n = strcspn(s, ", "), *s; s += MAX2(1, n)) { 4501e04c3fSmrg if (strlen(control->string) == n && 4601e04c3fSmrg !strncmp(control->string, s, n)) 4701e04c3fSmrg flag |= control->flag; 4801e04c3fSmrg } 4901e04c3fSmrg } 5001e04c3fSmrg } 5101e04c3fSmrg } 5201e04c3fSmrg 5301e04c3fSmrg return flag; 5401e04c3fSmrg} 5501e04c3fSmrg 568a1362adSmayabool 578a1362adSmayacomma_separated_list_contains(const char *list, const char *s) 588a1362adSmaya{ 598a1362adSmaya assert(list); 608a1362adSmaya const size_t len = strlen(s); 618a1362adSmaya 628a1362adSmaya for (unsigned n; n = strcspn(list, ","), *list; list += MAX2(1, n)) { 638a1362adSmaya if (n == len && !strncmp(list, s, n)) 648a1362adSmaya return true; 658a1362adSmaya } 668a1362adSmaya 678a1362adSmaya return false; 688a1362adSmaya} 698a1362adSmaya 7001e04c3fSmrg/** 7101e04c3fSmrg * Reads an environment variable and interprets its value as a boolean. 7201e04c3fSmrg * 7301e04c3fSmrg * Recognizes 0/false/no and 1/true/yes. Other values result in the default value. 7401e04c3fSmrg */ 7501e04c3fSmrgbool 7601e04c3fSmrgenv_var_as_boolean(const char *var_name, bool default_value) 7701e04c3fSmrg{ 7801e04c3fSmrg const char *str = getenv(var_name); 7901e04c3fSmrg if (str == NULL) 8001e04c3fSmrg return default_value; 8101e04c3fSmrg 8201e04c3fSmrg if (strcmp(str, "1") == 0 || 8301e04c3fSmrg strcasecmp(str, "true") == 0 || 847ec681f3Smrg strcasecmp(str, "y") == 0 || 8501e04c3fSmrg strcasecmp(str, "yes") == 0) { 8601e04c3fSmrg return true; 8701e04c3fSmrg } else if (strcmp(str, "0") == 0 || 8801e04c3fSmrg strcasecmp(str, "false") == 0 || 897ec681f3Smrg strcasecmp(str, "n") == 0 || 9001e04c3fSmrg strcasecmp(str, "no") == 0) { 9101e04c3fSmrg return false; 9201e04c3fSmrg } else { 9301e04c3fSmrg return default_value; 9401e04c3fSmrg } 9501e04c3fSmrg} 968a1362adSmaya 978a1362adSmaya/** 988a1362adSmaya * Reads an environment variable and interprets its value as a unsigned. 998a1362adSmaya */ 1008a1362adSmayaunsigned 1018a1362adSmayaenv_var_as_unsigned(const char *var_name, unsigned default_value) 1028a1362adSmaya{ 1038a1362adSmaya char *str = getenv(var_name); 1048a1362adSmaya if (str) { 1058a1362adSmaya char *end; 1068a1362adSmaya unsigned long result; 1078a1362adSmaya 1088a1362adSmaya errno = 0; 1098a1362adSmaya result = strtoul(str, &end, 0); 1108a1362adSmaya if (errno == 0 && end != str && *end == '\0') 1118a1362adSmaya return result; 1128a1362adSmaya } 1138a1362adSmaya return default_value; 1148a1362adSmaya} 115