OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #include <shellapi.h> | 9 #include <shellapi.h> |
10 #endif | 10 #endif |
(...skipping 11 matching lines...) Expand all Loading... |
22 // Since we use a lazy match, make sure that longer versions (like L"--") | 22 // Since we use a lazy match, make sure that longer versions (like L"--") |
23 // are listed before shorter versions (like L"-") of similar prefixes. | 23 // are listed before shorter versions (like L"-") of similar prefixes. |
24 #if defined(OS_WIN) | 24 #if defined(OS_WIN) |
25 const wchar_t* const CommandLine::kSwitchPrefixes[] = {L"--", L"-", L"/"}; | 25 const wchar_t* const CommandLine::kSwitchPrefixes[] = {L"--", L"-", L"/"}; |
26 #elif defined(OS_POSIX) | 26 #elif defined(OS_POSIX) |
27 // Unixes don't use slash as a switch. | 27 // Unixes don't use slash as a switch. |
28 const wchar_t* const CommandLine::kSwitchPrefixes[] = {L"--", L"-"}; | 28 const wchar_t* const CommandLine::kSwitchPrefixes[] = {L"--", L"-"}; |
29 #endif | 29 #endif |
30 | 30 |
31 const wchar_t CommandLine::kSwitchValueSeparator[] = L"="; | 31 const wchar_t CommandLine::kSwitchValueSeparator[] = L"="; |
| 32 const wchar_t CommandLine::kSwitchTerminator[] = L"--"; |
32 | 33 |
33 // Needed to avoid a typecast on the tolower() function pointer in Lowercase(). | 34 // Needed to avoid a typecast on the tolower() function pointer in Lowercase(). |
34 // MSVC accepts it as-is but GCC requires the typecast. | 35 // MSVC accepts it as-is but GCC requires the typecast. |
35 static int ToLower(int c) { | 36 static int ToLower(int c) { |
36 return tolower(c); | 37 return tolower(c); |
37 } | 38 } |
38 | 39 |
39 static void Lowercase(wstring* parameter) { | 40 static void Lowercase(wstring* parameter) { |
40 transform(parameter->begin(), parameter->end(), parameter->begin(), | 41 transform(parameter->begin(), parameter->end(), parameter->begin(), |
41 ToLower); | 42 ToLower); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 return; | 82 return; |
82 | 83 |
83 int num_args = 0; | 84 int num_args = 0; |
84 wchar_t** args = NULL; | 85 wchar_t** args = NULL; |
85 | 86 |
86 args = CommandLineToArgvW(command_line_string_.c_str(), &num_args); | 87 args = CommandLineToArgvW(command_line_string_.c_str(), &num_args); |
87 | 88 |
88 // Populate program_ with the trimmed version of the first arg. | 89 // Populate program_ with the trimmed version of the first arg. |
89 TrimWhitespace(args[0], TRIM_ALL, &program_); | 90 TrimWhitespace(args[0], TRIM_ALL, &program_); |
90 | 91 |
| 92 bool parse_switches = true; |
91 for (int i = 1; i < num_args; ++i) { | 93 for (int i = 1; i < num_args; ++i) { |
92 wstring arg; | 94 wstring arg; |
93 TrimWhitespace(args[i], TRIM_ALL, &arg); | 95 TrimWhitespace(args[i], TRIM_ALL, &arg); |
94 | 96 |
| 97 if (!parse_switches) { |
| 98 loose_values_.push_back(arg); |
| 99 continue; |
| 100 } |
| 101 |
| 102 if (arg == kSwitchTerminator) { |
| 103 parse_switches = false; |
| 104 continue; |
| 105 } |
| 106 |
95 wstring switch_string; | 107 wstring switch_string; |
96 wstring switch_value; | 108 wstring switch_value; |
97 if (IsSwitch(arg, &switch_string, &switch_value)) { | 109 if (IsSwitch(arg, &switch_string, &switch_value)) { |
98 switches_[switch_string] = switch_value; | 110 switches_[switch_string] = switch_value; |
99 } else { | 111 } else { |
100 loose_values_.push_back(arg); | 112 loose_values_.push_back(arg); |
101 } | 113 } |
102 } | 114 } |
103 | 115 |
104 if (args) | 116 if (args) |
105 LocalFree(args); | 117 LocalFree(args); |
106 } | 118 } |
107 | 119 |
108 #elif defined(OS_POSIX) | 120 #elif defined(OS_POSIX) |
109 // Does the actual parsing of the command line. | 121 // Does the actual parsing of the command line. |
110 void Init(int argc, const char* const* argv) { | 122 void Init(int argc, const char* const* argv) { |
111 if (argc < 1) | 123 if (argc < 1) |
112 return; | 124 return; |
113 program_ = base::SysNativeMBToWide(argv[0]); | 125 program_ = base::SysNativeMBToWide(argv[0]); |
114 command_line_string_ = program_; | 126 command_line_string_ = program_; |
115 | 127 |
| 128 bool parse_switches = true; |
116 for (int i = 1; i < argc; ++i) { | 129 for (int i = 1; i < argc; ++i) { |
117 std::wstring arg = base::SysNativeMBToWide(argv[i]); | 130 std::wstring arg = base::SysNativeMBToWide(argv[i]); |
118 command_line_string_.append(L" "); | 131 command_line_string_.append(L" "); |
119 command_line_string_.append(arg); | 132 command_line_string_.append(arg); |
120 | 133 |
| 134 if (!parse_switches) { |
| 135 loose_values_.push_back(arg); |
| 136 continue; |
| 137 } |
| 138 |
| 139 if (arg == kSwitchTerminator) { |
| 140 parse_switches = false; |
| 141 continue; |
| 142 } |
| 143 |
121 wstring switch_string; | 144 wstring switch_string; |
122 wstring switch_value; | 145 wstring switch_value; |
123 if (IsSwitch(arg, &switch_string, &switch_value)) { | 146 if (IsSwitch(arg, &switch_string, &switch_value)) { |
124 switches_[switch_string] = switch_value; | 147 switches_[switch_string] = switch_value; |
125 } else { | 148 } else { |
126 loose_values_.push_back(arg); | 149 loose_values_.push_back(arg); |
127 } | 150 } |
128 } | 151 } |
129 } | 152 } |
130 #endif | 153 #endif |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 if ((value_string.find(L" ") != std::wstring::npos) && | 306 if ((value_string.find(L" ") != std::wstring::npos) && |
284 (value_string[0] != L'"') && | 307 (value_string[0] != L'"') && |
285 (value_string[value_string.length() - 1] != L'"')) { | 308 (value_string[value_string.length() - 1] != L'"')) { |
286 // need to provide quotes | 309 // need to provide quotes |
287 StringAppendF(command_line_string, L"\"%ls\"", value_string.c_str()); | 310 StringAppendF(command_line_string, L"\"%ls\"", value_string.c_str()); |
288 } else { | 311 } else { |
289 command_line_string->append(value_string); | 312 command_line_string->append(value_string); |
290 } | 313 } |
291 } | 314 } |
292 | 315 |
OLD | NEW |