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) | 18 #if defined(OS_WIN) |
19 #include <windows.h> | 19 #include <windows.h> |
20 #include <shellapi.h> | 20 #include <shellapi.h> |
21 #endif | 21 #endif |
22 | 22 |
23 using base::FilePath; | 23 using base::FilePath; |
24 | 24 |
25 CommandLine* CommandLine::current_process_commandline_ = NULL; | 25 CommandLine* CommandLine::current_process_commandline_ = NULL; |
26 | 26 |
27 namespace { | 27 namespace { |
28 const CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--"); | 28 const CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--"); |
29 const CommandLine::CharType kSwitchValueSeparator[] = FILE_PATH_LITERAL("="); | 29 const CommandLine::CharType kSwitchValueSeparator[] = FILE_PATH_LITERAL("="); |
| 30 |
30 // Since we use a lazy match, make sure that longer versions (like "--") are | 31 // Since we use a lazy match, make sure that longer versions (like "--") are |
31 // listed before shorter versions (like "-") of similar prefixes. | 32 // listed before shorter versions (like "-") of similar prefixes. |
32 #if defined(OS_WIN) | 33 #if defined(OS_WIN) |
| 34 // By putting slash last, we can control whether it is treaded as a switch |
| 35 // value by changing the value of switch_prefix_count to be one less than |
| 36 // the array size. |
33 const CommandLine::CharType* const kSwitchPrefixes[] = {L"--", L"-", L"/"}; | 37 const CommandLine::CharType* const kSwitchPrefixes[] = {L"--", L"-", L"/"}; |
34 #elif defined(OS_POSIX) | 38 #elif defined(OS_POSIX) |
35 // Unixes don't use slash as a switch. | 39 // Unixes don't use slash as a switch. |
36 const CommandLine::CharType* const kSwitchPrefixes[] = {"--", "-"}; | 40 const CommandLine::CharType* const kSwitchPrefixes[] = {"--", "-"}; |
37 #endif | 41 #endif |
| 42 size_t switch_prefix_count = arraysize(kSwitchPrefixes); |
38 | 43 |
39 size_t GetSwitchPrefixLength(const CommandLine::StringType& string) { | 44 size_t GetSwitchPrefixLength(const CommandLine::StringType& string) { |
40 for (size_t i = 0; i < arraysize(kSwitchPrefixes); ++i) { | 45 for (size_t i = 0; i < switch_prefix_count; ++i) { |
41 CommandLine::StringType prefix(kSwitchPrefixes[i]); | 46 CommandLine::StringType prefix(kSwitchPrefixes[i]); |
42 if (string.compare(0, prefix.length(), prefix) == 0) | 47 if (string.compare(0, prefix.length(), prefix) == 0) |
43 return prefix.length(); | 48 return prefix.length(); |
44 } | 49 } |
45 return 0; | 50 return 0; |
46 } | 51 } |
47 | 52 |
48 // Fills in |switch_string| and |switch_value| if |string| is a switch. | 53 // Fills in |switch_string| and |switch_value| if |string| is a switch. |
49 // This will preserve the input switch prefix in the output |switch_string|. | 54 // This will preserve the input switch prefix in the output |switch_string|. |
50 bool IsSwitch(const CommandLine::StringType& string, | 55 bool IsSwitch(const CommandLine::StringType& string, |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 | 167 |
163 CommandLine::CommandLine(const StringVector& argv) | 168 CommandLine::CommandLine(const StringVector& argv) |
164 : argv_(1), | 169 : argv_(1), |
165 begin_args_(1) { | 170 begin_args_(1) { |
166 InitFromArgv(argv); | 171 InitFromArgv(argv); |
167 } | 172 } |
168 | 173 |
169 CommandLine::~CommandLine() { | 174 CommandLine::~CommandLine() { |
170 } | 175 } |
171 | 176 |
| 177 #if defined(OS_WIN) |
| 178 // static |
| 179 void CommandLine::set_slash_is_not_a_switch() { |
| 180 // The last switch prefix should be slash, so adjust the size to skip it. |
| 181 DCHECK(wcscmp(kSwitchPrefixes[arraysize(kSwitchPrefixes) - 1], L"/") == 0); |
| 182 switch_prefix_count = arraysize(kSwitchPrefixes) - 1; |
| 183 } |
| 184 #endif |
| 185 |
172 // static | 186 // static |
173 bool CommandLine::Init(int argc, const char* const* argv) { | 187 bool CommandLine::Init(int argc, const char* const* argv) { |
174 if (current_process_commandline_) { | 188 if (current_process_commandline_) { |
175 // If this is intentional, Reset() must be called first. If we are using | 189 // If this is intentional, Reset() must be called first. If we are using |
176 // the shared build mode, we have to share a single object across multiple | 190 // the shared build mode, we have to share a single object across multiple |
177 // shared libraries. | 191 // shared libraries. |
178 return false; | 192 return false; |
179 } | 193 } |
180 | 194 |
181 current_process_commandline_ = new CommandLine(NO_PROGRAM); | 195 current_process_commandline_ = new CommandLine(NO_PROGRAM); |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 int num_args = 0; | 429 int num_args = 0; |
416 wchar_t** args = NULL; | 430 wchar_t** args = NULL; |
417 args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args); | 431 args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args); |
418 | 432 |
419 DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: " | 433 DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: " |
420 << command_line; | 434 << command_line; |
421 InitFromArgv(num_args, args); | 435 InitFromArgv(num_args, args); |
422 LocalFree(args); | 436 LocalFree(args); |
423 } | 437 } |
424 #endif | 438 #endif |
OLD | NEW |