scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 8 | #include "SkCommandLineFlags.h" |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 9 | #include "SkTDArray.h" |
halcanary | 03758b8 | 2015-01-18 10:39:25 -0800 | [diff] [blame] | 10 | #include "SkTSort.h" |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 11 | |
mtklein | 77a8396 | 2014-06-20 08:24:56 -0700 | [diff] [blame] | 12 | DEFINE_bool(undefok, false, "Silently ignore unknown flags instead of crashing."); |
commit-bot@chromium.org | ffc224f | 2014-03-25 21:00:02 +0000 | [diff] [blame] | 13 | |
scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 14 | bool SkFlagInfo::CreateStringFlag(const char* name, const char* shortName, |
| 15 | SkCommandLineFlags::StringArray* pStrings, |
| 16 | const char* defaultValue, const char* helpString) { |
| 17 | SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, shortName, kString_FlagType, helpString)); |
| 18 | info->fDefaultString.set(defaultValue); |
| 19 | |
| 20 | info->fStrings = pStrings; |
| 21 | SetDefaultStrings(pStrings, defaultValue); |
| 22 | return true; |
| 23 | } |
| 24 | |
| 25 | void SkFlagInfo::SetDefaultStrings(SkCommandLineFlags::StringArray* pStrings, |
| 26 | const char* defaultValue) { |
| 27 | pStrings->reset(); |
scroggo@google.com | d041901 | 2013-04-24 19:37:52 +0000 | [diff] [blame] | 28 | if (NULL == defaultValue) { |
| 29 | return; |
| 30 | } |
scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 31 | // If default is "", leave the array empty. |
| 32 | size_t defaultLength = strlen(defaultValue); |
| 33 | if (defaultLength > 0) { |
| 34 | const char* const defaultEnd = defaultValue + defaultLength; |
| 35 | const char* begin = defaultValue; |
| 36 | while (true) { |
| 37 | while (begin < defaultEnd && ' ' == *begin) { |
| 38 | begin++; |
| 39 | } |
| 40 | if (begin < defaultEnd) { |
| 41 | const char* end = begin + 1; |
| 42 | while (end < defaultEnd && ' ' != *end) { |
| 43 | end++; |
| 44 | } |
| 45 | size_t length = end - begin; |
| 46 | pStrings->append(begin, length); |
| 47 | begin = end + 1; |
| 48 | } else { |
| 49 | break; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 55 | static bool string_is_in(const char* target, const char* set[], size_t len) { |
| 56 | for (size_t i = 0; i < len; i++) { |
| 57 | if (0 == strcmp(target, set[i])) { |
| 58 | return true; |
| 59 | } |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Check to see whether string represents a boolean value. |
| 66 | * @param string C style string to parse. |
| 67 | * @param result Pointer to a boolean which will be set to the value in the string, if the |
| 68 | * string represents a boolean. |
| 69 | * @param boolean True if the string represents a boolean, false otherwise. |
| 70 | */ |
| 71 | static bool parse_bool_arg(const char* string, bool* result) { |
| 72 | static const char* trueValues[] = { "1", "TRUE", "true" }; |
| 73 | if (string_is_in(string, trueValues, SK_ARRAY_COUNT(trueValues))) { |
| 74 | *result = true; |
| 75 | return true; |
| 76 | } |
| 77 | static const char* falseValues[] = { "0", "FALSE", "false" }; |
| 78 | if (string_is_in(string, falseValues, SK_ARRAY_COUNT(falseValues))) { |
| 79 | *result = false; |
| 80 | return true; |
| 81 | } |
| 82 | SkDebugf("Parameter \"%s\" not supported.\n", string); |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | bool SkFlagInfo::match(const char* string) { |
| 87 | if (SkStrStartsWith(string, '-') && strlen(string) > 1) { |
| 88 | string++; |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 89 | const SkString* compareName; |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 90 | if (SkStrStartsWith(string, '-') && strlen(string) > 1) { |
| 91 | string++; |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 92 | // There were two dashes. Compare against full name. |
| 93 | compareName = &fName; |
| 94 | } else { |
| 95 | // One dash. Compare against the short name. |
| 96 | compareName = &fShortName; |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 97 | } |
| 98 | if (kBool_FlagType == fFlagType) { |
| 99 | // In this case, go ahead and set the value. |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 100 | if (compareName->equals(string)) { |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 101 | *fBoolValue = true; |
| 102 | return true; |
| 103 | } |
| 104 | if (SkStrStartsWith(string, "no") && strlen(string) > 2) { |
| 105 | string += 2; |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 106 | // Only allow "no" to be prepended to the full name. |
| 107 | if (fName.equals(string)) { |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 108 | *fBoolValue = false; |
| 109 | return true; |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | int equalIndex = SkStrFind(string, "="); |
| 114 | if (equalIndex > 0) { |
| 115 | // The string has an equal sign. Check to see if the string matches. |
| 116 | SkString flag(string, equalIndex); |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 117 | if (flag.equals(*compareName)) { |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 118 | // Check to see if the remainder beyond the equal sign is true or false: |
| 119 | string += equalIndex + 1; |
| 120 | parse_bool_arg(string, fBoolValue); |
| 121 | return true; |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 122 | } else { |
| 123 | return false; |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | } |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 127 | return compareName->equals(string); |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 128 | } else { |
| 129 | // Has no dash |
| 130 | return false; |
| 131 | } |
| 132 | return false; |
| 133 | } |
| 134 | |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 135 | SkFlagInfo* SkCommandLineFlags::gHead; |
| 136 | SkString SkCommandLineFlags::gUsage; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 137 | |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 138 | void SkCommandLineFlags::SetUsage(const char* usage) { |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 139 | gUsage.set(usage); |
| 140 | } |
| 141 | |
| 142 | // Maximum line length for the help message. |
| 143 | #define LINE_LENGTH 80 |
| 144 | |
scroggo@google.com | 8366df0 | 2013-03-20 19:50:41 +0000 | [diff] [blame] | 145 | static void print_help_for_flag(const SkFlagInfo* flag) { |
| 146 | SkDebugf("\t--%s", flag->name().c_str()); |
| 147 | const SkString& shortName = flag->shortName(); |
| 148 | if (shortName.size() > 0) { |
| 149 | SkDebugf(" or -%s", shortName.c_str()); |
| 150 | } |
| 151 | SkDebugf(":\ttype: %s", flag->typeAsString().c_str()); |
| 152 | if (flag->defaultValue().size() > 0) { |
| 153 | SkDebugf("\tdefault: %s", flag->defaultValue().c_str()); |
| 154 | } |
| 155 | SkDebugf("\n"); |
| 156 | const SkString& help = flag->help(); |
| 157 | size_t length = help.size(); |
| 158 | const char* currLine = help.c_str(); |
| 159 | const char* stop = currLine + length; |
| 160 | while (currLine < stop) { |
| 161 | if (strlen(currLine) < LINE_LENGTH) { |
| 162 | // Only one line length's worth of text left. |
| 163 | SkDebugf("\t\t%s\n", currLine); |
| 164 | break; |
| 165 | } |
| 166 | int lineBreak = SkStrFind(currLine, "\n"); |
| 167 | if (lineBreak < 0 || lineBreak > LINE_LENGTH) { |
| 168 | // No line break within line length. Will need to insert one. |
| 169 | // Find a space before the line break. |
| 170 | int spaceIndex = LINE_LENGTH - 1; |
| 171 | while (spaceIndex > 0 && currLine[spaceIndex] != ' ') { |
| 172 | spaceIndex--; |
| 173 | } |
| 174 | int gap; |
| 175 | if (0 == spaceIndex) { |
| 176 | // No spaces on the entire line. Go ahead and break mid word. |
| 177 | spaceIndex = LINE_LENGTH; |
| 178 | gap = 0; |
| 179 | } else { |
| 180 | // Skip the space on the next line |
| 181 | gap = 1; |
| 182 | } |
| 183 | SkDebugf("\t\t%.*s\n", spaceIndex, currLine); |
| 184 | currLine += spaceIndex + gap; |
| 185 | } else { |
| 186 | // the line break is within the limit. Break there. |
| 187 | lineBreak++; |
| 188 | SkDebugf("\t\t%.*s", lineBreak, currLine); |
| 189 | currLine += lineBreak; |
| 190 | } |
| 191 | } |
| 192 | SkDebugf("\n"); |
| 193 | } |
| 194 | |
halcanary | 03758b8 | 2015-01-18 10:39:25 -0800 | [diff] [blame] | 195 | namespace { |
| 196 | struct CompareFlagsByName { |
| 197 | bool operator()(SkFlagInfo* a, SkFlagInfo* b) const { |
| 198 | return strcmp(a->name().c_str(), b->name().c_str()) < 0; |
| 199 | } |
| 200 | }; |
| 201 | } // namespace |
| 202 | |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 203 | void SkCommandLineFlags::Parse(int argc, char** argv) { |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 204 | // Only allow calling this function once. |
| 205 | static bool gOnce; |
| 206 | if (gOnce) { |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 207 | SkDebugf("Parse should only be called once at the beginning of main!\n"); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 208 | SkASSERT(false); |
| 209 | return; |
| 210 | } |
| 211 | gOnce = true; |
| 212 | |
| 213 | bool helpPrinted = false; |
| 214 | // Loop over argv, starting with 1, since the first is just the name of the program. |
| 215 | for (int i = 1; i < argc; i++) { |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 216 | if (0 == strcmp("-h", argv[i]) || 0 == strcmp("--help", argv[i])) { |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 217 | // Print help message. |
scroggo@google.com | 8366df0 | 2013-03-20 19:50:41 +0000 | [diff] [blame] | 218 | SkTDArray<const char*> helpFlags; |
| 219 | for (int j = i + 1; j < argc; j++) { |
| 220 | if (SkStrStartsWith(argv[j], '-')) { |
| 221 | break; |
| 222 | } |
| 223 | helpFlags.append(1, &argv[j]); |
| 224 | } |
| 225 | if (0 == helpFlags.count()) { |
| 226 | // Only print general help message if help for specific flags is not requested. |
| 227 | SkDebugf("%s\n%s\n", argv[0], gUsage.c_str()); |
| 228 | } |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 229 | SkDebugf("Flags:\n"); |
halcanary | 03758b8 | 2015-01-18 10:39:25 -0800 | [diff] [blame] | 230 | |
| 231 | if (0 == helpFlags.count()) { |
scroggo@google.com | 8366df0 | 2013-03-20 19:50:41 +0000 | [diff] [blame] | 232 | // If no flags followed --help, print them all |
halcanary | 03758b8 | 2015-01-18 10:39:25 -0800 | [diff] [blame] | 233 | SkTDArray<SkFlagInfo*> allFlags; |
| 234 | for (SkFlagInfo* flag = SkCommandLineFlags::gHead; flag; |
| 235 | flag = flag->next()) { |
| 236 | allFlags.push(flag); |
| 237 | } |
| 238 | SkTQSort(&allFlags[0], &allFlags[allFlags.count() - 1], |
| 239 | CompareFlagsByName()); |
| 240 | for (int i = 0; i < allFlags.count(); ++i) { |
| 241 | print_help_for_flag(allFlags[i]); |
| 242 | } |
| 243 | } else { |
| 244 | for (SkFlagInfo* flag = SkCommandLineFlags::gHead; flag; |
| 245 | flag = flag->next()) { |
scroggo@google.com | 8366df0 | 2013-03-20 19:50:41 +0000 | [diff] [blame] | 246 | for (int k = 0; k < helpFlags.count(); k++) { |
| 247 | if (flag->name().equals(helpFlags[k]) || |
| 248 | flag->shortName().equals(helpFlags[k])) { |
halcanary | 03758b8 | 2015-01-18 10:39:25 -0800 | [diff] [blame] | 249 | print_help_for_flag(flag); |
scroggo@google.com | 8366df0 | 2013-03-20 19:50:41 +0000 | [diff] [blame] | 250 | helpFlags.remove(k); |
| 251 | break; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 252 | } |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 255 | } |
scroggo@google.com | 8366df0 | 2013-03-20 19:50:41 +0000 | [diff] [blame] | 256 | if (helpFlags.count() > 0) { |
| 257 | SkDebugf("Requested help for unrecognized flags:\n"); |
| 258 | for (int k = 0; k < helpFlags.count(); k++) { |
| 259 | SkDebugf("\t--%s\n", helpFlags[k]); |
| 260 | } |
| 261 | } |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 262 | helpPrinted = true; |
| 263 | } |
| 264 | if (!helpPrinted) { |
| 265 | bool flagMatched = false; |
| 266 | SkFlagInfo* flag = gHead; |
| 267 | while (flag != NULL) { |
| 268 | if (flag->match(argv[i])) { |
| 269 | flagMatched = true; |
| 270 | switch (flag->getFlagType()) { |
| 271 | case SkFlagInfo::kBool_FlagType: |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 272 | // Can be handled by match, above, but can also be set by the next |
| 273 | // string. |
| 274 | if (i+1 < argc && !SkStrStartsWith(argv[i+1], '-')) { |
| 275 | i++; |
| 276 | bool value; |
| 277 | if (parse_bool_arg(argv[i], &value)) { |
| 278 | flag->setBool(value); |
| 279 | } |
| 280 | } |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 281 | break; |
| 282 | case SkFlagInfo::kString_FlagType: |
| 283 | flag->resetStrings(); |
| 284 | // Add all arguments until another flag is reached. |
mtklein | dfb7da3 | 2015-02-14 18:56:31 -0800 | [diff] [blame] | 285 | while (i+1 < argc) { |
| 286 | char* end = NULL; |
| 287 | (void)strtod(argv[i+1], &end); // Negative numbers aren't flags. |
| 288 | if (end == argv[i+1] && SkStrStartsWith(argv[i+1], '-')) { |
| 289 | break; |
| 290 | } |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 291 | i++; |
| 292 | flag->append(argv[i]); |
| 293 | } |
| 294 | break; |
| 295 | case SkFlagInfo::kInt_FlagType: |
| 296 | i++; |
| 297 | flag->setInt(atoi(argv[i])); |
| 298 | break; |
| 299 | case SkFlagInfo::kDouble_FlagType: |
| 300 | i++; |
| 301 | flag->setDouble(atof(argv[i])); |
| 302 | break; |
| 303 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 304 | SkDEBUGFAIL("Invalid flag type"); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 305 | } |
| 306 | break; |
| 307 | } |
| 308 | flag = flag->next(); |
| 309 | } |
| 310 | if (!flagMatched) { |
caryclark | c8fcafb | 2015-01-30 12:37:02 -0800 | [diff] [blame] | 311 | #if SK_BUILD_FOR_MAC |
| 312 | if (SkStrStartsWith(argv[i], "NSDocumentRevisions")) { |
| 313 | i++; // skip YES |
| 314 | } else |
| 315 | #endif |
mtklein | 929f63f | 2015-04-08 08:30:38 -0700 | [diff] [blame] | 316 | if (FLAGS_undefok) { |
| 317 | SkDebugf("FYI: ignoring unknown flag '%s'.\n", argv[i]); |
| 318 | } else { |
| 319 | SkDebugf("Got unknown flag '%s'. Exiting.\n", argv[i]); |
commit-bot@chromium.org | ffc224f | 2014-03-25 21:00:02 +0000 | [diff] [blame] | 320 | exit(-1); |
| 321 | } |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | } |
| 325 | // Since all of the flags have been set, release the memory used by each |
| 326 | // flag. FLAGS_x can still be used after this. |
| 327 | SkFlagInfo* flag = gHead; |
| 328 | gHead = NULL; |
| 329 | while (flag != NULL) { |
| 330 | SkFlagInfo* next = flag->next(); |
| 331 | SkDELETE(flag); |
| 332 | flag = next; |
| 333 | } |
| 334 | if (helpPrinted) { |
| 335 | exit(0); |
| 336 | } |
| 337 | } |
sglez@google.com | 586db93 | 2013-07-24 17:24:23 +0000 | [diff] [blame] | 338 | |
commit-bot@chromium.org | a6f37e7 | 2013-08-30 15:52:46 +0000 | [diff] [blame] | 339 | namespace { |
| 340 | |
| 341 | template <typename Strings> |
| 342 | bool ShouldSkipImpl(const Strings& strings, const char* name) { |
sglez@google.com | 586db93 | 2013-07-24 17:24:23 +0000 | [diff] [blame] | 343 | int count = strings.count(); |
| 344 | size_t testLen = strlen(name); |
| 345 | bool anyExclude = count == 0; |
| 346 | for (int i = 0; i < strings.count(); ++i) { |
| 347 | const char* matchName = strings[i]; |
| 348 | size_t matchLen = strlen(matchName); |
| 349 | bool matchExclude, matchStart, matchEnd; |
| 350 | if ((matchExclude = matchName[0] == '~')) { |
| 351 | anyExclude = true; |
| 352 | matchName++; |
| 353 | matchLen--; |
| 354 | } |
| 355 | if ((matchStart = matchName[0] == '^')) { |
| 356 | matchName++; |
| 357 | matchLen--; |
| 358 | } |
| 359 | if ((matchEnd = matchName[matchLen - 1] == '$')) { |
| 360 | matchLen--; |
| 361 | } |
| 362 | if (matchStart ? (!matchEnd || matchLen == testLen) |
| 363 | && strncmp(name, matchName, matchLen) == 0 |
| 364 | : matchEnd ? matchLen <= testLen |
| 365 | && strncmp(name + testLen - matchLen, matchName, matchLen) == 0 |
| 366 | : strstr(name, matchName) != 0) { |
| 367 | return matchExclude; |
| 368 | } |
| 369 | } |
| 370 | return !anyExclude; |
| 371 | } |
commit-bot@chromium.org | a6f37e7 | 2013-08-30 15:52:46 +0000 | [diff] [blame] | 372 | |
| 373 | } // namespace |
| 374 | |
| 375 | bool SkCommandLineFlags::ShouldSkip(const SkTDArray<const char*>& strings, const char* name) { |
| 376 | return ShouldSkipImpl(strings, name); |
| 377 | } |
| 378 | bool SkCommandLineFlags::ShouldSkip(const StringArray& strings, const char* name) { |
| 379 | return ShouldSkipImpl(strings, name); |
| 380 | } |