blob: a2fb7089b0a31656b460d8d3459f8e78770ac5f7 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 04:47:3111#include "rtc_base/flags.h"
kwiberg22487b22016-09-13 08:17:1012
henrike@webrtc.orgf0488722014-05-13 18:00:2613#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
Mirko Bonadei92ea95e2017-09-15 04:47:3117#include "rtc_base/checks.h"
Patrik Höglunda8005cf2017-12-13 15:05:4218#include "rtc_base/stringutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2619
20#if defined(WEBRTC_WIN)
Patrik Höglunda8005cf2017-12-13 15:05:4221#include <windows.h>
henrike@webrtc.orgf0488722014-05-13 18:00:2622#include <shellapi.h>
23#endif
24
Edward Lemur260c3982018-02-07 14:45:4025
26namespace {
27bool FlagEq(const char* arg, const char* flag) {
28 // Compare two flags for equality.
29 // 'arg' is the name of a flag passed via the command line and 'flag' is the
30 // name of a flag defined with the DEFINE_* macros.
31 // We compare the flags for equality, considering hyphens (-) and
32 // underscores (_) to be equivalent, so that --flag-name and --flag_name both
33 // match with --flag_name.
34 while (*arg != '\0' && (*arg == *flag || (*arg == '-' && *flag == '_'))) {
35 ++arg;
36 ++flag;
37 }
38 return *arg == '\0' && *flag == '\0';
39}
40} // namespace
41
henrike@webrtc.orgc50bf7c2014-05-14 18:24:1342namespace rtc {
henrike@webrtc.orgf0488722014-05-13 18:00:2643// -----------------------------------------------------------------------------
44// Implementation of Flag
45
46Flag::Flag(const char* file, const char* name, const char* comment,
47 Type type, void* variable, FlagValue default__)
48 : file_(file),
49 name_(name),
50 comment_(comment),
51 type_(type),
52 variable_(reinterpret_cast<FlagValue*>(variable)),
53 default_(default__) {
54 FlagList::Register(this);
55}
56
57
58void Flag::SetToDefault() {
59 // Note that we cannot simply do '*variable_ = default_;' since
60 // flag variables are not really of type FlagValue and thus may
61 // be smaller! The FlagValue union is simply 'overlayed' on top
62 // of a flag variable for convenient access. Since union members
63 // are guarantee to be aligned at the beginning, this works.
64 switch (type_) {
65 case Flag::BOOL:
66 variable_->b = default_.b;
67 return;
68 case Flag::INT:
69 variable_->i = default_.i;
70 return;
71 case Flag::FLOAT:
72 variable_->f = default_.f;
73 return;
74 case Flag::STRING:
75 variable_->s = default_.s;
76 return;
77 }
andrew@webrtc.orga5b78692014-08-28 16:28:2678 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:2679}
80
81
82static const char* Type2String(Flag::Type type) {
83 switch (type) {
84 case Flag::BOOL: return "bool";
85 case Flag::INT: return "int";
86 case Flag::FLOAT: return "float";
87 case Flag::STRING: return "string";
88 }
andrew@webrtc.orga5b78692014-08-28 16:28:2689 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:2690}
91
92
93static void PrintFlagValue(Flag::Type type, FlagValue* p) {
94 switch (type) {
95 case Flag::BOOL:
96 printf("%s", (p->b ? "true" : "false"));
97 return;
98 case Flag::INT:
99 printf("%d", p->i);
100 return;
101 case Flag::FLOAT:
102 printf("%f", p->f);
103 return;
104 case Flag::STRING:
105 printf("%s", p->s);
106 return;
107 }
andrew@webrtc.orga5b78692014-08-28 16:28:26108 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26109}
110
111
112void Flag::Print(bool print_current_value) {
113 printf(" --%s (%s) type: %s default: ", name_, comment_,
114 Type2String(type_));
115 PrintFlagValue(type_, &default_);
116 if (print_current_value) {
117 printf(" current value: ");
118 PrintFlagValue(type_, variable_);
119 }
120 printf("\n");
121}
122
123
124// -----------------------------------------------------------------------------
125// Implementation of FlagList
126
deadbeef37f5ecf2017-02-27 22:06:41127Flag* FlagList::list_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26128
129FlagList::FlagList() {
deadbeef37f5ecf2017-02-27 22:06:41130 list_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26131}
132
133void FlagList::Print(const char* file, bool print_current_value) {
134 // Since flag registration is likely by file (= C++ file),
135 // we don't need to sort by file and still get grouped output.
deadbeef37f5ecf2017-02-27 22:06:41136 const char* current = nullptr;
137 for (Flag* f = list_; f != nullptr; f = f->next()) {
138 if (file == nullptr || file == f->file()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26139 if (current != f->file()) {
140 printf("Flags from %s:\n", f->file());
141 current = f->file();
142 }
143 f->Print(print_current_value);
144 }
145 }
146}
147
148
149Flag* FlagList::Lookup(const char* name) {
150 Flag* f = list_;
Edward Lemur260c3982018-02-07 14:45:40151 while (f != nullptr && !FlagEq(name, f->name()))
henrike@webrtc.orgf0488722014-05-13 18:00:26152 f = f->next();
153 return f;
154}
155
156
157void FlagList::SplitArgument(const char* arg,
158 char* buffer, int buffer_size,
159 const char** name, const char** value,
160 bool* is_bool) {
deadbeef37f5ecf2017-02-27 22:06:41161 *name = nullptr;
162 *value = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26163 *is_bool = false;
164
165 if (*arg == '-') {
166 // find the begin of the flag name
167 arg++; // remove 1st '-'
168 if (*arg == '-')
169 arg++; // remove 2nd '-'
oprypin31377a22017-09-05 06:56:42170 if (arg[0] == 'n' && arg[1] == 'o' && Lookup(arg + 2)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26171 arg += 2; // remove "no"
172 *is_bool = true;
173 }
174 *name = arg;
175
176 // find the end of the flag name
177 while (*arg != '\0' && *arg != '=')
178 arg++;
179
180 // get the value if any
181 if (*arg == '=') {
182 // make a copy so we can NUL-terminate flag name
183 int n = static_cast<int>(arg - *name);
henrikg91d6ede2015-09-17 07:24:34184 RTC_CHECK_LT(n, buffer_size);
henrike@webrtc.orgf0488722014-05-13 18:00:26185 memcpy(buffer, *name, n * sizeof(char));
186 buffer[n] = '\0';
187 *name = buffer;
188 // get the value
189 *value = arg + 1;
190 }
191 }
192}
193
194
195int FlagList::SetFlagsFromCommandLine(int* argc, const char** argv,
196 bool remove_flags) {
197 // parse arguments
198 for (int i = 1; i < *argc; /* see below */) {
199 int j = i; // j > 0
200 const char* arg = argv[i++];
201
202 // split arg into flag components
203 char buffer[1024];
204 const char* name;
205 const char* value;
206 bool is_bool;
207 SplitArgument(arg, buffer, sizeof buffer, &name, &value, &is_bool);
208
deadbeef37f5ecf2017-02-27 22:06:41209 if (name != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26210 // lookup the flag
211 Flag* flag = Lookup(name);
deadbeef37f5ecf2017-02-27 22:06:41212 if (flag == nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26213 fprintf(stderr, "Error: unrecognized flag %s\n", arg);
214 return j;
215 }
216
217 // if we still need a flag value, use the next argument if available
deadbeef37f5ecf2017-02-27 22:06:41218 if (flag->type() != Flag::BOOL && value == nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26219 if (i < *argc) {
220 value = argv[i++];
221 } else {
222 fprintf(stderr, "Error: missing value for flag %s of type %s\n",
223 arg, Type2String(flag->type()));
224 return j;
225 }
226 }
227
228 // set the flag
229 char empty[] = { '\0' };
230 char* endp = empty;
231 switch (flag->type()) {
232 case Flag::BOOL:
233 *flag->bool_variable() = !is_bool;
234 break;
235 case Flag::INT:
236 *flag->int_variable() = strtol(value, &endp, 10);
237 break;
238 case Flag::FLOAT:
239 *flag->float_variable() = strtod(value, &endp);
240 break;
241 case Flag::STRING:
242 *flag->string_variable() = value;
243 break;
244 }
245
246 // handle errors
deadbeef37f5ecf2017-02-27 22:06:41247 if ((flag->type() == Flag::BOOL && value != nullptr) ||
248 (flag->type() != Flag::BOOL && is_bool) || *endp != '\0') {
henrike@webrtc.orgf0488722014-05-13 18:00:26249 fprintf(stderr, "Error: illegal value for flag %s of type %s\n",
250 arg, Type2String(flag->type()));
251 return j;
252 }
253
254 // remove the flag & value from the command
255 if (remove_flags)
256 while (j < i)
deadbeef37f5ecf2017-02-27 22:06:41257 argv[j++] = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26258 }
259 }
260
261 // shrink the argument list
262 if (remove_flags) {
263 int j = 1;
264 for (int i = 1; i < *argc; i++) {
deadbeef37f5ecf2017-02-27 22:06:41265 if (argv[i] != nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26266 argv[j++] = argv[i];
267 }
268 *argc = j;
269 }
270
271 // parsed all flags successfully
272 return 0;
273}
274
275void FlagList::Register(Flag* flag) {
kwiberg22487b22016-09-13 08:17:10276 RTC_DCHECK(flag);
kwibergaf476c72016-11-28 23:21:39277 RTC_DCHECK_GT(strlen(flag->name()), 0);
noahric73ab9172016-07-15 01:21:11278 // NOTE: Don't call Lookup() within Register because it accesses the name_
279 // of other flags in list_, and if the flags are coming from two different
280 // compilation units, the initialization order between them is undefined, and
281 // this will trigger an asan initialization-order-fiasco error.
henrike@webrtc.orgf0488722014-05-13 18:00:26282 flag->next_ = list_;
283 list_ = flag;
284}
285
286#if defined(WEBRTC_WIN)
287WindowsCommandLineArguments::WindowsCommandLineArguments() {
288 // start by getting the command line.
289 LPTSTR command_line = ::GetCommandLine();
290 // now, convert it to a list of wide char strings.
291 LPWSTR *wide_argv = ::CommandLineToArgvW(command_line, &argc_);
292 // now allocate an array big enough to hold that many string pointers.
293 argv_ = new char*[argc_];
294
295 // iterate over the returned wide strings;
296 for(int i = 0; i < argc_; ++i) {
297 std::string s = rtc::ToUtf8(wide_argv[i], wcslen(wide_argv[i]));
298 char *buffer = new char[s.length() + 1];
299 rtc::strcpyn(buffer, s.length() + 1, s.c_str());
300
301 // make sure the argv array has the right string at this point.
302 argv_[i] = buffer;
303 }
304 LocalFree(wide_argv);
305}
306
307WindowsCommandLineArguments::~WindowsCommandLineArguments() {
308 // need to free each string in the array, and then the array.
309 for(int i = 0; i < argc_; i++) {
310 delete[] argv_[i];
311 }
312
313 delete[] argv_;
314}
andrew@webrtc.orga5b78692014-08-28 16:28:26315#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26316
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13317} // namespace rtc