1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2015 Intel Corporation 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include <errno.h> 25bf215546Sopenharmony_ci#include <string.h> 26bf215546Sopenharmony_ci#include "debug.h" 27bf215546Sopenharmony_ci#include "u_string.h" 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ciuint64_t 30bf215546Sopenharmony_ciparse_debug_string(const char *debug, 31bf215546Sopenharmony_ci const struct debug_control *control) 32bf215546Sopenharmony_ci{ 33bf215546Sopenharmony_ci uint64_t flag = 0; 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_ci if (debug != NULL) { 36bf215546Sopenharmony_ci for (; control->string != NULL; control++) { 37bf215546Sopenharmony_ci if (!strcmp(debug, "all")) { 38bf215546Sopenharmony_ci flag |= control->flag; 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_ci } else { 41bf215546Sopenharmony_ci const char *s = debug; 42bf215546Sopenharmony_ci unsigned n; 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_ci for (; n = strcspn(s, ", "), *s; s += MAX2(1, n)) { 45bf215546Sopenharmony_ci if (strlen(control->string) == n && 46bf215546Sopenharmony_ci !strncmp(control->string, s, n)) 47bf215546Sopenharmony_ci flag |= control->flag; 48bf215546Sopenharmony_ci } 49bf215546Sopenharmony_ci } 50bf215546Sopenharmony_ci } 51bf215546Sopenharmony_ci } 52bf215546Sopenharmony_ci 53bf215546Sopenharmony_ci return flag; 54bf215546Sopenharmony_ci} 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ciuint64_t 57bf215546Sopenharmony_ciparse_enable_string(const char *debug, 58bf215546Sopenharmony_ci uint64_t default_value, 59bf215546Sopenharmony_ci const struct debug_control *control) 60bf215546Sopenharmony_ci{ 61bf215546Sopenharmony_ci uint64_t flag = default_value; 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ci if (debug != NULL) { 64bf215546Sopenharmony_ci for (; control->string != NULL; control++) { 65bf215546Sopenharmony_ci if (!strcmp(debug, "all")) { 66bf215546Sopenharmony_ci flag |= control->flag; 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_ci } else { 69bf215546Sopenharmony_ci const char *s = debug; 70bf215546Sopenharmony_ci unsigned n; 71bf215546Sopenharmony_ci 72bf215546Sopenharmony_ci for (; n = strcspn(s, ", "), *s; s += MAX2(1, n)) { 73bf215546Sopenharmony_ci bool enable; 74bf215546Sopenharmony_ci if (s[0] == '+') { 75bf215546Sopenharmony_ci enable = true; 76bf215546Sopenharmony_ci s++; n--; 77bf215546Sopenharmony_ci } else if (s[0] == '-') { 78bf215546Sopenharmony_ci enable = false; 79bf215546Sopenharmony_ci s++; n--; 80bf215546Sopenharmony_ci } else { 81bf215546Sopenharmony_ci enable = true; 82bf215546Sopenharmony_ci } 83bf215546Sopenharmony_ci if (strlen(control->string) == n && 84bf215546Sopenharmony_ci !strncmp(control->string, s, n)) { 85bf215546Sopenharmony_ci if (enable) 86bf215546Sopenharmony_ci flag |= control->flag; 87bf215546Sopenharmony_ci else 88bf215546Sopenharmony_ci flag &= ~control->flag; 89bf215546Sopenharmony_ci } 90bf215546Sopenharmony_ci } 91bf215546Sopenharmony_ci } 92bf215546Sopenharmony_ci } 93bf215546Sopenharmony_ci } 94bf215546Sopenharmony_ci 95bf215546Sopenharmony_ci return flag; 96bf215546Sopenharmony_ci} 97bf215546Sopenharmony_ci 98bf215546Sopenharmony_cibool 99bf215546Sopenharmony_cicomma_separated_list_contains(const char *list, const char *s) 100bf215546Sopenharmony_ci{ 101bf215546Sopenharmony_ci assert(list); 102bf215546Sopenharmony_ci const size_t len = strlen(s); 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci for (unsigned n; n = strcspn(list, ","), *list; list += MAX2(1, n)) { 105bf215546Sopenharmony_ci if (n == len && !strncmp(list, s, n)) 106bf215546Sopenharmony_ci return true; 107bf215546Sopenharmony_ci } 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci return false; 110bf215546Sopenharmony_ci} 111bf215546Sopenharmony_ci 112bf215546Sopenharmony_ci/** 113bf215546Sopenharmony_ci * Reads an environment variable and interprets its value as a boolean. 114bf215546Sopenharmony_ci * 115bf215546Sopenharmony_ci * Recognizes 0/false/no and 1/true/yes. Other values result in the default value. 116bf215546Sopenharmony_ci */ 117bf215546Sopenharmony_cibool 118bf215546Sopenharmony_cienv_var_as_boolean(const char *var_name, bool default_value) 119bf215546Sopenharmony_ci{ 120bf215546Sopenharmony_ci const char *str = getenv(var_name); 121bf215546Sopenharmony_ci if (str == NULL) 122bf215546Sopenharmony_ci return default_value; 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_ci if (strcmp(str, "1") == 0 || 125bf215546Sopenharmony_ci strcasecmp(str, "true") == 0 || 126bf215546Sopenharmony_ci strcasecmp(str, "y") == 0 || 127bf215546Sopenharmony_ci strcasecmp(str, "yes") == 0) { 128bf215546Sopenharmony_ci return true; 129bf215546Sopenharmony_ci } else if (strcmp(str, "0") == 0 || 130bf215546Sopenharmony_ci strcasecmp(str, "false") == 0 || 131bf215546Sopenharmony_ci strcasecmp(str, "n") == 0 || 132bf215546Sopenharmony_ci strcasecmp(str, "no") == 0) { 133bf215546Sopenharmony_ci return false; 134bf215546Sopenharmony_ci } else { 135bf215546Sopenharmony_ci return default_value; 136bf215546Sopenharmony_ci } 137bf215546Sopenharmony_ci} 138bf215546Sopenharmony_ci 139bf215546Sopenharmony_ci/** 140bf215546Sopenharmony_ci * Reads an environment variable and interprets its value as a unsigned. 141bf215546Sopenharmony_ci */ 142bf215546Sopenharmony_ciunsigned 143bf215546Sopenharmony_cienv_var_as_unsigned(const char *var_name, unsigned default_value) 144bf215546Sopenharmony_ci{ 145bf215546Sopenharmony_ci char *str = getenv(var_name); 146bf215546Sopenharmony_ci if (str) { 147bf215546Sopenharmony_ci char *end; 148bf215546Sopenharmony_ci unsigned long result; 149bf215546Sopenharmony_ci 150bf215546Sopenharmony_ci errno = 0; 151bf215546Sopenharmony_ci result = strtoul(str, &end, 0); 152bf215546Sopenharmony_ci if (errno == 0 && end != str && *end == '\0') 153bf215546Sopenharmony_ci return result; 154bf215546Sopenharmony_ci } 155bf215546Sopenharmony_ci return default_value; 156bf215546Sopenharmony_ci} 157