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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 out.push_back('"'); | 138 out.push_back('"'); |
139 | 139 |
140 return out; | 140 return out; |
141 } | 141 } |
142 #endif | 142 #endif |
143 | 143 |
144 } // namespace | 144 } // namespace |
145 | 145 |
146 CommandLine::CommandLine(NoProgram no_program) | 146 CommandLine::CommandLine(NoProgram no_program) |
147 : argv_(1), | 147 : argv_(1), |
148 begin_args_(1) { | 148 begin_args_(1), |
| 149 switch_lengths_mask_(0) { |
149 } | 150 } |
150 | 151 |
151 CommandLine::CommandLine(const FilePath& program) | 152 CommandLine::CommandLine(const FilePath& program) |
152 : argv_(1), | 153 : argv_(1), |
153 begin_args_(1) { | 154 begin_args_(1), |
| 155 switch_lengths_mask_(0) { |
154 SetProgram(program); | 156 SetProgram(program); |
155 } | 157 } |
156 | 158 |
157 CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv) | 159 CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv) |
158 : argv_(1), | 160 : argv_(1), |
159 begin_args_(1) { | 161 begin_args_(1), |
| 162 switch_lengths_mask_(0) { |
160 InitFromArgv(argc, argv); | 163 InitFromArgv(argc, argv); |
161 } | 164 } |
162 | 165 |
163 CommandLine::CommandLine(const StringVector& argv) | 166 CommandLine::CommandLine(const StringVector& argv) |
164 : argv_(1), | 167 : argv_(1), |
165 begin_args_(1) { | 168 begin_args_(1), |
| 169 switch_lengths_mask_(0) { |
166 InitFromArgv(argv); | 170 InitFromArgv(argv); |
167 } | 171 } |
168 | 172 |
169 CommandLine::~CommandLine() { | 173 CommandLine::~CommandLine() { |
170 } | 174 } |
171 | 175 |
172 // static | 176 // static |
173 bool CommandLine::Init(int argc, const char* const* argv) { | 177 bool CommandLine::Init(int argc, const char* const* argv) { |
174 if (current_process_commandline_) { | 178 if (current_process_commandline_) { |
175 // If this is intentional, Reset() must be called first. If we are using | 179 // If this is intentional, Reset() must be called first. If we are using |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 const CommandLine::CharType* const* argv) { | 223 const CommandLine::CharType* const* argv) { |
220 StringVector new_argv; | 224 StringVector new_argv; |
221 for (int i = 0; i < argc; ++i) | 225 for (int i = 0; i < argc; ++i) |
222 new_argv.push_back(argv[i]); | 226 new_argv.push_back(argv[i]); |
223 InitFromArgv(new_argv); | 227 InitFromArgv(new_argv); |
224 } | 228 } |
225 | 229 |
226 void CommandLine::InitFromArgv(const StringVector& argv) { | 230 void CommandLine::InitFromArgv(const StringVector& argv) { |
227 argv_ = StringVector(1); | 231 argv_ = StringVector(1); |
228 switches_.clear(); | 232 switches_.clear(); |
| 233 switch_lengths_mask_ = 0; |
229 begin_args_ = 1; | 234 begin_args_ = 1; |
230 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0])); | 235 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0])); |
231 AppendSwitchesAndArguments(*this, argv); | 236 AppendSwitchesAndArguments(*this, argv); |
232 } | 237 } |
233 | 238 |
234 CommandLine::StringType CommandLine::GetCommandLineString() const { | 239 CommandLine::StringType CommandLine::GetCommandLineString() const { |
235 StringType string(argv_[0]); | 240 StringType string(argv_[0]); |
236 #if defined(OS_WIN) | 241 #if defined(OS_WIN) |
237 string = QuoteForCommandLineToArgvW(string); | 242 string = QuoteForCommandLineToArgvW(string); |
238 #endif | 243 #endif |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 } | 280 } |
276 | 281 |
277 FilePath CommandLine::GetProgram() const { | 282 FilePath CommandLine::GetProgram() const { |
278 return FilePath(argv_[0]); | 283 return FilePath(argv_[0]); |
279 } | 284 } |
280 | 285 |
281 void CommandLine::SetProgram(const FilePath& program) { | 286 void CommandLine::SetProgram(const FilePath& program) { |
282 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]); | 287 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]); |
283 } | 288 } |
284 | 289 |
285 bool CommandLine::HasSwitch(const std::string& switch_string) const { | 290 bool CommandLine::HasStringSwitch(const std::string& switch_string) const { |
286 return switches_.find(LowerASCIIOnWindows(switch_string)) != switches_.end(); | 291 return switches_.find(LowerASCIIOnWindows(switch_string)) != switches_.end(); |
287 } | 292 } |
288 | 293 |
| 294 bool CommandLine::HasSwitch(const char* switch_string, |
| 295 size_t string_length) const { |
| 296 return HasSwitch(std::string(switch_string, string_length)); |
| 297 } |
| 298 |
289 std::string CommandLine::GetSwitchValueASCII( | 299 std::string CommandLine::GetSwitchValueASCII( |
290 const std::string& switch_string) const { | 300 const std::string& switch_string) const { |
291 StringType value = GetSwitchValueNative(switch_string); | 301 StringType value = GetSwitchValueNative(switch_string); |
292 if (!IsStringASCII(value)) { | 302 if (!IsStringASCII(value)) { |
293 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII."; | 303 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII."; |
294 return std::string(); | 304 return std::string(); |
295 } | 305 } |
296 #if defined(OS_WIN) | 306 #if defined(OS_WIN) |
297 return WideToASCII(value); | 307 return WideToASCII(value); |
298 #else | 308 #else |
(...skipping 25 matching lines...) Expand all Loading... |
324 void CommandLine::AppendSwitchNative(const std::string& switch_string, | 334 void CommandLine::AppendSwitchNative(const std::string& switch_string, |
325 const CommandLine::StringType& value) { | 335 const CommandLine::StringType& value) { |
326 std::string switch_key(LowerASCIIOnWindows(switch_string)); | 336 std::string switch_key(LowerASCIIOnWindows(switch_string)); |
327 #if defined(OS_WIN) | 337 #if defined(OS_WIN) |
328 StringType combined_switch_string(ASCIIToWide(switch_key)); | 338 StringType combined_switch_string(ASCIIToWide(switch_key)); |
329 #elif defined(OS_POSIX) | 339 #elif defined(OS_POSIX) |
330 StringType combined_switch_string(switch_string); | 340 StringType combined_switch_string(switch_string); |
331 #endif | 341 #endif |
332 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); | 342 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); |
333 switches_[switch_key.substr(prefix_length)] = value; | 343 switches_[switch_key.substr(prefix_length)] = value; |
| 344 switch_lengths_mask_ |= 1 << (prefix_length - 1); |
334 // Preserve existing switch prefixes in |argv_|; only append one if necessary. | 345 // Preserve existing switch prefixes in |argv_|; only append one if necessary. |
335 if (prefix_length == 0) | 346 if (prefix_length == 0) |
336 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string; | 347 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string; |
337 if (!value.empty()) | 348 if (!value.empty()) |
338 combined_switch_string += kSwitchValueSeparator + value; | 349 combined_switch_string += kSwitchValueSeparator + value; |
339 // Append the switch and update the switches/arguments divider |begin_args_|. | 350 // Append the switch and update the switches/arguments divider |begin_args_|. |
340 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string); | 351 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string); |
341 } | 352 } |
342 | 353 |
343 void CommandLine::AppendSwitchASCII(const std::string& switch_string, | 354 void CommandLine::AppendSwitchASCII(const std::string& switch_string, |
344 const std::string& value_string) { | 355 const std::string& value_string) { |
345 #if defined(OS_WIN) | 356 #if defined(OS_WIN) |
346 AppendSwitchNative(switch_string, ASCIIToWide(value_string)); | 357 AppendSwitchNative(switch_string, ASCIIToWide(value_string)); |
347 #elif defined(OS_POSIX) | 358 #elif defined(OS_POSIX) |
348 AppendSwitchNative(switch_string, value_string); | 359 AppendSwitchNative(switch_string, value_string); |
349 #endif | 360 #endif |
350 } | 361 } |
351 | 362 |
352 void CommandLine::CopySwitchesFrom(const CommandLine& source, | 363 void CommandLine::CopySwitchesFrom(const CommandLine& source, |
353 const char* const switches[], | 364 const char* const switches[], |
354 size_t count) { | 365 size_t count) { |
355 for (size_t i = 0; i < count; ++i) { | 366 for (size_t i = 0; i < count; ++i) { |
356 if (source.HasSwitch(switches[i])) | 367 std::string s = switches[i]; |
357 AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i])); | 368 if (source.HasSwitch(s)) |
| 369 AppendSwitchNative(s, source.GetSwitchValueNative(s)); |
358 } | 370 } |
359 } | 371 } |
360 | 372 |
361 CommandLine::StringVector CommandLine::GetArgs() const { | 373 CommandLine::StringVector CommandLine::GetArgs() const { |
362 // Gather all arguments after the last switch (may include kSwitchTerminator). | 374 // Gather all arguments after the last switch (may include kSwitchTerminator). |
363 StringVector args(argv_.begin() + begin_args_, argv_.end()); | 375 StringVector args(argv_.begin() + begin_args_, argv_.end()); |
364 // Erase only the first kSwitchTerminator (maybe "--" is a legitimate page?) | 376 // Erase only the first kSwitchTerminator (maybe "--" is a legitimate page?) |
365 StringVector::iterator switch_terminator = | 377 StringVector::iterator switch_terminator = |
366 std::find(args.begin(), args.end(), kSwitchTerminator); | 378 std::find(args.begin(), args.end(), kSwitchTerminator); |
367 if (switch_terminator != args.end()) | 379 if (switch_terminator != args.end()) |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 int num_args = 0; | 427 int num_args = 0; |
416 wchar_t** args = NULL; | 428 wchar_t** args = NULL; |
417 args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args); | 429 args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args); |
418 | 430 |
419 DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: " | 431 DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: " |
420 << command_line; | 432 << command_line; |
421 InitFromArgv(num_args, args); | 433 InitFromArgv(num_args, args); |
422 LocalFree(args); | 434 LocalFree(args); |
423 } | 435 } |
424 #endif | 436 #endif |
OLD | NEW |