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