henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/flags.h" |
kwiberg | 22487b2 | 2016-09-13 01:17:10 -0700 | [diff] [blame] | 12 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
Patrik Höglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 18 | #include "rtc_base/stringutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | |
| 20 | #if defined(WEBRTC_WIN) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 21 | // clang-format off |
| 22 | // clang formating would change include order. |
Patrik Höglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 23 | #include <windows.h> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 24 | #include <shellapi.h> // must come after windows.h |
| 25 | // clang-format on |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 26 | #endif |
| 27 | |
Edward Lemur | 260c398 | 2018-02-07 15:45:40 +0100 | [diff] [blame] | 28 | namespace { |
| 29 | bool FlagEq(const char* arg, const char* flag) { |
| 30 | // Compare two flags for equality. |
| 31 | // 'arg' is the name of a flag passed via the command line and 'flag' is the |
| 32 | // name of a flag defined with the DEFINE_* macros. |
| 33 | // We compare the flags for equality, considering hyphens (-) and |
| 34 | // underscores (_) to be equivalent, so that --flag-name and --flag_name both |
| 35 | // match with --flag_name. |
| 36 | while (*arg != '\0' && (*arg == *flag || (*arg == '-' && *flag == '_'))) { |
| 37 | ++arg; |
| 38 | ++flag; |
| 39 | } |
| 40 | return *arg == '\0' && *flag == '\0'; |
| 41 | } |
| 42 | } // namespace |
| 43 | |
henrike@webrtc.org | c50bf7c | 2014-05-14 18:24:13 +0000 | [diff] [blame] | 44 | namespace rtc { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 45 | // ----------------------------------------------------------------------------- |
| 46 | // Implementation of Flag |
| 47 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 48 | Flag::Flag(const char* file, |
| 49 | const char* name, |
| 50 | const char* comment, |
| 51 | Type type, |
| 52 | void* variable, |
| 53 | FlagValue default__) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 54 | : file_(file), |
| 55 | name_(name), |
| 56 | comment_(comment), |
| 57 | type_(type), |
| 58 | variable_(reinterpret_cast<FlagValue*>(variable)), |
| 59 | default_(default__) { |
| 60 | FlagList::Register(this); |
| 61 | } |
| 62 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 63 | void Flag::SetToDefault() { |
| 64 | // Note that we cannot simply do '*variable_ = default_;' since |
| 65 | // flag variables are not really of type FlagValue and thus may |
| 66 | // be smaller! The FlagValue union is simply 'overlayed' on top |
| 67 | // of a flag variable for convenient access. Since union members |
| 68 | // are guarantee to be aligned at the beginning, this works. |
| 69 | switch (type_) { |
| 70 | case Flag::BOOL: |
| 71 | variable_->b = default_.b; |
| 72 | return; |
| 73 | case Flag::INT: |
| 74 | variable_->i = default_.i; |
| 75 | return; |
| 76 | case Flag::FLOAT: |
| 77 | variable_->f = default_.f; |
| 78 | return; |
| 79 | case Flag::STRING: |
| 80 | variable_->s = default_.s; |
| 81 | return; |
| 82 | } |
andrew@webrtc.org | a5b7869 | 2014-08-28 16:28:26 +0000 | [diff] [blame] | 83 | FATAL() << "unreachable code"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 84 | } |
| 85 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 86 | static const char* Type2String(Flag::Type type) { |
| 87 | switch (type) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 88 | case Flag::BOOL: |
| 89 | return "bool"; |
| 90 | case Flag::INT: |
| 91 | return "int"; |
| 92 | case Flag::FLOAT: |
| 93 | return "float"; |
| 94 | case Flag::STRING: |
| 95 | return "string"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 96 | } |
andrew@webrtc.org | a5b7869 | 2014-08-28 16:28:26 +0000 | [diff] [blame] | 97 | FATAL() << "unreachable code"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 98 | } |
| 99 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 100 | static void PrintFlagValue(Flag::Type type, FlagValue* p) { |
| 101 | switch (type) { |
| 102 | case Flag::BOOL: |
| 103 | printf("%s", (p->b ? "true" : "false")); |
| 104 | return; |
| 105 | case Flag::INT: |
| 106 | printf("%d", p->i); |
| 107 | return; |
| 108 | case Flag::FLOAT: |
| 109 | printf("%f", p->f); |
| 110 | return; |
| 111 | case Flag::STRING: |
| 112 | printf("%s", p->s); |
| 113 | return; |
| 114 | } |
andrew@webrtc.org | a5b7869 | 2014-08-28 16:28:26 +0000 | [diff] [blame] | 115 | FATAL() << "unreachable code"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 116 | } |
| 117 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 118 | void Flag::Print(bool print_current_value) { |
| 119 | printf(" --%s (%s) type: %s default: ", name_, comment_, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 120 | Type2String(type_)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 121 | PrintFlagValue(type_, &default_); |
| 122 | if (print_current_value) { |
| 123 | printf(" current value: "); |
| 124 | PrintFlagValue(type_, variable_); |
| 125 | } |
| 126 | printf("\n"); |
| 127 | } |
| 128 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 129 | // ----------------------------------------------------------------------------- |
| 130 | // Implementation of FlagList |
| 131 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 132 | Flag* FlagList::list_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 133 | |
| 134 | FlagList::FlagList() { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 135 | list_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void FlagList::Print(const char* file, bool print_current_value) { |
| 139 | // Since flag registration is likely by file (= C++ file), |
| 140 | // we don't need to sort by file and still get grouped output. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 141 | const char* current = nullptr; |
| 142 | for (Flag* f = list_; f != nullptr; f = f->next()) { |
| 143 | if (file == nullptr || file == f->file()) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 144 | if (current != f->file()) { |
| 145 | printf("Flags from %s:\n", f->file()); |
| 146 | current = f->file(); |
| 147 | } |
| 148 | f->Print(print_current_value); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 153 | Flag* FlagList::Lookup(const char* name) { |
| 154 | Flag* f = list_; |
Edward Lemur | 260c398 | 2018-02-07 15:45:40 +0100 | [diff] [blame] | 155 | while (f != nullptr && !FlagEq(name, f->name())) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 156 | f = f->next(); |
| 157 | return f; |
| 158 | } |
| 159 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 160 | void FlagList::SplitArgument(const char* arg, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 161 | char* buffer, |
| 162 | int buffer_size, |
| 163 | const char** name, |
| 164 | const char** value, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 165 | bool* is_bool) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 166 | *name = nullptr; |
| 167 | *value = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 168 | *is_bool = false; |
| 169 | |
| 170 | if (*arg == '-') { |
| 171 | // find the begin of the flag name |
| 172 | arg++; // remove 1st '-' |
| 173 | if (*arg == '-') |
| 174 | arg++; // remove 2nd '-' |
oprypin | 31377a2 | 2017-09-04 23:56:42 -0700 | [diff] [blame] | 175 | if (arg[0] == 'n' && arg[1] == 'o' && Lookup(arg + 2)) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 176 | arg += 2; // remove "no" |
| 177 | *is_bool = true; |
| 178 | } |
| 179 | *name = arg; |
| 180 | |
| 181 | // find the end of the flag name |
| 182 | while (*arg != '\0' && *arg != '=') |
| 183 | arg++; |
| 184 | |
| 185 | // get the value if any |
| 186 | if (*arg == '=') { |
| 187 | // make a copy so we can NUL-terminate flag name |
| 188 | int n = static_cast<int>(arg - *name); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 189 | RTC_CHECK_LT(n, buffer_size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 190 | memcpy(buffer, *name, n * sizeof(char)); |
| 191 | buffer[n] = '\0'; |
| 192 | *name = buffer; |
| 193 | // get the value |
| 194 | *value = arg + 1; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 199 | int FlagList::SetFlagsFromCommandLine(int* argc, |
| 200 | const char** argv, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 201 | bool remove_flags) { |
| 202 | // parse arguments |
| 203 | for (int i = 1; i < *argc; /* see below */) { |
| 204 | int j = i; // j > 0 |
| 205 | const char* arg = argv[i++]; |
| 206 | |
| 207 | // split arg into flag components |
| 208 | char buffer[1024]; |
| 209 | const char* name; |
| 210 | const char* value; |
| 211 | bool is_bool; |
| 212 | SplitArgument(arg, buffer, sizeof buffer, &name, &value, &is_bool); |
| 213 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 214 | if (name != nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 215 | // lookup the flag |
| 216 | Flag* flag = Lookup(name); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 217 | if (flag == nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 218 | fprintf(stderr, "Error: unrecognized flag %s\n", arg); |
| 219 | return j; |
| 220 | } |
| 221 | |
| 222 | // if we still need a flag value, use the next argument if available |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 223 | if (flag->type() != Flag::BOOL && value == nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 224 | if (i < *argc) { |
| 225 | value = argv[i++]; |
| 226 | } else { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 227 | fprintf(stderr, "Error: missing value for flag %s of type %s\n", arg, |
| 228 | Type2String(flag->type())); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 229 | return j; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // set the flag |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 234 | char empty[] = {'\0'}; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 235 | char* endp = empty; |
| 236 | switch (flag->type()) { |
| 237 | case Flag::BOOL: |
| 238 | *flag->bool_variable() = !is_bool; |
| 239 | break; |
| 240 | case Flag::INT: |
| 241 | *flag->int_variable() = strtol(value, &endp, 10); |
| 242 | break; |
| 243 | case Flag::FLOAT: |
| 244 | *flag->float_variable() = strtod(value, &endp); |
| 245 | break; |
| 246 | case Flag::STRING: |
| 247 | *flag->string_variable() = value; |
| 248 | break; |
| 249 | } |
| 250 | |
| 251 | // handle errors |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 252 | if ((flag->type() == Flag::BOOL && value != nullptr) || |
| 253 | (flag->type() != Flag::BOOL && is_bool) || *endp != '\0') { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 254 | fprintf(stderr, "Error: illegal value for flag %s of type %s\n", arg, |
| 255 | Type2String(flag->type())); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 256 | return j; |
| 257 | } |
| 258 | |
| 259 | // remove the flag & value from the command |
| 260 | if (remove_flags) |
| 261 | while (j < i) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 262 | argv[j++] = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | // shrink the argument list |
| 267 | if (remove_flags) { |
| 268 | int j = 1; |
| 269 | for (int i = 1; i < *argc; i++) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 270 | if (argv[i] != nullptr) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 271 | argv[j++] = argv[i]; |
| 272 | } |
| 273 | *argc = j; |
| 274 | } |
| 275 | |
| 276 | // parsed all flags successfully |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | void FlagList::Register(Flag* flag) { |
kwiberg | 22487b2 | 2016-09-13 01:17:10 -0700 | [diff] [blame] | 281 | RTC_DCHECK(flag); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 282 | RTC_DCHECK_GT(strlen(flag->name()), 0); |
noahric | 73ab917 | 2016-07-14 18:21:11 -0700 | [diff] [blame] | 283 | // NOTE: Don't call Lookup() within Register because it accesses the name_ |
| 284 | // of other flags in list_, and if the flags are coming from two different |
| 285 | // compilation units, the initialization order between them is undefined, and |
| 286 | // this will trigger an asan initialization-order-fiasco error. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 287 | flag->next_ = list_; |
| 288 | list_ = flag; |
| 289 | } |
| 290 | |
| 291 | #if defined(WEBRTC_WIN) |
| 292 | WindowsCommandLineArguments::WindowsCommandLineArguments() { |
| 293 | // start by getting the command line. |
| 294 | LPTSTR command_line = ::GetCommandLine(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 295 | // now, convert it to a list of wide char strings. |
| 296 | LPWSTR* wide_argv = ::CommandLineToArgvW(command_line, &argc_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 297 | // now allocate an array big enough to hold that many string pointers. |
| 298 | argv_ = new char*[argc_]; |
| 299 | |
| 300 | // iterate over the returned wide strings; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 301 | for (int i = 0; i < argc_; ++i) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 302 | std::string s = rtc::ToUtf8(wide_argv[i], wcslen(wide_argv[i])); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 303 | char* buffer = new char[s.length() + 1]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 304 | rtc::strcpyn(buffer, s.length() + 1, s.c_str()); |
| 305 | |
| 306 | // make sure the argv array has the right string at this point. |
| 307 | argv_[i] = buffer; |
| 308 | } |
| 309 | LocalFree(wide_argv); |
| 310 | } |
| 311 | |
| 312 | WindowsCommandLineArguments::~WindowsCommandLineArguments() { |
| 313 | // need to free each string in the array, and then the array. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 314 | for (int i = 0; i < argc_; i++) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 315 | delete[] argv_[i]; |
| 316 | } |
| 317 | |
| 318 | delete[] argv_; |
| 319 | } |
andrew@webrtc.org | a5b7869 | 2014-08-28 16:28:26 +0000 | [diff] [blame] | 320 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 321 | |
henrike@webrtc.org | c50bf7c | 2014-05-14 18:24:13 +0000 | [diff] [blame] | 322 | } // namespace rtc |