blob: 6a87eb4b7b950e83a6a015c5ca992dfc1f572215 [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"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018
19#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +020020// clang-format off
21// clang formating would change include order.
Patrik Höglunda8005cf2017-12-13 16:05:42 +010022#include <windows.h>
Yves Gerey665174f2018-06-19 15:03:05 +020023#include <shellapi.h> // must come after windows.h
24// clang-format on
Yves Gerey2e00abc2018-10-05 15:39:24 +020025
Steve Anton10542f22019-01-11 09:11:00 -080026#include "rtc_base/string_utils.h" // For ToUtf8
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027#endif
28
Edward Lemur260c3982018-02-07 15:45:40 +010029namespace {
30bool FlagEq(const char* arg, const char* flag) {
31 // Compare two flags for equality.
32 // 'arg' is the name of a flag passed via the command line and 'flag' is the
33 // name of a flag defined with the DEFINE_* macros.
34 // We compare the flags for equality, considering hyphens (-) and
35 // underscores (_) to be equivalent, so that --flag-name and --flag_name both
36 // match with --flag_name.
37 while (*arg != '\0' && (*arg == *flag || (*arg == '-' && *flag == '_'))) {
38 ++arg;
39 ++flag;
40 }
41 return *arg == '\0' && *flag == '\0';
42}
43} // namespace
44
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000045namespace rtc {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046// -----------------------------------------------------------------------------
47// Implementation of Flag
48
Yves Gerey665174f2018-06-19 15:03:05 +020049Flag::Flag(const char* file,
50 const char* name,
51 const char* comment,
52 Type type,
53 void* variable,
54 FlagValue default__)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000055 : file_(file),
56 name_(name),
57 comment_(comment),
58 type_(type),
59 variable_(reinterpret_cast<FlagValue*>(variable)),
60 default_(default__) {
61 FlagList::Register(this);
62}
63
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000064void Flag::SetToDefault() {
65 // Note that we cannot simply do '*variable_ = default_;' since
66 // flag variables are not really of type FlagValue and thus may
67 // be smaller! The FlagValue union is simply 'overlayed' on top
68 // of a flag variable for convenient access. Since union members
69 // are guarantee to be aligned at the beginning, this works.
70 switch (type_) {
71 case Flag::BOOL:
72 variable_->b = default_.b;
73 return;
74 case Flag::INT:
75 variable_->i = default_.i;
76 return;
77 case Flag::FLOAT:
78 variable_->f = default_.f;
79 return;
80 case Flag::STRING:
81 variable_->s = default_.s;
82 return;
83 }
andrew@webrtc.orga5b78692014-08-28 16:28:26 +000084 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000085}
86
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087static const char* Type2String(Flag::Type type) {
88 switch (type) {
Yves Gerey665174f2018-06-19 15:03:05 +020089 case Flag::BOOL:
90 return "bool";
91 case Flag::INT:
92 return "int";
93 case Flag::FLOAT:
94 return "float";
95 case Flag::STRING:
96 return "string";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000097 }
andrew@webrtc.orga5b78692014-08-28 16:28:26 +000098 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000099}
100
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000101static void PrintFlagValue(Flag::Type type, FlagValue* p) {
102 switch (type) {
103 case Flag::BOOL:
104 printf("%s", (p->b ? "true" : "false"));
105 return;
106 case Flag::INT:
107 printf("%d", p->i);
108 return;
109 case Flag::FLOAT:
110 printf("%f", p->f);
111 return;
112 case Flag::STRING:
113 printf("%s", p->s);
114 return;
115 }
andrew@webrtc.orga5b78692014-08-28 16:28:26 +0000116 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000117}
118
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000119void Flag::Print(bool print_current_value) {
120 printf(" --%s (%s) type: %s default: ", name_, comment_,
Yves Gerey665174f2018-06-19 15:03:05 +0200121 Type2String(type_));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122 PrintFlagValue(type_, &default_);
123 if (print_current_value) {
124 printf(" current value: ");
125 PrintFlagValue(type_, variable_);
126 }
127 printf("\n");
128}
129
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130// -----------------------------------------------------------------------------
131// Implementation of FlagList
132
deadbeef37f5ecf2017-02-27 14:06:41 -0800133Flag* FlagList::list_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000134
135FlagList::FlagList() {
deadbeef37f5ecf2017-02-27 14:06:41 -0800136 list_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000137}
138
139void FlagList::Print(const char* file, bool print_current_value) {
140 // Since flag registration is likely by file (= C++ file),
141 // we don't need to sort by file and still get grouped output.
deadbeef37f5ecf2017-02-27 14:06:41 -0800142 const char* current = nullptr;
143 for (Flag* f = list_; f != nullptr; f = f->next()) {
144 if (file == nullptr || file == f->file()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000145 if (current != f->file()) {
146 printf("Flags from %s:\n", f->file());
147 current = f->file();
148 }
149 f->Print(print_current_value);
150 }
151 }
152}
153
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154Flag* FlagList::Lookup(const char* name) {
155 Flag* f = list_;
Edward Lemur260c3982018-02-07 15:45:40 +0100156 while (f != nullptr && !FlagEq(name, f->name()))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000157 f = f->next();
158 return f;
159}
160
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000161void FlagList::SplitArgument(const char* arg,
Yves Gerey665174f2018-06-19 15:03:05 +0200162 char* buffer,
163 int buffer_size,
164 const char** name,
165 const char** value,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000166 bool* is_bool) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800167 *name = nullptr;
168 *value = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000169 *is_bool = false;
170
171 if (*arg == '-') {
172 // find the begin of the flag name
173 arg++; // remove 1st '-'
174 if (*arg == '-')
175 arg++; // remove 2nd '-'
oprypin31377a22017-09-04 23:56:42 -0700176 if (arg[0] == 'n' && arg[1] == 'o' && Lookup(arg + 2)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000177 arg += 2; // remove "no"
178 *is_bool = true;
179 }
180 *name = arg;
181
182 // find the end of the flag name
183 while (*arg != '\0' && *arg != '=')
184 arg++;
185
186 // get the value if any
187 if (*arg == '=') {
188 // make a copy so we can NUL-terminate flag name
189 int n = static_cast<int>(arg - *name);
henrikg91d6ede2015-09-17 00:24:34 -0700190 RTC_CHECK_LT(n, buffer_size);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000191 memcpy(buffer, *name, n * sizeof(char));
192 buffer[n] = '\0';
193 *name = buffer;
194 // get the value
195 *value = arg + 1;
196 }
197 }
198}
199
Yves Gerey665174f2018-06-19 15:03:05 +0200200int FlagList::SetFlagsFromCommandLine(int* argc,
201 const char** argv,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000202 bool remove_flags) {
203 // parse arguments
204 for (int i = 1; i < *argc; /* see below */) {
205 int j = i; // j > 0
206 const char* arg = argv[i++];
207
208 // split arg into flag components
209 char buffer[1024];
210 const char* name;
211 const char* value;
212 bool is_bool;
213 SplitArgument(arg, buffer, sizeof buffer, &name, &value, &is_bool);
214
deadbeef37f5ecf2017-02-27 14:06:41 -0800215 if (name != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000216 // lookup the flag
217 Flag* flag = Lookup(name);
deadbeef37f5ecf2017-02-27 14:06:41 -0800218 if (flag == nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000219 fprintf(stderr, "Error: unrecognized flag %s\n", arg);
220 return j;
221 }
222
223 // if we still need a flag value, use the next argument if available
deadbeef37f5ecf2017-02-27 14:06:41 -0800224 if (flag->type() != Flag::BOOL && value == nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000225 if (i < *argc) {
226 value = argv[i++];
227 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200228 fprintf(stderr, "Error: missing value for flag %s of type %s\n", arg,
229 Type2String(flag->type()));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000230 return j;
231 }
232 }
233
234 // set the flag
Yves Gerey665174f2018-06-19 15:03:05 +0200235 char empty[] = {'\0'};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000236 char* endp = empty;
237 switch (flag->type()) {
238 case Flag::BOOL:
239 *flag->bool_variable() = !is_bool;
240 break;
241 case Flag::INT:
242 *flag->int_variable() = strtol(value, &endp, 10);
243 break;
244 case Flag::FLOAT:
245 *flag->float_variable() = strtod(value, &endp);
246 break;
247 case Flag::STRING:
248 *flag->string_variable() = value;
249 break;
250 }
251
252 // handle errors
deadbeef37f5ecf2017-02-27 14:06:41 -0800253 if ((flag->type() == Flag::BOOL && value != nullptr) ||
254 (flag->type() != Flag::BOOL && is_bool) || *endp != '\0') {
Yves Gerey665174f2018-06-19 15:03:05 +0200255 fprintf(stderr, "Error: illegal value for flag %s of type %s\n", arg,
256 Type2String(flag->type()));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000257 return j;
258 }
259
260 // remove the flag & value from the command
261 if (remove_flags)
262 while (j < i)
deadbeef37f5ecf2017-02-27 14:06:41 -0800263 argv[j++] = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000264 }
265 }
266
267 // shrink the argument list
268 if (remove_flags) {
269 int j = 1;
270 for (int i = 1; i < *argc; i++) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800271 if (argv[i] != nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000272 argv[j++] = argv[i];
273 }
274 *argc = j;
275 }
276
277 // parsed all flags successfully
278 return 0;
279}
280
281void FlagList::Register(Flag* flag) {
kwiberg22487b22016-09-13 01:17:10 -0700282 RTC_DCHECK(flag);
kwibergaf476c72016-11-28 15:21:39 -0800283 RTC_DCHECK_GT(strlen(flag->name()), 0);
noahric73ab9172016-07-14 18:21:11 -0700284 // NOTE: Don't call Lookup() within Register because it accesses the name_
285 // of other flags in list_, and if the flags are coming from two different
286 // compilation units, the initialization order between them is undefined, and
287 // this will trigger an asan initialization-order-fiasco error.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000288 flag->next_ = list_;
289 list_ = flag;
290}
291
292#if defined(WEBRTC_WIN)
293WindowsCommandLineArguments::WindowsCommandLineArguments() {
294 // start by getting the command line.
295 LPTSTR command_line = ::GetCommandLine();
Yves Gerey665174f2018-06-19 15:03:05 +0200296 // now, convert it to a list of wide char strings.
297 LPWSTR* wide_argv = ::CommandLineToArgvW(command_line, &argc_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000298 // now allocate an array big enough to hold that many string pointers.
299 argv_ = new char*[argc_];
300
301 // iterate over the returned wide strings;
Yves Gerey665174f2018-06-19 15:03:05 +0200302 for (int i = 0; i < argc_; ++i) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000303 std::string s = rtc::ToUtf8(wide_argv[i], wcslen(wide_argv[i]));
Yves Gerey665174f2018-06-19 15:03:05 +0200304 char* buffer = new char[s.length() + 1];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000305 rtc::strcpyn(buffer, s.length() + 1, s.c_str());
306
307 // make sure the argv array has the right string at this point.
308 argv_[i] = buffer;
309 }
310 LocalFree(wide_argv);
311}
312
313WindowsCommandLineArguments::~WindowsCommandLineArguments() {
314 // need to free each string in the array, and then the array.
Yves Gerey665174f2018-06-19 15:03:05 +0200315 for (int i = 0; i < argc_; i++) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000316 delete[] argv_[i];
317 }
318
319 delete[] argv_;
320}
andrew@webrtc.orga5b78692014-08-28 16:28:26 +0000321#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000322
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +0000323} // namespace rtc