blob: b4137981e3c5a5ef9d3b232faaeefa3ab7e8d063 [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)
Patrik Höglunda8005cf2017-12-13 16:05:42 +010021#include <windows.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022#include <shellapi.h>
23#endif
24
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000025namespace rtc {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026// -----------------------------------------------------------------------------
27// Implementation of Flag
28
29Flag::Flag(const char* file, const char* name, const char* comment,
30 Type type, void* variable, FlagValue default__)
31 : file_(file),
32 name_(name),
33 comment_(comment),
34 type_(type),
35 variable_(reinterpret_cast<FlagValue*>(variable)),
36 default_(default__) {
37 FlagList::Register(this);
38}
39
40
41void Flag::SetToDefault() {
42 // Note that we cannot simply do '*variable_ = default_;' since
43 // flag variables are not really of type FlagValue and thus may
44 // be smaller! The FlagValue union is simply 'overlayed' on top
45 // of a flag variable for convenient access. Since union members
46 // are guarantee to be aligned at the beginning, this works.
47 switch (type_) {
48 case Flag::BOOL:
49 variable_->b = default_.b;
50 return;
51 case Flag::INT:
52 variable_->i = default_.i;
53 return;
54 case Flag::FLOAT:
55 variable_->f = default_.f;
56 return;
57 case Flag::STRING:
58 variable_->s = default_.s;
59 return;
60 }
andrew@webrtc.orga5b78692014-08-28 16:28:26 +000061 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062}
63
64
65static const char* Type2String(Flag::Type type) {
66 switch (type) {
67 case Flag::BOOL: return "bool";
68 case Flag::INT: return "int";
69 case Flag::FLOAT: return "float";
70 case Flag::STRING: return "string";
71 }
andrew@webrtc.orga5b78692014-08-28 16:28:26 +000072 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073}
74
75
76static void PrintFlagValue(Flag::Type type, FlagValue* p) {
77 switch (type) {
78 case Flag::BOOL:
79 printf("%s", (p->b ? "true" : "false"));
80 return;
81 case Flag::INT:
82 printf("%d", p->i);
83 return;
84 case Flag::FLOAT:
85 printf("%f", p->f);
86 return;
87 case Flag::STRING:
88 printf("%s", p->s);
89 return;
90 }
andrew@webrtc.orga5b78692014-08-28 16:28:26 +000091 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000092}
93
94
95void Flag::Print(bool print_current_value) {
96 printf(" --%s (%s) type: %s default: ", name_, comment_,
97 Type2String(type_));
98 PrintFlagValue(type_, &default_);
99 if (print_current_value) {
100 printf(" current value: ");
101 PrintFlagValue(type_, variable_);
102 }
103 printf("\n");
104}
105
106
107// -----------------------------------------------------------------------------
108// Implementation of FlagList
109
deadbeef37f5ecf2017-02-27 14:06:41 -0800110Flag* FlagList::list_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000111
112FlagList::FlagList() {
deadbeef37f5ecf2017-02-27 14:06:41 -0800113 list_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000114}
115
116void FlagList::Print(const char* file, bool print_current_value) {
117 // Since flag registration is likely by file (= C++ file),
118 // we don't need to sort by file and still get grouped output.
deadbeef37f5ecf2017-02-27 14:06:41 -0800119 const char* current = nullptr;
120 for (Flag* f = list_; f != nullptr; f = f->next()) {
121 if (file == nullptr || file == f->file()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122 if (current != f->file()) {
123 printf("Flags from %s:\n", f->file());
124 current = f->file();
125 }
126 f->Print(print_current_value);
127 }
128 }
129}
130
131
132Flag* FlagList::Lookup(const char* name) {
133 Flag* f = list_;
deadbeef37f5ecf2017-02-27 14:06:41 -0800134 while (f != nullptr && strcmp(name, f->name()) != 0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000135 f = f->next();
136 return f;
137}
138
139
140void FlagList::SplitArgument(const char* arg,
141 char* buffer, int buffer_size,
142 const char** name, const char** value,
143 bool* is_bool) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800144 *name = nullptr;
145 *value = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146 *is_bool = false;
147
148 if (*arg == '-') {
149 // find the begin of the flag name
150 arg++; // remove 1st '-'
151 if (*arg == '-')
152 arg++; // remove 2nd '-'
oprypin31377a22017-09-04 23:56:42 -0700153 if (arg[0] == 'n' && arg[1] == 'o' && Lookup(arg + 2)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154 arg += 2; // remove "no"
155 *is_bool = true;
156 }
157 *name = arg;
158
159 // find the end of the flag name
160 while (*arg != '\0' && *arg != '=')
161 arg++;
162
163 // get the value if any
164 if (*arg == '=') {
165 // make a copy so we can NUL-terminate flag name
166 int n = static_cast<int>(arg - *name);
henrikg91d6ede2015-09-17 00:24:34 -0700167 RTC_CHECK_LT(n, buffer_size);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168 memcpy(buffer, *name, n * sizeof(char));
169 buffer[n] = '\0';
170 *name = buffer;
171 // get the value
172 *value = arg + 1;
173 }
174 }
175}
176
177
178int FlagList::SetFlagsFromCommandLine(int* argc, const char** argv,
179 bool remove_flags) {
180 // parse arguments
181 for (int i = 1; i < *argc; /* see below */) {
182 int j = i; // j > 0
183 const char* arg = argv[i++];
184
185 // split arg into flag components
186 char buffer[1024];
187 const char* name;
188 const char* value;
189 bool is_bool;
190 SplitArgument(arg, buffer, sizeof buffer, &name, &value, &is_bool);
191
deadbeef37f5ecf2017-02-27 14:06:41 -0800192 if (name != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000193 // lookup the flag
194 Flag* flag = Lookup(name);
deadbeef37f5ecf2017-02-27 14:06:41 -0800195 if (flag == nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000196 fprintf(stderr, "Error: unrecognized flag %s\n", arg);
197 return j;
198 }
199
200 // if we still need a flag value, use the next argument if available
deadbeef37f5ecf2017-02-27 14:06:41 -0800201 if (flag->type() != Flag::BOOL && value == nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000202 if (i < *argc) {
203 value = argv[i++];
204 } else {
205 fprintf(stderr, "Error: missing value for flag %s of type %s\n",
206 arg, Type2String(flag->type()));
207 return j;
208 }
209 }
210
211 // set the flag
212 char empty[] = { '\0' };
213 char* endp = empty;
214 switch (flag->type()) {
215 case Flag::BOOL:
216 *flag->bool_variable() = !is_bool;
217 break;
218 case Flag::INT:
219 *flag->int_variable() = strtol(value, &endp, 10);
220 break;
221 case Flag::FLOAT:
222 *flag->float_variable() = strtod(value, &endp);
223 break;
224 case Flag::STRING:
225 *flag->string_variable() = value;
226 break;
227 }
228
229 // handle errors
deadbeef37f5ecf2017-02-27 14:06:41 -0800230 if ((flag->type() == Flag::BOOL && value != nullptr) ||
231 (flag->type() != Flag::BOOL && is_bool) || *endp != '\0') {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000232 fprintf(stderr, "Error: illegal value for flag %s of type %s\n",
233 arg, Type2String(flag->type()));
234 return j;
235 }
236
237 // remove the flag & value from the command
238 if (remove_flags)
239 while (j < i)
deadbeef37f5ecf2017-02-27 14:06:41 -0800240 argv[j++] = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000241 }
242 }
243
244 // shrink the argument list
245 if (remove_flags) {
246 int j = 1;
247 for (int i = 1; i < *argc; i++) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800248 if (argv[i] != nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000249 argv[j++] = argv[i];
250 }
251 *argc = j;
252 }
253
254 // parsed all flags successfully
255 return 0;
256}
257
258void FlagList::Register(Flag* flag) {
kwiberg22487b22016-09-13 01:17:10 -0700259 RTC_DCHECK(flag);
kwibergaf476c72016-11-28 15:21:39 -0800260 RTC_DCHECK_GT(strlen(flag->name()), 0);
noahric73ab9172016-07-14 18:21:11 -0700261 // NOTE: Don't call Lookup() within Register because it accesses the name_
262 // of other flags in list_, and if the flags are coming from two different
263 // compilation units, the initialization order between them is undefined, and
264 // this will trigger an asan initialization-order-fiasco error.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000265 flag->next_ = list_;
266 list_ = flag;
267}
268
269#if defined(WEBRTC_WIN)
270WindowsCommandLineArguments::WindowsCommandLineArguments() {
271 // start by getting the command line.
272 LPTSTR command_line = ::GetCommandLine();
273 // now, convert it to a list of wide char strings.
274 LPWSTR *wide_argv = ::CommandLineToArgvW(command_line, &argc_);
275 // now allocate an array big enough to hold that many string pointers.
276 argv_ = new char*[argc_];
277
278 // iterate over the returned wide strings;
279 for(int i = 0; i < argc_; ++i) {
280 std::string s = rtc::ToUtf8(wide_argv[i], wcslen(wide_argv[i]));
281 char *buffer = new char[s.length() + 1];
282 rtc::strcpyn(buffer, s.length() + 1, s.c_str());
283
284 // make sure the argv array has the right string at this point.
285 argv_[i] = buffer;
286 }
287 LocalFree(wide_argv);
288}
289
290WindowsCommandLineArguments::~WindowsCommandLineArguments() {
291 // need to free each string in the array, and then the array.
292 for(int i = 0; i < argc_; i++) {
293 delete[] argv_[i];
294 }
295
296 delete[] argv_;
297}
andrew@webrtc.orga5b78692014-08-28 16:28:26 +0000298#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000299
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +0000300} // namespace rtc