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" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 #if defined(OS_WIN) | 98 #if defined(OS_WIN) |
99 return base::StringToLowerASCII(string); | 99 return base::StringToLowerASCII(string); |
100 #elif defined(OS_POSIX) | 100 #elif defined(OS_POSIX) |
101 return string; | 101 return string; |
102 #endif | 102 #endif |
103 } | 103 } |
104 | 104 |
105 | 105 |
106 #if defined(OS_WIN) | 106 #if defined(OS_WIN) |
107 // Quote a string as necessary for CommandLineToArgvW compatiblity *on Windows*. | 107 // Quote a string as necessary for CommandLineToArgvW compatiblity *on Windows*. |
108 std::wstring QuoteForCommandLineToArgvW(const std::wstring& arg) { | 108 std::wstring QuoteForCommandLineToArgvW(const std::wstring& arg, |
| 109 bool quote_placeholders) { |
109 // We follow the quoting rules of CommandLineToArgvW. | 110 // We follow the quoting rules of CommandLineToArgvW. |
110 // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx | 111 // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx |
111 if (arg.find_first_of(L" \\\"") == std::wstring::npos) { | 112 std::wstring quotable_chars(L" \\\""); |
| 113 // We may also be required to quote '%', which is commonly used in a command |
| 114 // line as a placeholder. (It may be substituted for a string with spaces.) |
| 115 if (quote_placeholders) |
| 116 quotable_chars.push_back(L'%'); |
| 117 if (arg.find_first_of(quotable_chars) == std::wstring::npos) { |
112 // No quoting necessary. | 118 // No quoting necessary. |
113 return arg; | 119 return arg; |
114 } | 120 } |
115 | 121 |
116 std::wstring out; | 122 std::wstring out; |
117 out.push_back(L'"'); | 123 out.push_back(L'"'); |
118 for (size_t i = 0; i < arg.size(); ++i) { | 124 for (size_t i = 0; i < arg.size(); ++i) { |
119 if (arg[i] == '\\') { | 125 if (arg[i] == '\\') { |
120 // Find the extent of this run of backslashes. | 126 // Find the extent of this run of backslashes. |
121 size_t start = i, end = start + 1; | 127 size_t start = i, end = start + 1; |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 } | 246 } |
241 | 247 |
242 void CommandLine::InitFromArgv(const StringVector& argv) { | 248 void CommandLine::InitFromArgv(const StringVector& argv) { |
243 argv_ = StringVector(1); | 249 argv_ = StringVector(1); |
244 switches_.clear(); | 250 switches_.clear(); |
245 begin_args_ = 1; | 251 begin_args_ = 1; |
246 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0])); | 252 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0])); |
247 AppendSwitchesAndArguments(*this, argv); | 253 AppendSwitchesAndArguments(*this, argv); |
248 } | 254 } |
249 | 255 |
250 CommandLine::StringType CommandLine::GetCommandLineString() const { | 256 CommandLine::StringType CommandLine::GetCommandLineString( |
| 257 bool quote_placeholders) const { |
251 StringType string(argv_[0]); | 258 StringType string(argv_[0]); |
252 #if defined(OS_WIN) | 259 #if defined(OS_WIN) |
253 string = QuoteForCommandLineToArgvW(string); | 260 string = QuoteForCommandLineToArgvW(string, quote_placeholders); |
254 #endif | 261 #endif |
255 StringType params(GetArgumentsString()); | 262 StringType params(GetArgumentsString(quote_placeholders)); |
256 if (!params.empty()) { | 263 if (!params.empty()) { |
257 string.append(StringType(FILE_PATH_LITERAL(" "))); | 264 string.append(StringType(FILE_PATH_LITERAL(" "))); |
258 string.append(params); | 265 string.append(params); |
259 } | 266 } |
260 return string; | 267 return string; |
261 } | 268 } |
262 | 269 |
263 CommandLine::StringType CommandLine::GetArgumentsString() const { | 270 CommandLine::StringType CommandLine::GetArgumentsString( |
| 271 bool quote_placeholders) const { |
264 StringType params; | 272 StringType params; |
265 // Append switches and arguments. | 273 // Append switches and arguments. |
266 bool parse_switches = true; | 274 bool parse_switches = true; |
267 for (size_t i = 1; i < argv_.size(); ++i) { | 275 for (size_t i = 1; i < argv_.size(); ++i) { |
268 StringType arg = argv_[i]; | 276 StringType arg = argv_[i]; |
269 StringType switch_string; | 277 StringType switch_string; |
270 StringType switch_value; | 278 StringType switch_value; |
271 parse_switches &= arg != kSwitchTerminator; | 279 parse_switches &= arg != kSwitchTerminator; |
272 if (i > 1) | 280 if (i > 1) |
273 params.append(StringType(FILE_PATH_LITERAL(" "))); | 281 params.append(StringType(FILE_PATH_LITERAL(" "))); |
274 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) { | 282 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) { |
275 params.append(switch_string); | 283 params.append(switch_string); |
276 if (!switch_value.empty()) { | 284 if (!switch_value.empty()) { |
277 #if defined(OS_WIN) | 285 #if defined(OS_WIN) |
278 switch_value = QuoteForCommandLineToArgvW(switch_value); | 286 switch_value = |
| 287 QuoteForCommandLineToArgvW(switch_value, quote_placeholders); |
279 #endif | 288 #endif |
280 params.append(kSwitchValueSeparator + switch_value); | 289 params.append(kSwitchValueSeparator + switch_value); |
281 } | 290 } |
282 } | 291 } |
283 else { | 292 else { |
284 #if defined(OS_WIN) | 293 #if defined(OS_WIN) |
285 arg = QuoteForCommandLineToArgvW(arg); | 294 arg = QuoteForCommandLineToArgvW(arg, quote_placeholders); |
286 #endif | 295 #endif |
287 params.append(arg); | 296 params.append(arg); |
288 } | 297 } |
289 } | 298 } |
290 return params; | 299 return params; |
291 } | 300 } |
292 | 301 |
293 FilePath CommandLine::GetProgram() const { | 302 FilePath CommandLine::GetProgram() const { |
294 return FilePath(argv_[0]); | 303 return FilePath(argv_[0]); |
295 } | 304 } |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args); | 442 args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args); |
434 | 443 |
435 DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: " | 444 DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: " |
436 << UTF16ToUTF8(command_line); | 445 << UTF16ToUTF8(command_line); |
437 InitFromArgv(num_args, args); | 446 InitFromArgv(num_args, args); |
438 LocalFree(args); | 447 LocalFree(args); |
439 } | 448 } |
440 #endif | 449 #endif |
441 | 450 |
442 } // namespace base | 451 } // namespace base |
OLD | NEW |