| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <ostream> | 8 #include <ostream> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 | 17 |
| 18 #if defined(OS_WIN) | |
| 19 #include <windows.h> | |
| 20 #include <shellapi.h> | |
| 21 #endif | |
| 22 | |
| 23 namespace base { | 18 namespace base { |
| 24 | 19 |
| 25 CommandLine* CommandLine::current_process_commandline_ = NULL; | 20 CommandLine* CommandLine::current_process_commandline_ = NULL; |
| 26 | 21 |
| 27 namespace { | 22 namespace { |
| 28 | 23 |
| 29 const CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--"); | 24 const CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--"); |
| 30 const CommandLine::CharType kSwitchValueSeparator[] = FILE_PATH_LITERAL("="); | 25 const CommandLine::CharType kSwitchValueSeparator[] = FILE_PATH_LITERAL("="); |
| 31 | 26 |
| 32 // Since we use a lazy match, make sure that longer versions (like "--") are | 27 // Since we use a lazy match, make sure that longer versions (like "--") are |
| 33 // listed before shorter versions (like "-") of similar prefixes. | 28 // listed before shorter versions (like "-") of similar prefixes. |
| 34 #if defined(OS_WIN) | |
| 35 // By putting slash last, we can control whether it is treaded as a switch | |
| 36 // value by changing the value of switch_prefix_count to be one less than | |
| 37 // the array size. | |
| 38 const CommandLine::CharType* const kSwitchPrefixes[] = {L"--", L"-", L"/"}; | |
| 39 #elif defined(OS_POSIX) | |
| 40 // Unixes don't use slash as a switch. | 29 // Unixes don't use slash as a switch. |
| 41 const CommandLine::CharType* const kSwitchPrefixes[] = {"--", "-"}; | 30 const CommandLine::CharType* const kSwitchPrefixes[] = {"--", "-"}; |
| 42 #endif | |
| 43 size_t switch_prefix_count = arraysize(kSwitchPrefixes); | 31 size_t switch_prefix_count = arraysize(kSwitchPrefixes); |
| 44 | 32 |
| 45 size_t GetSwitchPrefixLength(const CommandLine::StringType& string) { | 33 size_t GetSwitchPrefixLength(const CommandLine::StringType& string) { |
| 46 for (size_t i = 0; i < switch_prefix_count; ++i) { | 34 for (size_t i = 0; i < switch_prefix_count; ++i) { |
| 47 CommandLine::StringType prefix(kSwitchPrefixes[i]); | 35 CommandLine::StringType prefix(kSwitchPrefixes[i]); |
| 48 if (string.compare(0, prefix.length(), prefix) == 0) | 36 if (string.compare(0, prefix.length(), prefix) == 0) |
| 49 return prefix.length(); | 37 return prefix.length(); |
| 50 } | 38 } |
| 51 return 0; | 39 return 0; |
| 52 } | 40 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 74 const CommandLine::StringVector& argv) { | 62 const CommandLine::StringVector& argv) { |
| 75 bool parse_switches = true; | 63 bool parse_switches = true; |
| 76 for (size_t i = 1; i < argv.size(); ++i) { | 64 for (size_t i = 1; i < argv.size(); ++i) { |
| 77 CommandLine::StringType arg = argv[i]; | 65 CommandLine::StringType arg = argv[i]; |
| 78 TrimWhitespace(arg, TRIM_ALL, &arg); | 66 TrimWhitespace(arg, TRIM_ALL, &arg); |
| 79 | 67 |
| 80 CommandLine::StringType switch_string; | 68 CommandLine::StringType switch_string; |
| 81 CommandLine::StringType switch_value; | 69 CommandLine::StringType switch_value; |
| 82 parse_switches &= (arg != kSwitchTerminator); | 70 parse_switches &= (arg != kSwitchTerminator); |
| 83 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) { | 71 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) { |
| 84 #if defined(OS_WIN) | |
| 85 command_line->AppendSwitchNative(UTF16ToASCII(switch_string), | |
| 86 switch_value); | |
| 87 #elif defined(OS_POSIX) | |
| 88 command_line->AppendSwitchNative(switch_string, switch_value); | 72 command_line->AppendSwitchNative(switch_string, switch_value); |
| 89 #endif | |
| 90 } else { | 73 } else { |
| 91 command_line->AppendArgNative(arg); | 74 command_line->AppendArgNative(arg); |
| 92 } | 75 } |
| 93 } | 76 } |
| 94 } | 77 } |
| 95 | 78 |
| 96 #if defined(OS_WIN) | |
| 97 // Quote a string as necessary for CommandLineToArgvW compatiblity *on Windows*. | |
| 98 string16 QuoteForCommandLineToArgvW(const string16& arg, | |
| 99 bool quote_placeholders) { | |
| 100 // We follow the quoting rules of CommandLineToArgvW. | |
| 101 // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx | |
| 102 string16 quotable_chars(L" \\\""); | |
| 103 // We may also be required to quote '%', which is commonly used in a command | |
| 104 // line as a placeholder. (It may be substituted for a string with spaces.) | |
| 105 if (quote_placeholders) | |
| 106 quotable_chars.push_back(L'%'); | |
| 107 if (arg.find_first_of(quotable_chars) == string16::npos) { | |
| 108 // No quoting necessary. | |
| 109 return arg; | |
| 110 } | |
| 111 | |
| 112 string16 out; | |
| 113 out.push_back(L'"'); | |
| 114 for (size_t i = 0; i < arg.size(); ++i) { | |
| 115 if (arg[i] == '\\') { | |
| 116 // Find the extent of this run of backslashes. | |
| 117 size_t start = i, end = start + 1; | |
| 118 for (; end < arg.size() && arg[end] == '\\'; ++end) {} | |
| 119 size_t backslash_count = end - start; | |
| 120 | |
| 121 // Backslashes are escapes only if the run is followed by a double quote. | |
| 122 // Since we also will end the string with a double quote, we escape for | |
| 123 // either a double quote or the end of the string. | |
| 124 if (end == arg.size() || arg[end] == '"') { | |
| 125 // To quote, we need to output 2x as many backslashes. | |
| 126 backslash_count *= 2; | |
| 127 } | |
| 128 for (size_t j = 0; j < backslash_count; ++j) | |
| 129 out.push_back('\\'); | |
| 130 | |
| 131 // Advance i to one before the end to balance i++ in loop. | |
| 132 i = end - 1; | |
| 133 } else if (arg[i] == '"') { | |
| 134 out.push_back('\\'); | |
| 135 out.push_back('"'); | |
| 136 } else { | |
| 137 out.push_back(arg[i]); | |
| 138 } | |
| 139 } | |
| 140 out.push_back('"'); | |
| 141 | |
| 142 return out; | |
| 143 } | |
| 144 #endif | |
| 145 | |
| 146 } // namespace | 79 } // namespace |
| 147 | 80 |
| 148 CommandLine::CommandLine(NoProgram no_program) | 81 CommandLine::CommandLine(NoProgram no_program) |
| 149 : argv_(1), | 82 : argv_(1), |
| 150 begin_args_(1) { | 83 begin_args_(1) { |
| 151 } | 84 } |
| 152 | 85 |
| 153 CommandLine::CommandLine(const FilePath& program) | 86 CommandLine::CommandLine(const FilePath& program) |
| 154 : argv_(1), | 87 : argv_(1), |
| 155 begin_args_(1) { | 88 begin_args_(1) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 179 argv_ = other.argv_; | 112 argv_ = other.argv_; |
| 180 switches_ = other.switches_; | 113 switches_ = other.switches_; |
| 181 begin_args_ = other.begin_args_; | 114 begin_args_ = other.begin_args_; |
| 182 ResetStringPieces(); | 115 ResetStringPieces(); |
| 183 return *this; | 116 return *this; |
| 184 } | 117 } |
| 185 | 118 |
| 186 CommandLine::~CommandLine() { | 119 CommandLine::~CommandLine() { |
| 187 } | 120 } |
| 188 | 121 |
| 189 #if defined(OS_WIN) | |
| 190 // static | |
| 191 void CommandLine::set_slash_is_not_a_switch() { | |
| 192 // The last switch prefix should be slash, so adjust the size to skip it. | |
| 193 DCHECK_EQ(wcscmp(kSwitchPrefixes[arraysize(kSwitchPrefixes) - 1], L"/"), 0); | |
| 194 switch_prefix_count = arraysize(kSwitchPrefixes) - 1; | |
| 195 } | |
| 196 #endif | |
| 197 | |
| 198 // static | 122 // static |
| 199 bool CommandLine::Init(int argc, const char* const* argv) { | 123 bool CommandLine::Init(int argc, const char* const* argv) { |
| 200 if (current_process_commandline_) { | 124 if (current_process_commandline_) { |
| 201 // If this is intentional, Reset() must be called first. If we are using | 125 // If this is intentional, Reset() must be called first. If we are using |
| 202 // the shared build mode, we have to share a single object across multiple | 126 // the shared build mode, we have to share a single object across multiple |
| 203 // shared libraries. | 127 // shared libraries. |
| 204 return false; | 128 return false; |
| 205 } | 129 } |
| 206 | 130 |
| 207 current_process_commandline_ = new CommandLine(NO_PROGRAM); | 131 current_process_commandline_ = new CommandLine(NO_PROGRAM); |
| 208 #if defined(OS_WIN) | |
| 209 current_process_commandline_->ParseFromString(::GetCommandLineW()); | |
| 210 #elif defined(OS_POSIX) | |
| 211 current_process_commandline_->InitFromArgv(argc, argv); | 132 current_process_commandline_->InitFromArgv(argc, argv); |
| 212 #endif | |
| 213 | 133 |
| 214 return true; | 134 return true; |
| 215 } | 135 } |
| 216 | 136 |
| 217 // static | 137 // static |
| 218 void CommandLine::Reset() { | 138 void CommandLine::Reset() { |
| 219 DCHECK(current_process_commandline_); | 139 DCHECK(current_process_commandline_); |
| 220 delete current_process_commandline_; | 140 delete current_process_commandline_; |
| 221 current_process_commandline_ = NULL; | 141 current_process_commandline_ = NULL; |
| 222 } | 142 } |
| 223 | 143 |
| 224 // static | 144 // static |
| 225 CommandLine* CommandLine::ForCurrentProcess() { | 145 CommandLine* CommandLine::ForCurrentProcess() { |
| 226 DCHECK(current_process_commandline_); | 146 DCHECK(current_process_commandline_); |
| 227 return current_process_commandline_; | 147 return current_process_commandline_; |
| 228 } | 148 } |
| 229 | 149 |
| 230 // static | 150 // static |
| 231 bool CommandLine::InitializedForCurrentProcess() { | 151 bool CommandLine::InitializedForCurrentProcess() { |
| 232 return !!current_process_commandline_; | 152 return !!current_process_commandline_; |
| 233 } | 153 } |
| 234 | 154 |
| 235 #if defined(OS_WIN) | |
| 236 // static | |
| 237 CommandLine CommandLine::FromString(const string16& command_line) { | |
| 238 CommandLine cmd(NO_PROGRAM); | |
| 239 cmd.ParseFromString(command_line); | |
| 240 return cmd; | |
| 241 } | |
| 242 #endif | |
| 243 | |
| 244 void CommandLine::InitFromArgv(int argc, | 155 void CommandLine::InitFromArgv(int argc, |
| 245 const CommandLine::CharType* const* argv) { | 156 const CommandLine::CharType* const* argv) { |
| 246 StringVector new_argv; | 157 StringVector new_argv; |
| 247 for (int i = 0; i < argc; ++i) | 158 for (int i = 0; i < argc; ++i) |
| 248 new_argv.push_back(argv[i]); | 159 new_argv.push_back(argv[i]); |
| 249 InitFromArgv(new_argv); | 160 InitFromArgv(new_argv); |
| 250 } | 161 } |
| 251 | 162 |
| 252 void CommandLine::InitFromArgv(const StringVector& argv) { | 163 void CommandLine::InitFromArgv(const StringVector& argv) { |
| 253 argv_ = StringVector(1); | 164 argv_ = StringVector(1); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 276 return HasSwitch(base::StringPiece(switch_constant)); | 187 return HasSwitch(base::StringPiece(switch_constant)); |
| 277 } | 188 } |
| 278 | 189 |
| 279 std::string CommandLine::GetSwitchValueASCII( | 190 std::string CommandLine::GetSwitchValueASCII( |
| 280 const base::StringPiece& switch_string) const { | 191 const base::StringPiece& switch_string) const { |
| 281 StringType value = GetSwitchValueNative(switch_string); | 192 StringType value = GetSwitchValueNative(switch_string); |
| 282 if (!IsStringASCII(value)) { | 193 if (!IsStringASCII(value)) { |
| 283 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII."; | 194 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII."; |
| 284 return std::string(); | 195 return std::string(); |
| 285 } | 196 } |
| 286 #if defined(OS_WIN) | |
| 287 return UTF16ToASCII(value); | |
| 288 #else | |
| 289 return value; | 197 return value; |
| 290 #endif | |
| 291 } | 198 } |
| 292 | 199 |
| 293 FilePath CommandLine::GetSwitchValuePath( | 200 FilePath CommandLine::GetSwitchValuePath( |
| 294 const base::StringPiece& switch_string) const { | 201 const base::StringPiece& switch_string) const { |
| 295 return FilePath(GetSwitchValueNative(switch_string)); | 202 return FilePath(GetSwitchValueNative(switch_string)); |
| 296 } | 203 } |
| 297 | 204 |
| 298 CommandLine::StringType CommandLine::GetSwitchValueNative( | 205 CommandLine::StringType CommandLine::GetSwitchValueNative( |
| 299 const base::StringPiece& switch_string) const { | 206 const base::StringPiece& switch_string) const { |
| 300 DCHECK_EQ(StringToLowerASCII(switch_string.as_string()), switch_string); | 207 DCHECK_EQ(StringToLowerASCII(switch_string.as_string()), switch_string); |
| 301 auto result = switches_by_stringpiece_.find(switch_string); | 208 auto result = switches_by_stringpiece_.find(switch_string); |
| 302 return result == switches_by_stringpiece_.end() ? StringType() | 209 return result == switches_by_stringpiece_.end() ? StringType() |
| 303 : *(result->second); | 210 : *(result->second); |
| 304 } | 211 } |
| 305 | 212 |
| 306 void CommandLine::AppendSwitch(const std::string& switch_string) { | 213 void CommandLine::AppendSwitch(const std::string& switch_string) { |
| 307 AppendSwitchNative(switch_string, StringType()); | 214 AppendSwitchNative(switch_string, StringType()); |
| 308 } | 215 } |
| 309 | 216 |
| 310 void CommandLine::AppendSwitchPath(const std::string& switch_string, | 217 void CommandLine::AppendSwitchPath(const std::string& switch_string, |
| 311 const FilePath& path) { | 218 const FilePath& path) { |
| 312 AppendSwitchNative(switch_string, path.value()); | 219 AppendSwitchNative(switch_string, path.value()); |
| 313 } | 220 } |
| 314 | 221 |
| 315 void CommandLine::AppendSwitchNative(const std::string& switch_string, | 222 void CommandLine::AppendSwitchNative(const std::string& switch_string, |
| 316 const CommandLine::StringType& value) { | 223 const CommandLine::StringType& value) { |
| 317 #if defined(OS_WIN) | |
| 318 const std::string switch_key = StringToLowerASCII(switch_string); | |
| 319 StringType combined_switch_string(ASCIIToUTF16(switch_key)); | |
| 320 #elif defined(OS_POSIX) | |
| 321 const std::string& switch_key = switch_string; | 224 const std::string& switch_key = switch_string; |
| 322 StringType combined_switch_string(switch_key); | 225 StringType combined_switch_string(switch_key); |
| 323 #endif | |
| 324 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); | 226 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); |
| 325 auto insertion = | 227 auto insertion = |
| 326 switches_.insert(make_pair(switch_key.substr(prefix_length), value)); | 228 switches_.insert(make_pair(switch_key.substr(prefix_length), value)); |
| 327 if (!insertion.second) | 229 if (!insertion.second) |
| 328 insertion.first->second = value; | 230 insertion.first->second = value; |
| 329 switches_by_stringpiece_[insertion.first->first] = &(insertion.first->second); | 231 switches_by_stringpiece_[insertion.first->first] = &(insertion.first->second); |
| 330 // Preserve existing switch prefixes in |argv_|; only append one if necessary. | 232 // Preserve existing switch prefixes in |argv_|; only append one if necessary. |
| 331 if (prefix_length == 0) | 233 if (prefix_length == 0) |
| 332 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string; | 234 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string; |
| 333 if (!value.empty()) | 235 if (!value.empty()) |
| 334 combined_switch_string += kSwitchValueSeparator + value; | 236 combined_switch_string += kSwitchValueSeparator + value; |
| 335 // Append the switch and update the switches/arguments divider |begin_args_|. | 237 // Append the switch and update the switches/arguments divider |begin_args_|. |
| 336 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string); | 238 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string); |
| 337 } | 239 } |
| 338 | 240 |
| 339 void CommandLine::AppendSwitchASCII(const std::string& switch_string, | 241 void CommandLine::AppendSwitchASCII(const std::string& switch_string, |
| 340 const std::string& value_string) { | 242 const std::string& value_string) { |
| 341 #if defined(OS_WIN) | |
| 342 AppendSwitchNative(switch_string, ASCIIToUTF16(value_string)); | |
| 343 #elif defined(OS_POSIX) | |
| 344 AppendSwitchNative(switch_string, value_string); | 243 AppendSwitchNative(switch_string, value_string); |
| 345 #endif | |
| 346 } | 244 } |
| 347 | 245 |
| 348 void CommandLine::CopySwitchesFrom(const CommandLine& source, | 246 void CommandLine::CopySwitchesFrom(const CommandLine& source, |
| 349 const char* const switches[], | 247 const char* const switches[], |
| 350 size_t count) { | 248 size_t count) { |
| 351 for (size_t i = 0; i < count; ++i) { | 249 for (size_t i = 0; i < count; ++i) { |
| 352 if (source.HasSwitch(switches[i])) | 250 if (source.HasSwitch(switches[i])) |
| 353 AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i])); | 251 AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i])); |
| 354 } | 252 } |
| 355 } | 253 } |
| 356 | 254 |
| 357 CommandLine::StringVector CommandLine::GetArgs() const { | 255 CommandLine::StringVector CommandLine::GetArgs() const { |
| 358 // Gather all arguments after the last switch (may include kSwitchTerminator). | 256 // Gather all arguments after the last switch (may include kSwitchTerminator). |
| 359 StringVector args(argv_.begin() + begin_args_, argv_.end()); | 257 StringVector args(argv_.begin() + begin_args_, argv_.end()); |
| 360 // Erase only the first kSwitchTerminator (maybe "--" is a legitimate page?) | 258 // Erase only the first kSwitchTerminator (maybe "--" is a legitimate page?) |
| 361 StringVector::iterator switch_terminator = | 259 StringVector::iterator switch_terminator = |
| 362 std::find(args.begin(), args.end(), kSwitchTerminator); | 260 std::find(args.begin(), args.end(), kSwitchTerminator); |
| 363 if (switch_terminator != args.end()) | 261 if (switch_terminator != args.end()) |
| 364 args.erase(switch_terminator); | 262 args.erase(switch_terminator); |
| 365 return args; | 263 return args; |
| 366 } | 264 } |
| 367 | 265 |
| 368 void CommandLine::AppendArg(const std::string& value) { | 266 void CommandLine::AppendArg(const std::string& value) { |
| 369 #if defined(OS_WIN) | |
| 370 DCHECK(IsStringUTF8(value)); | |
| 371 AppendArgNative(UTF8ToWide(value)); | |
| 372 #elif defined(OS_POSIX) | |
| 373 AppendArgNative(value); | 267 AppendArgNative(value); |
| 374 #endif | |
| 375 } | 268 } |
| 376 | 269 |
| 377 void CommandLine::AppendArgPath(const FilePath& path) { | 270 void CommandLine::AppendArgPath(const FilePath& path) { |
| 378 AppendArgNative(path.value()); | 271 AppendArgNative(path.value()); |
| 379 } | 272 } |
| 380 | 273 |
| 381 void CommandLine::AppendArgNative(const CommandLine::StringType& value) { | 274 void CommandLine::AppendArgNative(const CommandLine::StringType& value) { |
| 382 argv_.push_back(value); | 275 argv_.push_back(value); |
| 383 } | 276 } |
| 384 | 277 |
| 385 void CommandLine::AppendArguments(const CommandLine& other, | 278 void CommandLine::AppendArguments(const CommandLine& other, |
| 386 bool include_program) { | 279 bool include_program) { |
| 387 if (include_program) | 280 if (include_program) |
| 388 SetProgram(other.GetProgram()); | 281 SetProgram(other.GetProgram()); |
| 389 AppendSwitchesAndArguments(this, other.argv()); | 282 AppendSwitchesAndArguments(this, other.argv()); |
| 390 } | 283 } |
| 391 | 284 |
| 392 void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) { | 285 void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) { |
| 393 if (wrapper.empty()) | 286 if (wrapper.empty()) |
| 394 return; | 287 return; |
| 395 // The wrapper may have embedded arguments (like "gdb --args"). In this case, | 288 // The wrapper may have embedded arguments (like "gdb --args"). In this case, |
| 396 // we don't pretend to do anything fancy, we just split on spaces. | 289 // we don't pretend to do anything fancy, we just split on spaces. |
| 397 StringVector wrapper_argv; | 290 StringVector wrapper_argv; |
| 398 SplitString(wrapper, FILE_PATH_LITERAL(' '), &wrapper_argv); | 291 SplitString(wrapper, FILE_PATH_LITERAL(' '), &wrapper_argv); |
| 399 // Prepend the wrapper and update the switches/arguments |begin_args_|. | 292 // Prepend the wrapper and update the switches/arguments |begin_args_|. |
| 400 argv_.insert(argv_.begin(), wrapper_argv.begin(), wrapper_argv.end()); | 293 argv_.insert(argv_.begin(), wrapper_argv.begin(), wrapper_argv.end()); |
| 401 begin_args_ += wrapper_argv.size(); | 294 begin_args_ += wrapper_argv.size(); |
| 402 } | 295 } |
| 403 | 296 |
| 404 #if defined(OS_WIN) | |
| 405 void CommandLine::ParseFromString(const string16& command_line) { | |
| 406 string16 command_line_string; | |
| 407 TrimWhitespace(command_line, TRIM_ALL, &command_line_string); | |
| 408 if (command_line_string.empty()) | |
| 409 return; | |
| 410 | |
| 411 int num_args = 0; | |
| 412 wchar_t** args = NULL; | |
| 413 args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args); | |
| 414 | |
| 415 DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: " | |
| 416 << UTF16ToUTF8(command_line); | |
| 417 InitFromArgv(num_args, args); | |
| 418 LocalFree(args); | |
| 419 } | |
| 420 #endif | |
| 421 | |
| 422 CommandLine::StringType CommandLine::GetCommandLineStringInternal( | 297 CommandLine::StringType CommandLine::GetCommandLineStringInternal( |
| 423 bool quote_placeholders) const { | 298 bool quote_placeholders) const { |
| 424 StringType string(argv_[0]); | 299 StringType string(argv_[0]); |
| 425 #if defined(OS_WIN) | |
| 426 string = QuoteForCommandLineToArgvW(string, quote_placeholders); | |
| 427 #endif | |
| 428 StringType params(GetArgumentsStringInternal(quote_placeholders)); | 300 StringType params(GetArgumentsStringInternal(quote_placeholders)); |
| 429 if (!params.empty()) { | 301 if (!params.empty()) { |
| 430 string.append(StringType(FILE_PATH_LITERAL(" "))); | 302 string.append(StringType(FILE_PATH_LITERAL(" "))); |
| 431 string.append(params); | 303 string.append(params); |
| 432 } | 304 } |
| 433 return string; | 305 return string; |
| 434 } | 306 } |
| 435 | 307 |
| 436 CommandLine::StringType CommandLine::GetArgumentsStringInternal( | 308 CommandLine::StringType CommandLine::GetArgumentsStringInternal( |
| 437 bool quote_placeholders) const { | 309 bool quote_placeholders) const { |
| 438 StringType params; | 310 StringType params; |
| 439 // Append switches and arguments. | 311 // Append switches and arguments. |
| 440 bool parse_switches = true; | 312 bool parse_switches = true; |
| 441 for (size_t i = 1; i < argv_.size(); ++i) { | 313 for (size_t i = 1; i < argv_.size(); ++i) { |
| 442 StringType arg = argv_[i]; | 314 StringType arg = argv_[i]; |
| 443 StringType switch_string; | 315 StringType switch_string; |
| 444 StringType switch_value; | 316 StringType switch_value; |
| 445 parse_switches &= arg != kSwitchTerminator; | 317 parse_switches &= arg != kSwitchTerminator; |
| 446 if (i > 1) | 318 if (i > 1) |
| 447 params.append(StringType(FILE_PATH_LITERAL(" "))); | 319 params.append(StringType(FILE_PATH_LITERAL(" "))); |
| 448 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) { | 320 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) { |
| 449 params.append(switch_string); | 321 params.append(switch_string); |
| 450 if (!switch_value.empty()) { | 322 if (!switch_value.empty()) { |
| 451 #if defined(OS_WIN) | |
| 452 switch_value = | |
| 453 QuoteForCommandLineToArgvW(switch_value, quote_placeholders); | |
| 454 #endif | |
| 455 params.append(kSwitchValueSeparator + switch_value); | 323 params.append(kSwitchValueSeparator + switch_value); |
| 456 } | 324 } |
| 457 } else { | 325 } else { |
| 458 #if defined(OS_WIN) | |
| 459 arg = QuoteForCommandLineToArgvW(arg, quote_placeholders); | |
| 460 #endif | |
| 461 params.append(arg); | 326 params.append(arg); |
| 462 } | 327 } |
| 463 } | 328 } |
| 464 return params; | 329 return params; |
| 465 } | 330 } |
| 466 | 331 |
| 467 void CommandLine::ResetStringPieces() { | 332 void CommandLine::ResetStringPieces() { |
| 468 switches_by_stringpiece_.clear(); | 333 switches_by_stringpiece_.clear(); |
| 469 for (const auto& entry : switches_) | 334 for (const auto& entry : switches_) |
| 470 switches_by_stringpiece_[entry.first] = &(entry.second); | 335 switches_by_stringpiece_[entry.first] = &(entry.second); |
| 471 } | 336 } |
| 472 | 337 |
| 473 } // namespace base | 338 } // namespace base |
| OLD | NEW |