blob: ca32c730e54575bc86f255ed39bf8d996d851850 [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
Edward Lemur260c3982018-02-07 15:45:40 +010019namespace {
20bool FlagEq(const char* arg, const char* flag) {
21 // Compare two flags for equality.
22 // 'arg' is the name of a flag passed via the command line and 'flag' is the
23 // name of a flag defined with the DEFINE_* macros.
24 // We compare the flags for equality, considering hyphens (-) and
25 // underscores (_) to be equivalent, so that --flag-name and --flag_name both
26 // match with --flag_name.
27 while (*arg != '\0' && (*arg == *flag || (*arg == '-' && *flag == '_'))) {
28 ++arg;
29 ++flag;
30 }
31 return *arg == '\0' && *flag == '\0';
32}
33} // namespace
34
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000035namespace rtc {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036// -----------------------------------------------------------------------------
37// Implementation of Flag
38
Yves Gerey665174f2018-06-19 15:03:05 +020039Flag::Flag(const char* file,
40 const char* name,
41 const char* comment,
42 Type type,
43 void* variable,
44 FlagValue default__)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000045 : file_(file),
46 name_(name),
47 comment_(comment),
48 type_(type),
49 variable_(reinterpret_cast<FlagValue*>(variable)),
50 default_(default__) {
51 FlagList::Register(this);
52}
53
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000054void Flag::SetToDefault() {
55 // Note that we cannot simply do '*variable_ = default_;' since
56 // flag variables are not really of type FlagValue and thus may
57 // be smaller! The FlagValue union is simply 'overlayed' on top
58 // of a flag variable for convenient access. Since union members
59 // are guarantee to be aligned at the beginning, this works.
60 switch (type_) {
61 case Flag::BOOL:
62 variable_->b = default_.b;
63 return;
64 case Flag::INT:
65 variable_->i = default_.i;
66 return;
67 case Flag::FLOAT:
68 variable_->f = default_.f;
69 return;
70 case Flag::STRING:
71 variable_->s = default_.s;
72 return;
73 }
andrew@webrtc.orga5b78692014-08-28 16:28:26 +000074 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075}
76
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000077static const char* Type2String(Flag::Type type) {
78 switch (type) {
Yves Gerey665174f2018-06-19 15:03:05 +020079 case Flag::BOOL:
80 return "bool";
81 case Flag::INT:
82 return "int";
83 case Flag::FLOAT:
84 return "float";
85 case Flag::STRING:
86 return "string";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087 }
andrew@webrtc.orga5b78692014-08-28 16:28:26 +000088 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089}
90
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000091static void PrintFlagValue(Flag::Type type, FlagValue* p) {
92 switch (type) {
93 case Flag::BOOL:
94 printf("%s", (p->b ? "true" : "false"));
95 return;
96 case Flag::INT:
97 printf("%d", p->i);
98 return;
99 case Flag::FLOAT:
100 printf("%f", p->f);
101 return;
102 case Flag::STRING:
103 printf("%s", p->s);
104 return;
105 }
andrew@webrtc.orga5b78692014-08-28 16:28:26 +0000106 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000107}
108
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000109void Flag::Print(bool print_current_value) {
110 printf(" --%s (%s) type: %s default: ", name_, comment_,
Yves Gerey665174f2018-06-19 15:03:05 +0200111 Type2String(type_));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000112 PrintFlagValue(type_, &default_);
113 if (print_current_value) {
114 printf(" current value: ");
115 PrintFlagValue(type_, variable_);
116 }
117 printf("\n");
118}
119
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000120// -----------------------------------------------------------------------------
121// Implementation of FlagList
122
deadbeef37f5ecf2017-02-27 14:06:41 -0800123Flag* FlagList::list_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000124
125FlagList::FlagList() {
deadbeef37f5ecf2017-02-27 14:06:41 -0800126 list_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127}
128
129void FlagList::Print(const char* file, bool print_current_value) {
130 // Since flag registration is likely by file (= C++ file),
131 // we don't need to sort by file and still get grouped output.
deadbeef37f5ecf2017-02-27 14:06:41 -0800132 const char* current = nullptr;
133 for (Flag* f = list_; f != nullptr; f = f->next()) {
134 if (file == nullptr || file == f->file()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000135 if (current != f->file()) {
136 printf("Flags from %s:\n", f->file());
137 current = f->file();
138 }
139 f->Print(print_current_value);
140 }
141 }
142}
143
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000144Flag* FlagList::Lookup(const char* name) {
145 Flag* f = list_;
Edward Lemur260c3982018-02-07 15:45:40 +0100146 while (f != nullptr && !FlagEq(name, f->name()))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000147 f = f->next();
148 return f;
149}
150
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000151void FlagList::SplitArgument(const char* arg,
Yves Gerey665174f2018-06-19 15:03:05 +0200152 char* buffer,
153 int buffer_size,
154 const char** name,
155 const char** value,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000156 bool* is_bool) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800157 *name = nullptr;
158 *value = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000159 *is_bool = false;
160
161 if (*arg == '-') {
162 // find the begin of the flag name
163 arg++; // remove 1st '-'
164 if (*arg == '-')
165 arg++; // remove 2nd '-'
oprypin31377a22017-09-04 23:56:42 -0700166 if (arg[0] == 'n' && arg[1] == 'o' && Lookup(arg + 2)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000167 arg += 2; // remove "no"
168 *is_bool = true;
169 }
170 *name = arg;
171
172 // find the end of the flag name
173 while (*arg != '\0' && *arg != '=')
174 arg++;
175
176 // get the value if any
177 if (*arg == '=') {
178 // make a copy so we can NUL-terminate flag name
179 int n = static_cast<int>(arg - *name);
henrikg91d6ede2015-09-17 00:24:34 -0700180 RTC_CHECK_LT(n, buffer_size);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000181 memcpy(buffer, *name, n * sizeof(char));
182 buffer[n] = '\0';
183 *name = buffer;
184 // get the value
185 *value = arg + 1;
186 }
187 }
188}
189
Yves Gerey665174f2018-06-19 15:03:05 +0200190int FlagList::SetFlagsFromCommandLine(int* argc,
191 const char** argv,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000192 bool remove_flags) {
193 // parse arguments
194 for (int i = 1; i < *argc; /* see below */) {
195 int j = i; // j > 0
196 const char* arg = argv[i++];
197
198 // split arg into flag components
199 char buffer[1024];
200 const char* name;
201 const char* value;
202 bool is_bool;
203 SplitArgument(arg, buffer, sizeof buffer, &name, &value, &is_bool);
204
deadbeef37f5ecf2017-02-27 14:06:41 -0800205 if (name != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000206 // lookup the flag
207 Flag* flag = Lookup(name);
deadbeef37f5ecf2017-02-27 14:06:41 -0800208 if (flag == nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000209 fprintf(stderr, "Error: unrecognized flag %s\n", arg);
210 return j;
211 }
212
213 // if we still need a flag value, use the next argument if available
deadbeef37f5ecf2017-02-27 14:06:41 -0800214 if (flag->type() != Flag::BOOL && value == nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000215 if (i < *argc) {
216 value = argv[i++];
217 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200218 fprintf(stderr, "Error: missing value for flag %s of type %s\n", arg,
219 Type2String(flag->type()));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000220 return j;
221 }
222 }
223
224 // set the flag
Yves Gerey665174f2018-06-19 15:03:05 +0200225 char empty[] = {'\0'};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000226 char* endp = empty;
227 switch (flag->type()) {
228 case Flag::BOOL:
229 *flag->bool_variable() = !is_bool;
230 break;
231 case Flag::INT:
232 *flag->int_variable() = strtol(value, &endp, 10);
233 break;
234 case Flag::FLOAT:
235 *flag->float_variable() = strtod(value, &endp);
236 break;
237 case Flag::STRING:
238 *flag->string_variable() = value;
239 break;
240 }
241
242 // handle errors
deadbeef37f5ecf2017-02-27 14:06:41 -0800243 if ((flag->type() == Flag::BOOL && value != nullptr) ||
244 (flag->type() != Flag::BOOL && is_bool) || *endp != '\0') {
Yves Gerey665174f2018-06-19 15:03:05 +0200245 fprintf(stderr, "Error: illegal value for flag %s of type %s\n", arg,
246 Type2String(flag->type()));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000247 return j;
248 }
249
250 // remove the flag & value from the command
251 if (remove_flags)
252 while (j < i)
deadbeef37f5ecf2017-02-27 14:06:41 -0800253 argv[j++] = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000254 }
255 }
256
257 // shrink the argument list
258 if (remove_flags) {
259 int j = 1;
260 for (int i = 1; i < *argc; i++) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800261 if (argv[i] != nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000262 argv[j++] = argv[i];
263 }
264 *argc = j;
265 }
266
267 // parsed all flags successfully
268 return 0;
269}
270
271void FlagList::Register(Flag* flag) {
kwiberg22487b22016-09-13 01:17:10 -0700272 RTC_DCHECK(flag);
kwibergaf476c72016-11-28 15:21:39 -0800273 RTC_DCHECK_GT(strlen(flag->name()), 0);
noahric73ab9172016-07-14 18:21:11 -0700274 // NOTE: Don't call Lookup() within Register because it accesses the name_
275 // of other flags in list_, and if the flags are coming from two different
276 // compilation units, the initialization order between them is undefined, and
277 // this will trigger an asan initialization-order-fiasco error.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000278 flag->next_ = list_;
279 list_ = flag;
280}
281
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +0000282} // namespace rtc