blob: a138b8fb9b6812c9e7d680d90de58de19504da1f [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
kwiberg22487b22016-09-13 08:17:1011#include "webrtc/base/flags.h"
12
henrike@webrtc.orgf0488722014-05-13 18:00:2613#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
kwiberg22487b22016-09-13 08:17:1017#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2618
19#if defined(WEBRTC_WIN)
20#include "webrtc/base/win32.h"
21#include <shellapi.h>
22#endif
23
henrike@webrtc.orgc50bf7c2014-05-14 18:24:1324namespace rtc {
henrike@webrtc.orgf0488722014-05-13 18:00:2625// -----------------------------------------------------------------------------
26// Implementation of Flag
27
28Flag::Flag(const char* file, const char* name, const char* comment,
29 Type type, void* variable, FlagValue default__)
30 : file_(file),
31 name_(name),
32 comment_(comment),
33 type_(type),
34 variable_(reinterpret_cast<FlagValue*>(variable)),
35 default_(default__) {
36 FlagList::Register(this);
37}
38
39
40void Flag::SetToDefault() {
41 // Note that we cannot simply do '*variable_ = default_;' since
42 // flag variables are not really of type FlagValue and thus may
43 // be smaller! The FlagValue union is simply 'overlayed' on top
44 // of a flag variable for convenient access. Since union members
45 // are guarantee to be aligned at the beginning, this works.
46 switch (type_) {
47 case Flag::BOOL:
48 variable_->b = default_.b;
49 return;
50 case Flag::INT:
51 variable_->i = default_.i;
52 return;
53 case Flag::FLOAT:
54 variable_->f = default_.f;
55 return;
56 case Flag::STRING:
57 variable_->s = default_.s;
58 return;
59 }
andrew@webrtc.orga5b78692014-08-28 16:28:2660 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:2661}
62
63
64static const char* Type2String(Flag::Type type) {
65 switch (type) {
66 case Flag::BOOL: return "bool";
67 case Flag::INT: return "int";
68 case Flag::FLOAT: return "float";
69 case Flag::STRING: return "string";
70 }
andrew@webrtc.orga5b78692014-08-28 16:28:2671 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:2672}
73
74
75static void PrintFlagValue(Flag::Type type, FlagValue* p) {
76 switch (type) {
77 case Flag::BOOL:
78 printf("%s", (p->b ? "true" : "false"));
79 return;
80 case Flag::INT:
81 printf("%d", p->i);
82 return;
83 case Flag::FLOAT:
84 printf("%f", p->f);
85 return;
86 case Flag::STRING:
87 printf("%s", p->s);
88 return;
89 }
andrew@webrtc.orga5b78692014-08-28 16:28:2690 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:2691}
92
93
94void Flag::Print(bool print_current_value) {
95 printf(" --%s (%s) type: %s default: ", name_, comment_,
96 Type2String(type_));
97 PrintFlagValue(type_, &default_);
98 if (print_current_value) {
99 printf(" current value: ");
100 PrintFlagValue(type_, variable_);
101 }
102 printf("\n");
103}
104
105
106// -----------------------------------------------------------------------------
107// Implementation of FlagList
108
109Flag* FlagList::list_ = NULL;
110
111
112FlagList::FlagList() {
113 list_ = NULL;
114}
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.
119 const char* current = NULL;
120 for (Flag* f = list_; f != NULL; f = f->next()) {
121 if (file == NULL || file == f->file()) {
122 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_;
134 while (f != NULL && strcmp(name, f->name()) != 0)
135 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) {
144 *name = NULL;
145 *value = NULL;
146 *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 '-'
153 if (arg[0] == 'n' && arg[1] == 'o') {
154 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 07:24:34167 RTC_CHECK_LT(n, buffer_size);
henrike@webrtc.orgf0488722014-05-13 18:00:26168 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
192 if (name != NULL) {
193 // lookup the flag
194 Flag* flag = Lookup(name);
195 if (flag == NULL) {
196 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
201 if (flag->type() != Flag::BOOL && value == NULL) {
202 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
230 if ((flag->type() == Flag::BOOL && value != NULL) ||
231 (flag->type() != Flag::BOOL && is_bool) ||
232 *endp != '\0') {
233 fprintf(stderr, "Error: illegal value for flag %s of type %s\n",
234 arg, Type2String(flag->type()));
235 return j;
236 }
237
238 // remove the flag & value from the command
239 if (remove_flags)
240 while (j < i)
241 argv[j++] = NULL;
242 }
243 }
244
245 // shrink the argument list
246 if (remove_flags) {
247 int j = 1;
248 for (int i = 1; i < *argc; i++) {
249 if (argv[i] != NULL)
250 argv[j++] = argv[i];
251 }
252 *argc = j;
253 }
254
255 // parsed all flags successfully
256 return 0;
257}
258
259void FlagList::Register(Flag* flag) {
kwiberg22487b22016-09-13 08:17:10260 RTC_DCHECK(flag);
261 RTC_DCHECK_GT(strlen(flag->name()), 0u);
noahric73ab9172016-07-15 01:21:11262 // NOTE: Don't call Lookup() within Register because it accesses the name_
263 // of other flags in list_, and if the flags are coming from two different
264 // compilation units, the initialization order between them is undefined, and
265 // this will trigger an asan initialization-order-fiasco error.
henrike@webrtc.orgf0488722014-05-13 18:00:26266 flag->next_ = list_;
267 list_ = flag;
268}
269
270#if defined(WEBRTC_WIN)
271WindowsCommandLineArguments::WindowsCommandLineArguments() {
272 // start by getting the command line.
273 LPTSTR command_line = ::GetCommandLine();
274 // now, convert it to a list of wide char strings.
275 LPWSTR *wide_argv = ::CommandLineToArgvW(command_line, &argc_);
276 // now allocate an array big enough to hold that many string pointers.
277 argv_ = new char*[argc_];
278
279 // iterate over the returned wide strings;
280 for(int i = 0; i < argc_; ++i) {
281 std::string s = rtc::ToUtf8(wide_argv[i], wcslen(wide_argv[i]));
282 char *buffer = new char[s.length() + 1];
283 rtc::strcpyn(buffer, s.length() + 1, s.c_str());
284
285 // make sure the argv array has the right string at this point.
286 argv_[i] = buffer;
287 }
288 LocalFree(wide_argv);
289}
290
291WindowsCommandLineArguments::~WindowsCommandLineArguments() {
292 // need to free each string in the array, and then the array.
293 for(int i = 0; i < argc_; i++) {
294 delete[] argv_[i];
295 }
296
297 delete[] argv_;
298}
andrew@webrtc.orga5b78692014-08-28 16:28:26299#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26300
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13301} // namespace rtc