blob: 5b28794f6ca1c232d4c301dc94e6cf4af3f5c29e [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2006 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/flags.h"
kwiberg22487b22016-09-13 01:17:10 -070012
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
Patrik Höglunda8005cf2017-12-13 16:05:42 +010018#include "rtc_base/stringutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
20#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +020021// clang-format off
22// clang formating would change include order.
Patrik Höglunda8005cf2017-12-13 16:05:42 +010023#include <windows.h>
Yves Gerey665174f2018-06-19 15:03:05 +020024#include <shellapi.h> // must come after windows.h
25// clang-format on
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026#endif
27
Edward Lemur260c3982018-02-07 15:45:40 +010028namespace {
29bool FlagEq(const char* arg, const char* flag) {
30 // Compare two flags for equality.
31 // 'arg' is the name of a flag passed via the command line and 'flag' is the
32 // name of a flag defined with the DEFINE_* macros.
33 // We compare the flags for equality, considering hyphens (-) and
34 // underscores (_) to be equivalent, so that --flag-name and --flag_name both
35 // match with --flag_name.
36 while (*arg != '\0' && (*arg == *flag || (*arg == '-' && *flag == '_'))) {
37 ++arg;
38 ++flag;
39 }
40 return *arg == '\0' && *flag == '\0';
41}
42} // namespace
43
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000044namespace rtc {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000045// -----------------------------------------------------------------------------
46// Implementation of Flag
47
Yves Gerey665174f2018-06-19 15:03:05 +020048Flag::Flag(const char* file,
49 const char* name,
50 const char* comment,
51 Type type,
52 void* variable,
53 FlagValue default__)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000054 : file_(file),
55 name_(name),
56 comment_(comment),
57 type_(type),
58 variable_(reinterpret_cast<FlagValue*>(variable)),
59 default_(default__) {
60 FlagList::Register(this);
61}
62
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063void Flag::SetToDefault() {
64 // Note that we cannot simply do '*variable_ = default_;' since
65 // flag variables are not really of type FlagValue and thus may
66 // be smaller! The FlagValue union is simply 'overlayed' on top
67 // of a flag variable for convenient access. Since union members
68 // are guarantee to be aligned at the beginning, this works.
69 switch (type_) {
70 case Flag::BOOL:
71 variable_->b = default_.b;
72 return;
73 case Flag::INT:
74 variable_->i = default_.i;
75 return;
76 case Flag::FLOAT:
77 variable_->f = default_.f;
78 return;
79 case Flag::STRING:
80 variable_->s = default_.s;
81 return;
82 }
andrew@webrtc.orga5b78692014-08-28 16:28:26 +000083 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000084}
85
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086static const char* Type2String(Flag::Type type) {
87 switch (type) {
Yves Gerey665174f2018-06-19 15:03:05 +020088 case Flag::BOOL:
89 return "bool";
90 case Flag::INT:
91 return "int";
92 case Flag::FLOAT:
93 return "float";
94 case Flag::STRING:
95 return "string";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096 }
andrew@webrtc.orga5b78692014-08-28 16:28:26 +000097 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000098}
99
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100static void PrintFlagValue(Flag::Type type, FlagValue* p) {
101 switch (type) {
102 case Flag::BOOL:
103 printf("%s", (p->b ? "true" : "false"));
104 return;
105 case Flag::INT:
106 printf("%d", p->i);
107 return;
108 case Flag::FLOAT:
109 printf("%f", p->f);
110 return;
111 case Flag::STRING:
112 printf("%s", p->s);
113 return;
114 }
andrew@webrtc.orga5b78692014-08-28 16:28:26 +0000115 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116}
117
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000118void Flag::Print(bool print_current_value) {
119 printf(" --%s (%s) type: %s default: ", name_, comment_,
Yves Gerey665174f2018-06-19 15:03:05 +0200120 Type2String(type_));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121 PrintFlagValue(type_, &default_);
122 if (print_current_value) {
123 printf(" current value: ");
124 PrintFlagValue(type_, variable_);
125 }
126 printf("\n");
127}
128
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129// -----------------------------------------------------------------------------
130// Implementation of FlagList
131
deadbeef37f5ecf2017-02-27 14:06:41 -0800132Flag* FlagList::list_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000133
134FlagList::FlagList() {
deadbeef37f5ecf2017-02-27 14:06:41 -0800135 list_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000136}
137
138void FlagList::Print(const char* file, bool print_current_value) {
139 // Since flag registration is likely by file (= C++ file),
140 // we don't need to sort by file and still get grouped output.
deadbeef37f5ecf2017-02-27 14:06:41 -0800141 const char* current = nullptr;
142 for (Flag* f = list_; f != nullptr; f = f->next()) {
143 if (file == nullptr || file == f->file()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000144 if (current != f->file()) {
145 printf("Flags from %s:\n", f->file());
146 current = f->file();
147 }
148 f->Print(print_current_value);
149 }
150 }
151}
152
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000153Flag* FlagList::Lookup(const char* name) {
154 Flag* f = list_;
Edward Lemur260c3982018-02-07 15:45:40 +0100155 while (f != nullptr && !FlagEq(name, f->name()))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000156 f = f->next();
157 return f;
158}
159
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000160void FlagList::SplitArgument(const char* arg,
Yves Gerey665174f2018-06-19 15:03:05 +0200161 char* buffer,
162 int buffer_size,
163 const char** name,
164 const char** value,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000165 bool* is_bool) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800166 *name = nullptr;
167 *value = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168 *is_bool = false;
169
170 if (*arg == '-') {
171 // find the begin of the flag name
172 arg++; // remove 1st '-'
173 if (*arg == '-')
174 arg++; // remove 2nd '-'
oprypin31377a22017-09-04 23:56:42 -0700175 if (arg[0] == 'n' && arg[1] == 'o' && Lookup(arg + 2)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000176 arg += 2; // remove "no"
177 *is_bool = true;
178 }
179 *name = arg;
180
181 // find the end of the flag name
182 while (*arg != '\0' && *arg != '=')
183 arg++;
184
185 // get the value if any
186 if (*arg == '=') {
187 // make a copy so we can NUL-terminate flag name
188 int n = static_cast<int>(arg - *name);
henrikg91d6ede2015-09-17 00:24:34 -0700189 RTC_CHECK_LT(n, buffer_size);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000190 memcpy(buffer, *name, n * sizeof(char));
191 buffer[n] = '\0';
192 *name = buffer;
193 // get the value
194 *value = arg + 1;
195 }
196 }
197}
198
Yves Gerey665174f2018-06-19 15:03:05 +0200199int FlagList::SetFlagsFromCommandLine(int* argc,
200 const char** argv,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000201 bool remove_flags) {
202 // parse arguments
203 for (int i = 1; i < *argc; /* see below */) {
204 int j = i; // j > 0
205 const char* arg = argv[i++];
206
207 // split arg into flag components
208 char buffer[1024];
209 const char* name;
210 const char* value;
211 bool is_bool;
212 SplitArgument(arg, buffer, sizeof buffer, &name, &value, &is_bool);
213
deadbeef37f5ecf2017-02-27 14:06:41 -0800214 if (name != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000215 // lookup the flag
216 Flag* flag = Lookup(name);
deadbeef37f5ecf2017-02-27 14:06:41 -0800217 if (flag == nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000218 fprintf(stderr, "Error: unrecognized flag %s\n", arg);
219 return j;
220 }
221
222 // if we still need a flag value, use the next argument if available
deadbeef37f5ecf2017-02-27 14:06:41 -0800223 if (flag->type() != Flag::BOOL && value == nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000224 if (i < *argc) {
225 value = argv[i++];
226 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200227 fprintf(stderr, "Error: missing value for flag %s of type %s\n", arg,
228 Type2String(flag->type()));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000229 return j;
230 }
231 }
232
233 // set the flag
Yves Gerey665174f2018-06-19 15:03:05 +0200234 char empty[] = {'\0'};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000235 char* endp = empty;
236 switch (flag->type()) {
237 case Flag::BOOL:
238 *flag->bool_variable() = !is_bool;
239 break;
240 case Flag::INT:
241 *flag->int_variable() = strtol(value, &endp, 10);
242 break;
243 case Flag::FLOAT:
244 *flag->float_variable() = strtod(value, &endp);
245 break;
246 case Flag::STRING:
247 *flag->string_variable() = value;
248 break;
249 }
250
251 // handle errors
deadbeef37f5ecf2017-02-27 14:06:41 -0800252 if ((flag->type() == Flag::BOOL && value != nullptr) ||
253 (flag->type() != Flag::BOOL && is_bool) || *endp != '\0') {
Yves Gerey665174f2018-06-19 15:03:05 +0200254 fprintf(stderr, "Error: illegal value for flag %s of type %s\n", arg,
255 Type2String(flag->type()));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000256 return j;
257 }
258
259 // remove the flag & value from the command
260 if (remove_flags)
261 while (j < i)
deadbeef37f5ecf2017-02-27 14:06:41 -0800262 argv[j++] = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000263 }
264 }
265
266 // shrink the argument list
267 if (remove_flags) {
268 int j = 1;
269 for (int i = 1; i < *argc; i++) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800270 if (argv[i] != nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000271 argv[j++] = argv[i];
272 }
273 *argc = j;
274 }
275
276 // parsed all flags successfully
277 return 0;
278}
279
280void FlagList::Register(Flag* flag) {
kwiberg22487b22016-09-13 01:17:10 -0700281 RTC_DCHECK(flag);
kwibergaf476c72016-11-28 15:21:39 -0800282 RTC_DCHECK_GT(strlen(flag->name()), 0);
noahric73ab9172016-07-14 18:21:11 -0700283 // NOTE: Don't call Lookup() within Register because it accesses the name_
284 // of other flags in list_, and if the flags are coming from two different
285 // compilation units, the initialization order between them is undefined, and
286 // this will trigger an asan initialization-order-fiasco error.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000287 flag->next_ = list_;
288 list_ = flag;
289}
290
291#if defined(WEBRTC_WIN)
292WindowsCommandLineArguments::WindowsCommandLineArguments() {
293 // start by getting the command line.
294 LPTSTR command_line = ::GetCommandLine();
Yves Gerey665174f2018-06-19 15:03:05 +0200295 // now, convert it to a list of wide char strings.
296 LPWSTR* wide_argv = ::CommandLineToArgvW(command_line, &argc_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000297 // now allocate an array big enough to hold that many string pointers.
298 argv_ = new char*[argc_];
299
300 // iterate over the returned wide strings;
Yves Gerey665174f2018-06-19 15:03:05 +0200301 for (int i = 0; i < argc_; ++i) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000302 std::string s = rtc::ToUtf8(wide_argv[i], wcslen(wide_argv[i]));
Yves Gerey665174f2018-06-19 15:03:05 +0200303 char* buffer = new char[s.length() + 1];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000304 rtc::strcpyn(buffer, s.length() + 1, s.c_str());
305
306 // make sure the argv array has the right string at this point.
307 argv_[i] = buffer;
308 }
309 LocalFree(wide_argv);
310}
311
312WindowsCommandLineArguments::~WindowsCommandLineArguments() {
313 // need to free each string in the array, and then the array.
Yves Gerey665174f2018-06-19 15:03:05 +0200314 for (int i = 0; i < argc_; i++) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000315 delete[] argv_[i];
316 }
317
318 delete[] argv_;
319}
andrew@webrtc.orga5b78692014-08-28 16:28:26 +0000320#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000321
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +0000322} // namespace rtc