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