blob: bfc9f5eebc03f24eb64f05b0ee8a034e0b1e7d35 [file] [log] [blame]
scroggo@google.com161e1ba2013-03-04 16:41:06 +00001/*
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.comd9ba9a02013-03-21 19:43:15 +00008#include "SkCommandLineFlags.h"
scroggo@google.comb7dbf632013-04-23 15:38:09 +00009#include "SkTDArray.h"
halcanary03758b82015-01-18 10:39:25 -080010#include "SkTSort.h"
scroggo@google.com161e1ba2013-03-04 16:41:06 +000011
bungeman60e0fee2015-08-26 05:15:46 -070012#include <stdlib.h>
13
benjaminwagner0b8321e2016-03-23 08:38:59 -070014#if defined(GOOGLE3) && defined(SK_BUILD_FOR_IOS)
15 // This is defined by //base only for iOS (I don't know why).
benjaminwagner86ea33e2015-10-26 10:46:25 -070016 DECLARE_bool(undefok)
17#else
18 DEFINE_bool(undefok, false, "Silently ignore unknown flags instead of crashing.");
19#endif
commit-bot@chromium.orgffc224f2014-03-25 21:00:02 +000020
mtklein19e259b2015-05-04 10:54:48 -070021template <typename T> static void ignore_result(const T&) {}
22
scroggo@google.com58104a92013-04-24 19:25:26 +000023bool SkFlagInfo::CreateStringFlag(const char* name, const char* shortName,
24 SkCommandLineFlags::StringArray* pStrings,
kkinnunen3e980c32015-12-23 01:33:00 -080025 const char* defaultValue, const char* helpString,
26 const char* extendedHelpString) {
27 SkFlagInfo* info = new SkFlagInfo(name, shortName, kString_FlagType, helpString,
28 extendedHelpString);
scroggo@google.com58104a92013-04-24 19:25:26 +000029 info->fDefaultString.set(defaultValue);
30
31 info->fStrings = pStrings;
32 SetDefaultStrings(pStrings, defaultValue);
33 return true;
34}
35
36void SkFlagInfo::SetDefaultStrings(SkCommandLineFlags::StringArray* pStrings,
37 const char* defaultValue) {
38 pStrings->reset();
halcanary96fcdcc2015-08-27 07:41:13 -070039 if (nullptr == defaultValue) {
scroggo@google.comd0419012013-04-24 19:37:52 +000040 return;
41 }
scroggo@google.com58104a92013-04-24 19:25:26 +000042 // If default is "", leave the array empty.
43 size_t defaultLength = strlen(defaultValue);
44 if (defaultLength > 0) {
45 const char* const defaultEnd = defaultValue + defaultLength;
46 const char* begin = defaultValue;
47 while (true) {
48 while (begin < defaultEnd && ' ' == *begin) {
49 begin++;
50 }
51 if (begin < defaultEnd) {
52 const char* end = begin + 1;
53 while (end < defaultEnd && ' ' != *end) {
54 end++;
55 }
56 size_t length = end - begin;
57 pStrings->append(begin, length);
58 begin = end + 1;
59 } else {
60 break;
61 }
62 }
63 }
64}
65
scroggo@google.com5dc4ca12013-03-21 13:10:59 +000066static bool string_is_in(const char* target, const char* set[], size_t len) {
67 for (size_t i = 0; i < len; i++) {
68 if (0 == strcmp(target, set[i])) {
69 return true;
70 }
71 }
72 return false;
73}
74
75/**
76 * Check to see whether string represents a boolean value.
77 * @param string C style string to parse.
78 * @param result Pointer to a boolean which will be set to the value in the string, if the
79 * string represents a boolean.
80 * @param boolean True if the string represents a boolean, false otherwise.
81 */
82static bool parse_bool_arg(const char* string, bool* result) {
83 static const char* trueValues[] = { "1", "TRUE", "true" };
84 if (string_is_in(string, trueValues, SK_ARRAY_COUNT(trueValues))) {
85 *result = true;
86 return true;
87 }
88 static const char* falseValues[] = { "0", "FALSE", "false" };
89 if (string_is_in(string, falseValues, SK_ARRAY_COUNT(falseValues))) {
90 *result = false;
91 return true;
92 }
93 SkDebugf("Parameter \"%s\" not supported.\n", string);
94 return false;
95}
96
97bool SkFlagInfo::match(const char* string) {
98 if (SkStrStartsWith(string, '-') && strlen(string) > 1) {
99 string++;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000100 const SkString* compareName;
scroggo@google.com5dc4ca12013-03-21 13:10:59 +0000101 if (SkStrStartsWith(string, '-') && strlen(string) > 1) {
102 string++;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000103 // There were two dashes. Compare against full name.
104 compareName = &fName;
105 } else {
106 // One dash. Compare against the short name.
107 compareName = &fShortName;
scroggo@google.com5dc4ca12013-03-21 13:10:59 +0000108 }
109 if (kBool_FlagType == fFlagType) {
110 // In this case, go ahead and set the value.
scroggo@google.com604e0c22013-04-09 21:25:46 +0000111 if (compareName->equals(string)) {
scroggo@google.com5dc4ca12013-03-21 13:10:59 +0000112 *fBoolValue = true;
113 return true;
114 }
115 if (SkStrStartsWith(string, "no") && strlen(string) > 2) {
116 string += 2;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000117 // Only allow "no" to be prepended to the full name.
118 if (fName.equals(string)) {
scroggo@google.com5dc4ca12013-03-21 13:10:59 +0000119 *fBoolValue = false;
120 return true;
121 }
122 return false;
123 }
124 int equalIndex = SkStrFind(string, "=");
125 if (equalIndex > 0) {
126 // The string has an equal sign. Check to see if the string matches.
127 SkString flag(string, equalIndex);
scroggo@google.com604e0c22013-04-09 21:25:46 +0000128 if (flag.equals(*compareName)) {
scroggo@google.com5dc4ca12013-03-21 13:10:59 +0000129 // Check to see if the remainder beyond the equal sign is true or false:
130 string += equalIndex + 1;
131 parse_bool_arg(string, fBoolValue);
132 return true;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000133 } else {
134 return false;
scroggo@google.com5dc4ca12013-03-21 13:10:59 +0000135 }
136 }
137 }
scroggo@google.com604e0c22013-04-09 21:25:46 +0000138 return compareName->equals(string);
scroggo@google.com5dc4ca12013-03-21 13:10:59 +0000139 } else {
140 // Has no dash
141 return false;
142 }
143 return false;
144}
145
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000146SkFlagInfo* SkCommandLineFlags::gHead;
147SkString SkCommandLineFlags::gUsage;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000148
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000149void SkCommandLineFlags::SetUsage(const char* usage) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000150 gUsage.set(usage);
151}
152
msarett3478f752016-02-12 14:47:09 -0800153void SkCommandLineFlags::PrintUsage() {
154 SkDebugf("%s", gUsage.c_str());
155}
156
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000157// Maximum line length for the help message.
halcanary27523142015-04-27 06:41:35 -0700158#define LINE_LENGTH 72
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000159
kkinnunen3e980c32015-12-23 01:33:00 -0800160static void print_indented(const SkString& text) {
161 size_t length = text.size();
162 const char* currLine = text.c_str();
scroggo@google.com8366df02013-03-20 19:50:41 +0000163 const char* stop = currLine + length;
164 while (currLine < stop) {
rmistry0f515bd2015-12-22 10:22:26 -0800165 int lineBreak = SkStrFind(currLine, "\n");
kkinnunen3e980c32015-12-23 01:33:00 -0800166 if (lineBreak < 0) {
167 lineBreak = static_cast<int>(strlen(currLine));
168 }
169 if (lineBreak > LINE_LENGTH) {
scroggo@google.com8366df02013-03-20 19:50:41 +0000170 // No line break within line length. Will need to insert one.
171 // Find a space before the line break.
172 int spaceIndex = LINE_LENGTH - 1;
173 while (spaceIndex > 0 && currLine[spaceIndex] != ' ') {
174 spaceIndex--;
175 }
176 int gap;
177 if (0 == spaceIndex) {
178 // No spaces on the entire line. Go ahead and break mid word.
179 spaceIndex = LINE_LENGTH;
180 gap = 0;
181 } else {
182 // Skip the space on the next line
183 gap = 1;
184 }
halcanary27523142015-04-27 06:41:35 -0700185 SkDebugf(" %.*s\n", spaceIndex, currLine);
scroggo@google.com8366df02013-03-20 19:50:41 +0000186 currLine += spaceIndex + gap;
187 } else {
188 // the line break is within the limit. Break there.
189 lineBreak++;
halcanary27523142015-04-27 06:41:35 -0700190 SkDebugf(" %.*s", lineBreak, currLine);
scroggo@google.com8366df02013-03-20 19:50:41 +0000191 currLine += lineBreak;
192 }
193 }
kkinnunen3e980c32015-12-23 01:33:00 -0800194}
195
196static void print_help_for_flag(const SkFlagInfo* flag) {
197 SkDebugf(" --%s", flag->name().c_str());
198 const SkString& shortName = flag->shortName();
199 if (shortName.size() > 0) {
200 SkDebugf(" or -%s", shortName.c_str());
201 }
202 SkDebugf(":\ttype: %s", flag->typeAsString().c_str());
203 if (flag->defaultValue().size() > 0) {
204 SkDebugf("\tdefault: %s", flag->defaultValue().c_str());
205 }
206 SkDebugf("\n");
207 const SkString& help = flag->help();
208 print_indented(help);
209 SkDebugf("\n");
210}
211static void print_extended_help_for_flag(const SkFlagInfo* flag) {
212 print_help_for_flag(flag);
213 print_indented(flag->extendedHelp());
scroggo@google.com8366df02013-03-20 19:50:41 +0000214 SkDebugf("\n");
215}
216
halcanary03758b82015-01-18 10:39:25 -0800217namespace {
218struct CompareFlagsByName {
219 bool operator()(SkFlagInfo* a, SkFlagInfo* b) const {
220 return strcmp(a->name().c_str(), b->name().c_str()) < 0;
221 }
222};
223} // namespace
224
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000225void SkCommandLineFlags::Parse(int argc, char** argv) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000226 // Only allow calling this function once.
227 static bool gOnce;
228 if (gOnce) {
scroggo@google.com604e0c22013-04-09 21:25:46 +0000229 SkDebugf("Parse should only be called once at the beginning of main!\n");
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000230 SkASSERT(false);
231 return;
232 }
233 gOnce = true;
234
235 bool helpPrinted = false;
Cary Clark0933f332017-07-06 08:33:19 -0400236 bool flagsPrinted = false;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000237 // Loop over argv, starting with 1, since the first is just the name of the program.
238 for (int i = 1; i < argc; i++) {
scroggo@google.com604e0c22013-04-09 21:25:46 +0000239 if (0 == strcmp("-h", argv[i]) || 0 == strcmp("--help", argv[i])) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000240 // Print help message.
scroggo@google.com8366df02013-03-20 19:50:41 +0000241 SkTDArray<const char*> helpFlags;
242 for (int j = i + 1; j < argc; j++) {
243 if (SkStrStartsWith(argv[j], '-')) {
244 break;
245 }
246 helpFlags.append(1, &argv[j]);
247 }
248 if (0 == helpFlags.count()) {
249 // Only print general help message if help for specific flags is not requested.
250 SkDebugf("%s\n%s\n", argv[0], gUsage.c_str());
251 }
Cary Clark0933f332017-07-06 08:33:19 -0400252 if (!flagsPrinted) {
253 SkDebugf("Flags:\n");
254 flagsPrinted = true;
255 }
halcanary03758b82015-01-18 10:39:25 -0800256 if (0 == helpFlags.count()) {
scroggo@google.com8366df02013-03-20 19:50:41 +0000257 // If no flags followed --help, print them all
halcanary03758b82015-01-18 10:39:25 -0800258 SkTDArray<SkFlagInfo*> allFlags;
259 for (SkFlagInfo* flag = SkCommandLineFlags::gHead; flag;
260 flag = flag->next()) {
261 allFlags.push(flag);
262 }
263 SkTQSort(&allFlags[0], &allFlags[allFlags.count() - 1],
264 CompareFlagsByName());
265 for (int i = 0; i < allFlags.count(); ++i) {
266 print_help_for_flag(allFlags[i]);
kkinnunen3e980c32015-12-23 01:33:00 -0800267 if (allFlags[i]->extendedHelp().size() > 0) {
268 SkDebugf(" Use '--help %s' for more information.\n",
269 allFlags[i]->name().c_str());
270 }
halcanary03758b82015-01-18 10:39:25 -0800271 }
272 } else {
273 for (SkFlagInfo* flag = SkCommandLineFlags::gHead; flag;
274 flag = flag->next()) {
scroggo@google.com8366df02013-03-20 19:50:41 +0000275 for (int k = 0; k < helpFlags.count(); k++) {
276 if (flag->name().equals(helpFlags[k]) ||
277 flag->shortName().equals(helpFlags[k])) {
kkinnunen3e980c32015-12-23 01:33:00 -0800278 print_extended_help_for_flag(flag);
scroggo@google.com8366df02013-03-20 19:50:41 +0000279 helpFlags.remove(k);
280 break;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000281 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000282 }
283 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000284 }
scroggo@google.com8366df02013-03-20 19:50:41 +0000285 if (helpFlags.count() > 0) {
286 SkDebugf("Requested help for unrecognized flags:\n");
287 for (int k = 0; k < helpFlags.count(); k++) {
halcanary27523142015-04-27 06:41:35 -0700288 SkDebugf(" --%s\n", helpFlags[k]);
scroggo@google.com8366df02013-03-20 19:50:41 +0000289 }
290 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000291 helpPrinted = true;
292 }
293 if (!helpPrinted) {
Brian Osman49f57122016-10-24 14:53:08 -0400294 SkFlagInfo* matchedFlag = nullptr;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000295 SkFlagInfo* flag = gHead;
Brian Osman49f57122016-10-24 14:53:08 -0400296 int startI = i;
halcanary96fcdcc2015-08-27 07:41:13 -0700297 while (flag != nullptr) {
Brian Osman49f57122016-10-24 14:53:08 -0400298 if (flag->match(argv[startI])) {
299 i = startI;
300 if (matchedFlag) {
301 // Don't redefine the same flag with different types.
302 SkASSERT(matchedFlag->getFlagType() == flag->getFlagType());
303 } else {
304 matchedFlag = flag;
305 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000306 switch (flag->getFlagType()) {
307 case SkFlagInfo::kBool_FlagType:
scroggo@google.com5dc4ca12013-03-21 13:10:59 +0000308 // Can be handled by match, above, but can also be set by the next
309 // string.
310 if (i+1 < argc && !SkStrStartsWith(argv[i+1], '-')) {
311 i++;
312 bool value;
313 if (parse_bool_arg(argv[i], &value)) {
314 flag->setBool(value);
315 }
316 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000317 break;
318 case SkFlagInfo::kString_FlagType:
319 flag->resetStrings();
320 // Add all arguments until another flag is reached.
mtkleindfb7da32015-02-14 18:56:31 -0800321 while (i+1 < argc) {
halcanary96fcdcc2015-08-27 07:41:13 -0700322 char* end = nullptr;
mtklein19e259b2015-05-04 10:54:48 -0700323 // Negative numbers aren't flags.
324 ignore_result(strtod(argv[i+1], &end));
mtkleindfb7da32015-02-14 18:56:31 -0800325 if (end == argv[i+1] && SkStrStartsWith(argv[i+1], '-')) {
326 break;
327 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000328 i++;
329 flag->append(argv[i]);
330 }
331 break;
332 case SkFlagInfo::kInt_FlagType:
333 i++;
334 flag->setInt(atoi(argv[i]));
335 break;
336 case SkFlagInfo::kDouble_FlagType:
337 i++;
338 flag->setDouble(atof(argv[i]));
339 break;
340 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000341 SkDEBUGFAIL("Invalid flag type");
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000342 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000343 }
344 flag = flag->next();
345 }
Brian Osman49f57122016-10-24 14:53:08 -0400346 if (!matchedFlag) {
mtklein06343172016-07-28 09:58:44 -0700347#if defined(SK_BUILD_FOR_MAC)
caryclark83ca6282015-06-10 09:31:09 -0700348 if (SkStrStartsWith(argv[i], "NSDocumentRevisions")
349 || SkStrStartsWith(argv[i], "-NSDocumentRevisions")) {
350 i++; // skip YES
caryclarkc8fcafb2015-01-30 12:37:02 -0800351 } else
352#endif
mtklein929f63f2015-04-08 08:30:38 -0700353 if (FLAGS_undefok) {
354 SkDebugf("FYI: ignoring unknown flag '%s'.\n", argv[i]);
355 } else {
356 SkDebugf("Got unknown flag '%s'. Exiting.\n", argv[i]);
commit-bot@chromium.orgffc224f2014-03-25 21:00:02 +0000357 exit(-1);
358 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000359 }
360 }
361 }
362 // Since all of the flags have been set, release the memory used by each
363 // flag. FLAGS_x can still be used after this.
364 SkFlagInfo* flag = gHead;
halcanary96fcdcc2015-08-27 07:41:13 -0700365 gHead = nullptr;
366 while (flag != nullptr) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000367 SkFlagInfo* next = flag->next();
halcanary385fe4d2015-08-26 13:07:48 -0700368 delete flag;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000369 flag = next;
370 }
371 if (helpPrinted) {
372 exit(0);
373 }
374}
sglez@google.com586db932013-07-24 17:24:23 +0000375
commit-bot@chromium.orga6f37e72013-08-30 15:52:46 +0000376namespace {
377
378template <typename Strings>
379bool ShouldSkipImpl(const Strings& strings, const char* name) {
sglez@google.com586db932013-07-24 17:24:23 +0000380 int count = strings.count();
381 size_t testLen = strlen(name);
382 bool anyExclude = count == 0;
383 for (int i = 0; i < strings.count(); ++i) {
384 const char* matchName = strings[i];
385 size_t matchLen = strlen(matchName);
386 bool matchExclude, matchStart, matchEnd;
387 if ((matchExclude = matchName[0] == '~')) {
388 anyExclude = true;
389 matchName++;
390 matchLen--;
391 }
392 if ((matchStart = matchName[0] == '^')) {
393 matchName++;
394 matchLen--;
395 }
396 if ((matchEnd = matchName[matchLen - 1] == '$')) {
397 matchLen--;
398 }
399 if (matchStart ? (!matchEnd || matchLen == testLen)
400 && strncmp(name, matchName, matchLen) == 0
401 : matchEnd ? matchLen <= testLen
402 && strncmp(name + testLen - matchLen, matchName, matchLen) == 0
403 : strstr(name, matchName) != 0) {
404 return matchExclude;
405 }
406 }
407 return !anyExclude;
408}
commit-bot@chromium.orga6f37e72013-08-30 15:52:46 +0000409
410} // namespace
411
412bool SkCommandLineFlags::ShouldSkip(const SkTDArray<const char*>& strings, const char* name) {
413 return ShouldSkipImpl(strings, name);
414}
415bool SkCommandLineFlags::ShouldSkip(const StringArray& strings, const char* name) {
416 return ShouldSkipImpl(strings, name);
417}