| 6 |
| 7 | #include <algorithm> |
| 8 | #include <ostream> |
| 9 |
| 10 | #include "base/containers/span.h" |
| 11 | #include "base/files/file_path.h" |
| 12 | #include "base/logging.h" |
| 13 | #include "base/stl_util.h" |
| 14 | #include "base/strings/string_split.h" |
| 15 | #include "base/strings/string_tokenizer.h" |
| 16 | #include "base/strings/string_util.h" |
| 17 | #include "base/strings/utf_string_conversions.h" |
| 18 | #include "build/build_config.h" |
| 19 |
| 20 | #if defined(OS_WIN) |
| 21 | #include <windows.h> |
| 22 | #include <shellapi.h> |
| 23 |
| 24 | #include "base/strings/string_util_win.h" |
| 25 | #endif |
| 26 |
| 27 | namespace base { |
| 28 |
| 29 | CommandLine* CommandLine::current_process_commandline_ = nullptr; |
| 30 |
| 31 | namespace { |
| 32 |
| 33 | constexpr CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--"); |
| 34 | constexpr CommandLine::CharType kSwitchValueSeparator[] = |
| 35 | FILE_PATH_LITERAL("="); |
| 36 |
| 37 | // Since we use a lazy match, make sure that longer versions (like "--") are |
| 38 | // listed before shorter versions (like "-") of similar prefixes. |
| 39 | #if defined(OS_WIN) |
| 40 | // By putting slash last, we can control whether it is treaded as a switch |
| 41 | // value by changing the value of switch_prefix_count to be one less than |
| 42 | // the array size. |
| 43 | constexpr CommandLine::StringPieceType kSwitchPrefixes[] = {L"--", L"-", L"/"}; |
| 44 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 45 | // Unixes don't use slash as a switch. |
| 46 | constexpr CommandLine::StringPieceType kSwitchPrefixes[] = {"--", "-"}; |
| 47 | #endif |
| 48 | size_t switch_prefix_count = base::size(kSwitchPrefixes); |
| 49 |
| 50 | size_t GetSwitchPrefixLength(CommandLine::StringPieceType string) { |
| 51 | for (size_t i = 0; i < switch_prefix_count; ++i) { |
| 52 | CommandLine::StringType prefix(kSwitchPrefixes[i]); |
| 53 | if (string.substr(0, prefix.length()) == prefix) |
| 54 | return prefix.length(); |
| 55 | } |
| 56 | return 0; |
| 57 | } |
| 58 |
| 59 | // Fills in |switch_string| and |switch_value| if |string| is a switch. |
| 60 | // This will preserve the input switch prefix in the output |switch_string|. |
| 61 | bool IsSwitch(const CommandLine::StringType& string, |
| 62 | CommandLine::StringType* switch_string, |
| 63 | CommandLine::StringType* switch_value) { |
| 64 | switch_string->clear(); |
| 65 | switch_value->clear(); |
| 66 | size_t prefix_length = GetSwitchPrefixLength(string); |
| 67 | if (prefix_length == 0 || prefix_length == string.length()) |
| 68 | return false; |
| 69 |
| 70 | const size_t equals_position = string.find(kSwitchValueSeparator); |
| 71 | *switch_string = string.substr(0, equals_position); |
| 72 | if (equals_position != CommandLine::StringType::npos) |
| 73 | *switch_value = string.substr(equals_position + 1); |
| 74 | return true; |
| 75 | } |
| 76 |
| 77 | // Returns true iff |string| represents a switch with key |
| 78 | // |switch_key_without_prefix|, regardless of value. |
| 79 | bool IsSwitchWithKey(CommandLine::StringPieceType string, |
| 80 | CommandLine::StringPieceType switch_key_without_prefix) { |
| 81 | size_t prefix_length = GetSwitchPrefixLength(string); |
| 82 | if (prefix_length == 0 || prefix_length == string.length()) |
| 83 | return false; |
| 84 |
| 85 | const size_t equals_position = string.find(kSwitchValueSeparator); |
| 86 | return string.substr(prefix_length, equals_position - prefix_length) == |
| 87 | switch_key_without_prefix; |
| 88 | } |
| 89 |
| 90 | // Append switches and arguments, keeping switches before arguments. |
| 91 | void AppendSwitchesAndArguments(CommandLine* command_line, |
| 92 | const CommandLine::StringVector& argv) { |
| 93 | bool parse_switches = true; |
| 94 | for (size_t i = 1; i < argv.size(); ++i) { |
| 95 | CommandLine::StringType arg = argv[i]; |
| 96 | #if defined(OS_WIN) |
| 97 | arg = CommandLine::StringType(TrimWhitespace(arg, TRIM_ALL)); |
| 98 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 99 | TrimWhitespaceASCII(arg, TRIM_ALL, &arg); |
| 100 | #endif |
| 101 |
| 102 | CommandLine::StringType switch_string; |
| 103 | CommandLine::StringType switch_value; |
| 104 | parse_switches &= (arg != kSwitchTerminator); |
| 105 | if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) { |
| 106 | #if defined(OS_WIN) |
| 107 | command_line->AppendSwitchNative(WideToUTF8(switch_string), switch_value); |
| 108 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 109 | command_line->AppendSwitchNative(switch_string, switch_value); |
| 110 | #else |
| 111 | #error Unsupported platform |
| 112 | #endif |
| 113 | } else { |
| 114 | command_line->AppendArgNative(arg); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 |
| 119 | #if defined(OS_WIN) |
| 120 | // Quote a string as necessary for CommandLineToArgvW compatiblity *on Windows*. |
| 121 | std::wstring QuoteForCommandLineToArgvW(const std::wstring& arg, |
| 122 | bool quote_placeholders) { |
| 123 | // We follow the quoting rules of CommandLineToArgvW. |
| 124 | // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx |
| 125 | std::wstring quotable_chars(L" \\\""); |
| 126 | // We may also be required to quote '%', which is commonly used in a command |
| 127 | // line as a placeholder. (It may be substituted for a string with spaces.) |
| 128 | if (quote_placeholders) |
| 129 | quotable_chars.push_back('%'); |
| 130 | if (arg.find_first_of(quotable_chars) == std::wstring::npos) { |
| 131 | // No quoting necessary. |
| 132 | return arg; |
| 133 | } |
| 134 |
| 135 | std::wstring out; |
| 136 | out.push_back('"'); |
| 137 | for (size_t i = 0; i < arg.size(); ++i) { |
| 138 | if (arg[i] == '\\') { |
| 139 | // Find the extent of this run of backslashes. |
| 140 | size_t start = i, end = start + 1; |
| 141 | for (; end < arg.size() && arg[end] == '\\'; ++end) {} |
| 142 | size_t backslash_count = end - start; |
| 143 |
| 144 | // Backslashes are escapes only if the run is followed by a double quote. |
| 145 | // Since we also will end the string with a double quote, we escape for |
| 146 | // either a double quote or the end of the string. |
| 147 | if (end == arg.size() || arg[end] == '"') { |
| 148 | // To quote, we need to output 2x as many backslashes. |
| 149 | backslash_count *= 2; |
| 150 | } |
| 151 | for (size_t j = 0; j < backslash_count; ++j) |
| 152 | out.push_back('\\'); |
| 153 |
| 154 | // Advance i to one before the end to balance i++ in loop. |
| 155 | i = end - 1; |
| 156 | } else if (arg[i] == '"') { |
| 157 | out.push_back('\\'); |
| 158 | out.push_back('"'); |
| 159 | } else { |
| 160 | out.push_back(arg[i]); |
| 161 | } |
| 162 | } |
| 163 | out.push_back('"'); |
| 164 |
| 165 | return out; |
| 166 | } |
| 167 | #endif |
| 168 |
| 169 | } // namespace |
| 170 |
| 171 | CommandLine::CommandLine(NoProgram no_program) |
| 172 | : argv_(1), |
| 173 | begin_args_(1) { |
| 174 | } |
| 175 |
| 176 | CommandLine::CommandLine(const FilePath& program) |
| 177 | : argv_(1), |
| 178 | begin_args_(1) { |
| 179 | SetProgram(program); |
| 180 | } |
| 181 |
| 182 | CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv) |
| 183 | : argv_(1), |
| 184 | begin_args_(1) { |
| 185 | InitFromArgv(argc, argv); |
| 186 | } |
| 187 |
| 188 | CommandLine::CommandLine(const StringVector& argv) |
| 189 | : argv_(1), |
| 190 | begin_args_(1) { |
| 191 | InitFromArgv(argv); |
| 192 | } |
| 193 |
| 194 | CommandLine::CommandLine(const CommandLine& other) = default; |
| 195 |
| 196 | CommandLine& CommandLine::operator=(const CommandLine& other) = default; |
| 197 |
| 198 | CommandLine::~CommandLine() = default; |
| 199 |
| 200 | #if defined(OS_WIN) |
| 201 | // static |
| 202 | void CommandLine::set_slash_is_not_a_switch() { |
| 203 | // The last switch prefix should be slash, so adjust the size to skip it. |
| 204 | static_assert(base::make_span(kSwitchPrefixes).back() == L"/", |
| 205 | "Error: Last switch prefix is not a slash."); |
| 206 | switch_prefix_count = base::size(kSwitchPrefixes) - 1; |
| 207 | } |
| 208 |
| 209 | // static |
| 210 | void CommandLine::InitUsingArgvForTesting(int argc, const char* const* argv) { |
| 211 | DCHECK(!current_process_commandline_); |
| 212 | current_process_commandline_ = new CommandLine(NO_PROGRAM); |
| 213 | // On Windows we need to convert the command line arguments to std::wstring. |
| 214 | CommandLine::StringVector argv_vector; |
| 215 | for (int i = 0; i < argc; ++i) |
| 216 | argv_vector.push_back(UTF8ToWide(argv[i])); |
| 217 | current_process_commandline_->InitFromArgv(argv_vector); |
| 218 | } |
| 219 | #endif |
| 220 |
| 221 | // static |
| 222 | bool CommandLine::Init(int argc, const char* const* argv) { |
| 223 | if (current_process_commandline_) { |
| 224 | // If this is intentional, Reset() must be called first. If we are using |
| 225 | // the shared build mode, we have to share a single object across multiple |
| 226 | // shared libraries. |
| 227 | return false; |
| 228 | } |
| 229 |
| 230 | current_process_commandline_ = new CommandLine(NO_PROGRAM); |
| 231 | #if defined(OS_WIN) |
| 232 | current_process_commandline_->ParseFromString(::GetCommandLineW()); |
| 233 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 234 | current_process_commandline_->InitFromArgv(argc, argv); |
| 235 | #else |
| 236 | #error Unsupported platform |
| 237 | #endif |
| 238 |
| 239 | return true; |
| 240 | } |
| 241 |
| 242 | // static |
| 243 | void CommandLine::Reset() { |
| 244 | DCHECK(current_process_commandline_); |
| 245 | delete current_process_commandline_; |
| 246 | current_process_commandline_ = nullptr; |
| 247 | } |
| 248 |
| 249 | // static |
| 250 | CommandLine* CommandLine::ForCurrentProcess() { |
| 251 | DCHECK(current_process_commandline_); |
| 252 | return current_process_commandline_; |
| 253 | } |
| 254 |
| 255 | // static |
| 256 | bool CommandLine::InitializedForCurrentProcess() { |
| 257 | return !!current_process_commandline_; |
| 258 | } |
| 259 |
| 260 | #if defined(OS_WIN) |
| 261 | // static |
| 262 | CommandLine CommandLine::FromString(StringPieceType command_line) { |
| 263 | CommandLine cmd(NO_PROGRAM); |
| 264 | cmd.ParseFromString(command_line); |
| 265 | return cmd; |
| 266 | } |
| 267 | #endif |
| 268 |
| 269 | void CommandLine::InitFromArgv(int argc, |
| 270 | const CommandLine::CharType* const* argv) { |
| 271 | StringVector new_argv; |
| 272 | for (int i = 0; i < argc; ++i) |
| 273 | new_argv.push_back(argv[i]); |
| 274 | InitFromArgv(new_argv); |
| 275 | } |
| 276 |
| 277 | void CommandLine::InitFromArgv(const StringVector& argv) { |
| 278 | argv_ = StringVector(1); |
| 279 | switches_.clear(); |
| 280 | begin_args_ = 1; |
| 281 | SetProgram(argv.empty() ? FilePath() : FilePath(argv[0])); |
| 282 | AppendSwitchesAndArguments(this, argv); |
| 283 | } |
| 284 |
| 285 | FilePath CommandLine::GetProgram() const { |
| 286 | return FilePath(argv_[0]); |
| 287 | } |
| 288 |
| 289 | void CommandLine::SetProgram(const FilePath& program) { |
| 290 | #if defined(OS_WIN) |
| 291 | argv_[0] = StringType(TrimWhitespace(program.value(), TRIM_ALL)); |
| 292 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 293 | TrimWhitespaceASCII(program.value(), TRIM_ALL, &argv_[0]); |
| 294 | #else |
| 295 | #error Unsupported platform |
| 296 | #endif |
| 297 | } |
| 298 |
| 299 | bool CommandLine::HasSwitch(const StringPiece& switch_string) const { |
| 300 | DCHECK_EQ(ToLowerASCII(switch_string), switch_string); |
| 301 | return Contains(switches_, switch_string); |
| 302 | } |
| 303 |
| 304 | bool CommandLine::HasSwitch(const char switch_constant[]) const { |
| 305 | return HasSwitch(StringPiece(switch_constant)); |
| 306 | } |
| 307 |
| 308 | std::string CommandLine::GetSwitchValueASCII( |
| 309 | const StringPiece& switch_string) const { |
| 310 | StringType value = GetSwitchValueNative(switch_string); |
| 311 | #if defined(OS_WIN) |
| 312 | if (!IsStringASCII(base::AsStringPiece16(value))) { |
| 313 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 314 | if (!IsStringASCII(value)) { |
| 315 | #endif |
| 316 | DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII."; |
| 317 | return std::string(); |
| 318 | } |
| 319 | #if defined(OS_WIN) |
| 320 | return WideToUTF8(value); |
| 321 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 322 | return value; |
| 323 | #endif |
| 324 | } |
| 325 |
| 326 | FilePath CommandLine::GetSwitchValuePath( |
| 327 | const StringPiece& switch_string) const { |
| 328 | return FilePath(GetSwitchValueNative(switch_string)); |
| 329 | } |
| 330 |
| 331 | CommandLine::StringType CommandLine::GetSwitchValueNative( |
| 332 | const StringPiece& switch_string) const { |
| 333 | DCHECK_EQ(ToLowerASCII(switch_string), switch_string); |
| 334 | auto result = switches_.find(switch_string); |
| 335 | return result == switches_.end() ? StringType() : result->second; |
| 336 | } |
| 337 |
| 338 | void CommandLine::AppendSwitch(const std::string& switch_string) { |
| 339 | AppendSwitchNative(switch_string, StringType()); |
| 340 | } |
| 341 |
| 342 | void CommandLine::AppendSwitchPath(const std::string& switch_string, |
| 343 | const FilePath& path) { |
| 344 | AppendSwitchNative(switch_string, path.value()); |
| 345 | } |
| 346 |
| 347 | void CommandLine::AppendSwitchNative(const std::string& switch_string, |
| 348 | const CommandLine::StringType& value) { |
| 349 | #if defined(OS_WIN) |
| 350 | const std::string switch_key = ToLowerASCII(switch_string); |
| 351 | StringType combined_switch_string(UTF8ToWide(switch_key)); |
| 352 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 353 | const std::string& switch_key = switch_string; |
| 354 | StringType combined_switch_string(switch_key); |
| 355 | #endif |
| 356 | size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); |
| 357 | auto insertion = |
| 358 | switches_.insert(make_pair(switch_key.substr(prefix_length), value)); |
| 359 | if (!insertion.second) |
| 360 | insertion.first->second = value; |
| 361 | // Preserve existing switch prefixes in |argv_|; only append one if necessary. |
| 362 | if (prefix_length == 0) { |
| 363 | combined_switch_string.insert(0, kSwitchPrefixes[0].data(), |
| 364 | kSwitchPrefixes[0].size()); |
| 365 | } |
| 366 | if (!value.empty()) |
| 367 | combined_switch_string += kSwitchValueSeparator + value; |
| 368 | // Append the switch and update the switches/arguments divider |begin_args_|. |
| 369 | argv_.insert(argv_.begin() + begin_args_++, combined_switch_string); |
| 370 | } |
| 371 |
| 372 | void CommandLine::AppendSwitchASCII(const std::string& switch_string, |
| 373 | const std::string& value_string) { |
| 374 | #if defined(OS_WIN) |
| 375 | AppendSwitchNative(switch_string, UTF8ToWide(value_string)); |
| 376 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 377 | AppendSwitchNative(switch_string, value_string); |
| 378 | #else |
| 379 | #error Unsupported platform |
| 380 | #endif |
| 381 | } |
| 382 |
| 383 | void CommandLine::RemoveSwitch(base::StringPiece switch_key_without_prefix) { |
| 384 | #if defined(OS_WIN) |
| 385 | StringType switch_key_native = UTF8ToWide(switch_key_without_prefix); |
| 386 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 387 | StringType switch_key_native = switch_key_without_prefix.as_string(); |
| 388 | #endif |
| 389 |
| 390 | DCHECK_EQ(ToLowerASCII(switch_key_without_prefix), switch_key_without_prefix); |
| 391 | DCHECK_EQ(0u, GetSwitchPrefixLength(switch_key_native)); |
| 392 | size_t erased_from_switches = |
| 393 | switches_.erase(switch_key_without_prefix.as_string()); |
| 394 | DCHECK(erased_from_switches <= 1); |
| 395 | if (!erased_from_switches) |
| 396 | return; |
| 397 |
| 398 | // Also erase from the switches section of |argv_| and update |begin_args_| |
| 399 | // accordingly. |
| 400 | // Switches in |argv_| have indices [1, begin_args_). |
| 401 | auto argv_switches_begin = argv_.begin() + 1; |
| 402 | auto argv_switches_end = argv_.begin() + begin_args_; |
| 403 | DCHECK(argv_switches_begin <= argv_switches_end); |
| 404 | DCHECK(argv_switches_end <= argv_.end()); |
| 405 | auto expell = std::remove_if(argv_switches_begin, argv_switches_end, |
| 406 | [&switch_key_native](const StringType& arg) { |
| 407 | return IsSwitchWithKey(arg, switch_key_native); |
| 408 | }); |
| 409 | if (expell == argv_switches_end) { |
| 410 | NOTREACHED(); |
| 411 | return; |
| 412 | } |
| 413 | begin_args_ -= argv_switches_end - expell; |
| 414 | argv_.erase(expell, argv_switches_end); |
| 415 | } |
| 416 |
| 417 | void CommandLine::CopySwitchesFrom(const CommandLine& source, |
| 418 | const char* const switches[], |
| 419 | size_t count) { |
| 420 | for (size_t i = 0; i < count; ++i) { |
| 421 | if (source.HasSwitch(switches[i])) |
| 422 | AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i])); |
| 423 | } |
| 424 | } |
| 425 |
| 426 | CommandLine::StringVector CommandLine::GetArgs() const { |
| 427 | // Gather all arguments after the last switch (may include kSwitchTerminator). |
| 428 | StringVector args(argv_.begin() + begin_args_, argv_.end()); |
| 429 | // Erase only the first kSwitchTerminator (maybe "--" is a legitimate page?) |
| 430 | auto switch_terminator = |
| 431 | std::find(args.begin(), args.end(), kSwitchTerminator); |
| 432 | if (switch_terminator != args.end()) |
| 433 | args.erase(switch_terminator); |
| 434 | return args; |
| 435 | } |
| 436 |
| 437 | void CommandLine::AppendArg(const std::string& value) { |
| 438 | #if defined(OS_WIN) |
| 439 | DCHECK(IsStringUTF8(value)); |
| 440 | AppendArgNative(UTF8ToWide(value)); |
| 441 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 442 | AppendArgNative(value); |
| 443 | #else |
| 444 | #error Unsupported platform |
| 445 | #endif |
| 446 | } |
| 447 |
| 448 | void CommandLine::AppendArgPath(const FilePath& path) { |
| 449 | AppendArgNative(path.value()); |
| 450 | } |
| 451 |
| 452 | void CommandLine::AppendArgNative(const CommandLine::StringType& value) { |
| 453 | argv_.push_back(value); |
| 454 | } |
| 455 |
| 456 | void CommandLine::AppendArguments(const CommandLine& other, |
| 457 | bool include_program) { |
| 458 | if (include_program) |
| 459 | SetProgram(other.GetProgram()); |
| 460 | AppendSwitchesAndArguments(this, other.argv()); |
| 461 | } |
| 462 |
| 463 | void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) { |
| 464 | if (wrapper.empty()) |
| 465 | return; |
| 466 | // Split the wrapper command based on whitespace (with quoting). |
| 467 | using CommandLineTokenizer = |
| 468 | StringTokenizerT<StringType, StringType::const_iterator>; |
| 469 | CommandLineTokenizer tokenizer(wrapper, FILE_PATH_LITERAL(" ")); |
| 470 | tokenizer.set_quote_chars(FILE_PATH_LITERAL("'\"")); |
| 471 | std::vector<StringType> wrapper_argv; |
| 472 | while (tokenizer.GetNext()) |
| 473 | wrapper_argv.emplace_back(tokenizer.token()); |
| 474 |
| 475 | // Prepend the wrapper and update the switches/arguments |begin_args_|. |
| 476 | argv_.insert(argv_.begin(), wrapper_argv.begin(), wrapper_argv.end()); |
| 477 | begin_args_ += wrapper_argv.size(); |
| 478 | } |
| 479 |
| 480 | #if defined(OS_WIN) |
| 481 | void CommandLine::ParseFromString(StringPieceType command_line) { |
| 482 | command_line = TrimWhitespace(command_line, TRIM_ALL); |
| 483 | if (command_line.empty()) |
| 484 | return; |
| 485 |
| 486 | int num_args = 0; |
| 487 | wchar_t** args = NULL; |
| 488 | // When calling CommandLineToArgvW, use the apiset if available. |
| 489 | // Doing so will bypass loading shell32.dll on Win8+. |
| 490 | HMODULE downlevel_shell32_dll = |
| 491 | ::LoadLibraryEx(L"api-ms-win-downlevel-shell32-l1-1-0.dll", nullptr, |
| 492 | LOAD_LIBRARY_SEARCH_SYSTEM32); |
| 493 | if (downlevel_shell32_dll) { |
| 494 | auto command_line_to_argv_w_proc = |
| 495 | reinterpret_cast<decltype(::CommandLineToArgvW)*>( |
| 496 | ::GetProcAddress(downlevel_shell32_dll, "CommandLineToArgvW")); |
| 497 | if (command_line_to_argv_w_proc) |
| 498 | args = command_line_to_argv_w_proc(command_line.data(), &num_args); |
| 499 | } else { |
| 500 | // Since the apiset is not available, allow the delayload of shell32.dll |
| 501 | // to take place. |
| 502 | args = ::CommandLineToArgvW(command_line.data(), &num_args); |
| 503 | } |
| 504 |
| 505 | DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: " |
| 506 | << command_line; |
| 507 | StringVector argv(args, args + num_args); |
| 508 | InitFromArgv(argv); |
| 509 | LocalFree(args); |
| 510 |
| 511 | if (downlevel_shell32_dll) |
| 512 | ::FreeLibrary(downlevel_shell32_dll); |
| 513 | } |
| 514 | #endif |
| 515 |
| 516 | CommandLine::StringType CommandLine::GetCommandLineStringInternal( |
| 517 | bool quote_placeholders) const { |
| 518 | StringType string(argv_[0]); |
| 519 | #if defined(OS_WIN) |
| 520 | string = QuoteForCommandLineToArgvW(string, quote_placeholders); |
| 521 | #endif |
| 522 | StringType params(GetArgumentsStringInternal(quote_placeholders)); |
| 523 | if (!params.empty()) { |
| 524 | string.append(FILE_PATH_LITERAL(" ")); |
| 525 | string.append(params); |
| 526 | } |
| 527 | return string; |
| 528 | } |
| 529 |
| 530 | CommandLine::StringType CommandLine::GetArgumentsStringInternal( |
| 531 | bool quote_placeholders) const { |
| 532 | StringType params; |
| 533 | // Append switches and arguments. |
| 534 | bool parse_switches = true; |
| 535 | for (size_t i = 1; i < argv_.size(); ++i) { |
| 536 | StringType arg = argv_[i]; |
| 537 | StringType switch_string; |
| 538 | StringType switch_value; |
| 539 | parse_switches &= arg != kSwitchTerminator; |
| 540 | if (i > 1) |
| 541 | params.append(FILE_PATH_LITERAL(" ")); |
| 542 | if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) { |
| 543 | params.append(switch_string); |
| 544 | if (!switch_value.empty()) { |
| 545 | #if defined(OS_WIN) |
| 546 | switch_value = |
| 547 | QuoteForCommandLineToArgvW(switch_value, quote_placeholders); |
| 548 | #endif |
| 549 | params.append(kSwitchValueSeparator + switch_value); |
| 550 | } |
| 551 | } else { |
| 552 | #if defined(OS_WIN) |
| 553 | arg = QuoteForCommandLineToArgvW(arg, quote_placeholders); |
| 554 | #endif |
| 555 | params.append(arg); |
| 556 | } |
| 557 | } |
| 558 | return params; |
| 559 | } |
| 560 |
| 561 | } // namespace base |
| 562 |