OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/file_path.h" | 11 #include "base/file_path.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/string_split.h" | 13 #include "base/string_split.h" |
14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
15 #include "base/utf_string_conversions.h" | 15 #include "base/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 CommandLine* CommandLine::current_process_commandline_ = NULL; | 23 CommandLine* CommandLine::current_process_commandline_ = NULL; |
24 | 24 |
25 namespace { | 25 namespace { |
26 typedef CommandLine::StringType::value_type CharType; | 26 const CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--"); |
27 | 27 const CommandLine::CharType kSwitchValueSeparator[] = FILE_PATH_LITERAL("="); |
28 const CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--"); | |
29 const CharType kSwitchValueSeparator[] = FILE_PATH_LITERAL("="); | |
30 // Since we use a lazy match, make sure that longer versions (like "--") are | 28 // Since we use a lazy match, make sure that longer versions (like "--") are |
31 // listed before shorter versions (like "-") of similar prefixes. | 29 // listed before shorter versions (like "-") of similar prefixes. |
32 #if defined(OS_WIN) | 30 #if defined(OS_WIN) |
33 const CharType* const kSwitchPrefixes[] = {L"--", L"-", L"/"}; | 31 const CommandLine::CharType* const kSwitchPrefixes[] = {L"--", L"-", L"/"}; |
34 #elif defined(OS_POSIX) | 32 #elif defined(OS_POSIX) |
35 // Unixes don't use slash as a switch. | 33 // Unixes don't use slash as a switch. |
36 const CharType* const kSwitchPrefixes[] = {"--", "-"}; | 34 const CommandLine::CharType* const kSwitchPrefixes[] = {"--", "-"}; |
37 #endif | 35 #endif |
38 | 36 |
39 #if defined(OS_WIN) | 37 size_t GetSwitchPrefixLength(const CommandLine::StringType& string) { |
40 // Lowercase a string for case-insensitivity of switches. | 38 for (size_t i = 0; i < arraysize(kSwitchPrefixes); ++i) { |
41 // Is this desirable? It exists for backwards compatibility on Windows. | 39 CommandLine::StringType prefix(kSwitchPrefixes[i]); |
42 void Lowercase(std::string* arg) { | 40 if (string.find(prefix) == 0) |
43 transform(arg->begin(), arg->end(), arg->begin(), tolower); | 41 return prefix.length(); |
| 42 } |
| 43 return 0; |
44 } | 44 } |
45 | 45 |
46 // Quote a string if necessary, such that CommandLineToArgvW() will always | 46 // Fills in |switch_string| and |switch_value| if |string| is a switch. |
47 // process it as a single argument. | 47 // This will preserve the input switch prefix in the output |switch_string|. |
48 std::wstring WindowsStyleQuote(const std::wstring& arg) { | 48 bool IsSwitch(const CommandLine::StringType& string, |
| 49 CommandLine::StringType* switch_string, |
| 50 CommandLine::StringType* switch_value) { |
| 51 switch_string->clear(); |
| 52 switch_value->clear(); |
| 53 if (GetSwitchPrefixLength(string) == 0) |
| 54 return false; |
| 55 |
| 56 const size_t equals_position = string.find(kSwitchValueSeparator); |
| 57 *switch_string = string.substr(0, equals_position); |
| 58 if (equals_position != CommandLine::StringType::npos) |
| 59 *switch_value = string.substr(equals_position + 1); |
| 60 return true; |
| 61 } |
| 62 |
| 63 // Append switches and arguments, keeping switches before arguments. |
| 64 void AppendSwitchesAndArguments(CommandLine& command_line, |
| 65 const CommandLine::StringVector& argv) { |
| 66 bool parse_switches = true; |
| 67 for (size_t i = 1; i < argv.size(); ++i) { |
| 68 CommandLine::StringType arg = argv[i]; |
| 69 TrimWhitespace(arg, TRIM_ALL, &arg); |
| 70 |
| 71 CommandLine::StringType switch_string; |
| 72 CommandLine::StringType switch_value; |
| 73 parse_switches &= (arg != kSwitchTerminator); |
| 74 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) { |
| 75 #if defined(OS_WIN) |
| 76 command_line.AppendSwitchNative(WideToASCII(switch_string), switch_value); |
| 77 #elif defined(OS_POSIX) |
| 78 command_line.AppendSwitchNative(switch_string, switch_value); |
| 79 #endif |
| 80 } else { |
| 81 command_line.AppendArgNative(arg); |
| 82 } |
| 83 } |
| 84 } |
| 85 |
| 86 // Lowercase switches for backwards compatiblity *on Windows*. |
| 87 std::string LowerASCIIOnWindows(const std::string& string) { |
| 88 #if defined(OS_WIN) |
| 89 return StringToLowerASCII(string); |
| 90 #elif defined(OS_POSIX) |
| 91 return string; |
| 92 #endif |
| 93 } |
| 94 |
| 95 |
| 96 #if defined(OS_WIN) |
| 97 // Quote a string as necessary for CommandLineToArgvW compatiblity *on Windows*. |
| 98 std::wstring QuoteForCommandLineToArgvW(const std::wstring& arg) { |
49 // We follow the quoting rules of CommandLineToArgvW. | 99 // We follow the quoting rules of CommandLineToArgvW. |
50 // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx | 100 // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx |
51 if (arg.find_first_of(L" \\\"") == std::wstring::npos) { | 101 if (arg.find_first_of(L" \\\"") == std::wstring::npos) { |
52 // No quoting necessary. | 102 // No quoting necessary. |
53 return arg; | 103 return arg; |
54 } | 104 } |
55 | 105 |
56 std::wstring out; | 106 std::wstring out; |
57 out.push_back(L'"'); | 107 out.push_back(L'"'); |
58 for (size_t i = 0; i < arg.size(); ++i) { | 108 for (size_t i = 0; i < arg.size(); ++i) { |
(...skipping 22 matching lines...) Expand all Loading... |
81 } else { | 131 } else { |
82 out.push_back(arg[i]); | 132 out.push_back(arg[i]); |
83 } | 133 } |
84 } | 134 } |
85 out.push_back('"'); | 135 out.push_back('"'); |
86 | 136 |
87 return out; | 137 return out; |
88 } | 138 } |
89 #endif | 139 #endif |
90 | 140 |
91 // Returns true and fills in |switch_string| and |switch_value| if | 141 } // namespace |
92 // |parameter_string| represents a switch. | |
93 bool IsSwitch(const CommandLine::StringType& parameter_string, | |
94 std::string* switch_string, | |
95 CommandLine::StringType* switch_value) { | |
96 switch_string->clear(); | |
97 switch_value->clear(); | |
98 | 142 |
99 for (size_t i = 0; i < arraysize(kSwitchPrefixes); ++i) { | 143 CommandLine::CommandLine(NoProgram no_program) |
100 CommandLine::StringType prefix(kSwitchPrefixes[i]); | 144 : argv_(1), |
101 if (parameter_string.find(prefix) != 0) | 145 begin_args_(1) { |
102 continue; | |
103 | |
104 const size_t switch_start = prefix.length(); | |
105 const size_t equals_position = parameter_string.find( | |
106 kSwitchValueSeparator, switch_start); | |
107 CommandLine::StringType switch_native; | |
108 if (equals_position == CommandLine::StringType::npos) { | |
109 switch_native = parameter_string.substr(switch_start); | |
110 } else { | |
111 switch_native = parameter_string.substr( | |
112 switch_start, equals_position - switch_start); | |
113 *switch_value = parameter_string.substr(equals_position + 1); | |
114 } | |
115 #if defined(OS_WIN) | |
116 *switch_string = WideToASCII(switch_native); | |
117 Lowercase(switch_string); | |
118 #else | |
119 *switch_string = switch_native; | |
120 #endif | |
121 | |
122 return true; | |
123 } | |
124 | |
125 return false; | |
126 } | 146 } |
127 | 147 |
128 } // namespace | 148 CommandLine::CommandLine(const FilePath& program) |
129 | 149 : argv_(1), |
130 CommandLine::CommandLine(NoProgram no_program) { | 150 begin_args_(1) { |
131 #if defined(OS_POSIX) | 151 SetProgram(program); |
132 // Push an empty argument, because we always assume argv_[0] is a program. | |
133 argv_.push_back(""); | |
134 #endif | |
135 } | 152 } |
136 | 153 |
137 CommandLine::CommandLine(const FilePath& program) { | 154 CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv) |
138 #if defined(OS_WIN) | 155 : argv_(1), |
139 if (!program.empty()) { | 156 begin_args_(1) { |
140 program_ = program.value(); | |
141 // TODO(evanm): proper quoting here. | |
142 command_line_string_ = L'"' + program.value() + L'"'; | |
143 } | |
144 #elif defined(OS_POSIX) | |
145 argv_.push_back(program.value()); | |
146 #endif | |
147 } | |
148 | |
149 #if defined(OS_POSIX) | |
150 CommandLine::CommandLine(int argc, const char* const* argv) { | |
151 InitFromArgv(argc, argv); | 157 InitFromArgv(argc, argv); |
152 } | 158 } |
153 | 159 |
154 CommandLine::CommandLine(const StringVector& argv) { | 160 CommandLine::CommandLine(const StringVector& argv) |
| 161 : argv_(1), |
| 162 begin_args_(1) { |
155 InitFromArgv(argv); | 163 InitFromArgv(argv); |
156 } | 164 } |
157 #endif // OS_POSIX | |
158 | 165 |
159 CommandLine::~CommandLine() { | 166 CommandLine::~CommandLine() { |
160 } | 167 } |
161 | 168 |
162 // static | 169 // static |
163 void CommandLine::Init(int argc, const char* const* argv) { | 170 void CommandLine::Init(int argc, const char* const* argv) { |
164 delete current_process_commandline_; | 171 delete current_process_commandline_; |
165 current_process_commandline_ = new CommandLine; | 172 current_process_commandline_ = new CommandLine(NO_PROGRAM); |
166 #if defined(OS_WIN) | 173 #if defined(OS_WIN) |
167 current_process_commandline_->ParseFromString(::GetCommandLineW()); | 174 current_process_commandline_->ParseFromString(::GetCommandLineW()); |
168 #elif defined(OS_POSIX) | 175 #elif defined(OS_POSIX) |
169 current_process_commandline_->InitFromArgv(argc, argv); | 176 current_process_commandline_->InitFromArgv(argc, argv); |
170 #endif | 177 #endif |
171 } | 178 } |
172 | 179 |
173 // static | 180 // static |
174 void CommandLine::Reset() { | 181 void CommandLine::Reset() { |
175 DCHECK(current_process_commandline_); | 182 DCHECK(current_process_commandline_); |
176 delete current_process_commandline_; | 183 delete current_process_commandline_; |
177 current_process_commandline_ = NULL; | 184 current_process_commandline_ = NULL; |
178 } | 185 } |
179 | 186 |
180 // static | 187 // static |
181 CommandLine* CommandLine::ForCurrentProcess() { | 188 CommandLine* CommandLine::ForCurrentProcess() { |
182 DCHECK(current_process_commandline_); | 189 DCHECK(current_process_commandline_); |
183 return current_process_commandline_; | 190 return current_process_commandline_; |
184 } | 191 } |
185 | 192 |
186 #if defined(OS_WIN) | 193 #if defined(OS_WIN) |
187 // static | 194 // static |
188 CommandLine CommandLine::FromString(const std::wstring& command_line) { | 195 CommandLine CommandLine::FromString(const std::wstring& command_line) { |
189 CommandLine cmd; | 196 CommandLine cmd(NO_PROGRAM); |
190 cmd.ParseFromString(command_line); | 197 cmd.ParseFromString(command_line); |
191 return cmd; | 198 return cmd; |
192 } | 199 } |
193 #endif // OS_WIN | 200 #endif |
194 | 201 |
195 #if defined(OS_POSIX) | 202 void CommandLine::InitFromArgv(int argc, |
196 void CommandLine::InitFromArgv(int argc, const char* const* argv) { | 203 const CommandLine::CharType* const* argv) { |
| 204 StringVector new_argv; |
197 for (int i = 0; i < argc; ++i) | 205 for (int i = 0; i < argc; ++i) |
198 argv_.push_back(argv[i]); | 206 new_argv.push_back(argv[i]); |
199 InitFromArgv(argv_); | 207 InitFromArgv(new_argv); |
200 } | 208 } |
201 | 209 |
202 void CommandLine::InitFromArgv(const StringVector& argv) { | 210 void CommandLine::InitFromArgv(const StringVector& argv) { |
203 argv_ = argv; | 211 argv_ = StringVector(1); |
| 212 begin_args_ = 1; |
| 213 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0])); |
| 214 AppendSwitchesAndArguments(*this, argv); |
| 215 } |
| 216 |
| 217 CommandLine::StringType CommandLine::command_line_string() const { |
| 218 StringType string(argv_[0]); |
| 219 #if defined(OS_WIN) |
| 220 string = QuoteForCommandLineToArgvW(string); |
| 221 #endif |
| 222 // Append switches and arguments. |
204 bool parse_switches = true; | 223 bool parse_switches = true; |
205 for (size_t i = 1; i < argv_.size(); ++i) { | 224 for (size_t i = 1; i < argv_.size(); ++i) { |
206 const std::string& arg = argv_[i]; | 225 CommandLine::StringType arg = argv_[i]; |
207 | 226 CommandLine::StringType switch_string; |
208 if (!parse_switches) { | 227 CommandLine::StringType switch_value; |
209 args_.push_back(arg); | 228 parse_switches &= arg != kSwitchTerminator; |
210 continue; | 229 string.append(StringType(FILE_PATH_LITERAL(" "))); |
| 230 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) { |
| 231 string.append(switch_string); |
| 232 if (!switch_value.empty()) { |
| 233 #if defined(OS_WIN) |
| 234 switch_value = QuoteForCommandLineToArgvW(switch_value); |
| 235 #endif |
| 236 string.append(kSwitchValueSeparator + switch_value); |
| 237 } |
211 } | 238 } |
212 | 239 else { |
213 if (arg == kSwitchTerminator) { | 240 #if defined(OS_WIN) |
214 parse_switches = false; | 241 arg = QuoteForCommandLineToArgvW(arg); |
215 continue; | 242 #endif |
216 } | 243 string.append(arg); |
217 | |
218 std::string switch_string; | |
219 StringType switch_value; | |
220 if (IsSwitch(arg, &switch_string, &switch_value)) { | |
221 switches_[switch_string] = switch_value; | |
222 } else { | |
223 args_.push_back(arg); | |
224 } | 244 } |
225 } | 245 } |
226 } | 246 return string; |
227 #endif // OS_POSIX | |
228 | |
229 CommandLine::StringType CommandLine::command_line_string() const { | |
230 #if defined(OS_WIN) | |
231 return command_line_string_; | |
232 #elif defined(OS_POSIX) | |
233 return JoinString(argv_, ' '); | |
234 #endif | |
235 } | 247 } |
236 | 248 |
237 FilePath CommandLine::GetProgram() const { | 249 FilePath CommandLine::GetProgram() const { |
238 #if defined(OS_WIN) | |
239 return FilePath(program_); | |
240 #else | |
241 DCHECK_GT(argv_.size(), 0U); | |
242 return FilePath(argv_[0]); | 250 return FilePath(argv_[0]); |
243 #endif | 251 } |
| 252 |
| 253 void CommandLine::SetProgram(const FilePath& program) { |
| 254 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]); |
244 } | 255 } |
245 | 256 |
246 bool CommandLine::HasSwitch(const std::string& switch_string) const { | 257 bool CommandLine::HasSwitch(const std::string& switch_string) const { |
247 std::string lowercased_switch(switch_string); | 258 return switches_.find(LowerASCIIOnWindows(switch_string)) != switches_.end(); |
248 #if defined(OS_WIN) | |
249 Lowercase(&lowercased_switch); | |
250 #endif | |
251 return switches_.find(lowercased_switch) != switches_.end(); | |
252 } | 259 } |
253 | 260 |
254 std::string CommandLine::GetSwitchValueASCII( | 261 std::string CommandLine::GetSwitchValueASCII( |
255 const std::string& switch_string) const { | 262 const std::string& switch_string) const { |
256 CommandLine::StringType value = GetSwitchValueNative(switch_string); | 263 StringType value = GetSwitchValueNative(switch_string); |
257 if (!IsStringASCII(value)) { | 264 if (!IsStringASCII(value)) { |
258 LOG(WARNING) << "Value of --" << switch_string << " must be ASCII."; | 265 LOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII."; |
259 return ""; | 266 return ""; |
260 } | 267 } |
261 #if defined(OS_WIN) | 268 #if defined(OS_WIN) |
262 return WideToASCII(value); | 269 return WideToASCII(value); |
263 #else | 270 #else |
264 return value; | 271 return value; |
265 #endif | 272 #endif |
266 } | 273 } |
267 | 274 |
268 FilePath CommandLine::GetSwitchValuePath( | 275 FilePath CommandLine::GetSwitchValuePath( |
269 const std::string& switch_string) const { | 276 const std::string& switch_string) const { |
270 return FilePath(GetSwitchValueNative(switch_string)); | 277 return FilePath(GetSwitchValueNative(switch_string)); |
271 } | 278 } |
272 | 279 |
273 CommandLine::StringType CommandLine::GetSwitchValueNative( | 280 CommandLine::StringType CommandLine::GetSwitchValueNative( |
274 const std::string& switch_string) const { | 281 const std::string& switch_string) const { |
275 std::string lowercased_switch(switch_string); | 282 SwitchMap::const_iterator result = switches_.end(); |
276 #if defined(OS_WIN) | 283 result = switches_.find(LowerASCIIOnWindows(switch_string)); |
277 Lowercase(&lowercased_switch); | 284 return result == switches_.end() ? StringType() : result->second; |
278 #endif | |
279 | |
280 SwitchMap::const_iterator result = switches_.find(lowercased_switch); | |
281 | |
282 if (result == switches_.end()) { | |
283 return CommandLine::StringType(); | |
284 } else { | |
285 return result->second; | |
286 } | |
287 } | 285 } |
288 | 286 |
289 size_t CommandLine::GetSwitchCount() const { | 287 size_t CommandLine::GetSwitchCount() const { |
290 return switches_.size(); | 288 return switches_.size(); |
291 } | 289 } |
292 | 290 |
293 void CommandLine::AppendSwitch(const std::string& switch_string) { | 291 void CommandLine::AppendSwitch(const std::string& switch_string) { |
294 #if defined(OS_WIN) | 292 AppendSwitchNative(switch_string, StringType()); |
295 command_line_string_.append(L" "); | |
296 command_line_string_.append(kSwitchPrefixes[0] + ASCIIToWide(switch_string)); | |
297 switches_[switch_string] = L""; | |
298 #elif defined(OS_POSIX) | |
299 argv_.push_back(kSwitchPrefixes[0] + switch_string); | |
300 switches_[switch_string] = ""; | |
301 #endif | |
302 } | 293 } |
303 | 294 |
304 void CommandLine::AppendSwitchPath(const std::string& switch_string, | 295 void CommandLine::AppendSwitchPath(const std::string& switch_string, |
305 const FilePath& path) { | 296 const FilePath& path) { |
306 AppendSwitchNative(switch_string, path.value()); | 297 AppendSwitchNative(switch_string, path.value()); |
307 } | 298 } |
308 | 299 |
309 void CommandLine::AppendSwitchNative(const std::string& switch_string, | 300 void CommandLine::AppendSwitchNative(const std::string& switch_string, |
310 const CommandLine::StringType& value) { | 301 const CommandLine::StringType& value) { |
| 302 std::string switch_key(LowerASCIIOnWindows(switch_string)); |
311 #if defined(OS_WIN) | 303 #if defined(OS_WIN) |
312 StringType combined_switch_string = | 304 StringType combined_switch_string(ASCIIToWide(switch_key)); |
313 kSwitchPrefixes[0] + ASCIIToWide(switch_string); | |
314 if (!value.empty()) | |
315 combined_switch_string += kSwitchValueSeparator + WindowsStyleQuote(value); | |
316 | |
317 command_line_string_.append(L" "); | |
318 command_line_string_.append(combined_switch_string); | |
319 | |
320 switches_[switch_string] = value; | |
321 #elif defined(OS_POSIX) | 305 #elif defined(OS_POSIX) |
322 StringType combined_switch_string = kSwitchPrefixes[0] + switch_string; | 306 StringType combined_switch_string(switch_string); |
| 307 #endif |
| 308 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); |
| 309 switches_[switch_key.substr(prefix_length)] = value; |
| 310 // Preserve existing switch prefixes in |argv_|; only append one if necessary. |
| 311 if (prefix_length == 0) |
| 312 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string; |
323 if (!value.empty()) | 313 if (!value.empty()) |
324 combined_switch_string += kSwitchValueSeparator + value; | 314 combined_switch_string += kSwitchValueSeparator + value; |
325 argv_.push_back(combined_switch_string); | 315 // Append the switch and update the switches/arguments divider |begin_args_|. |
326 switches_[switch_string] = value; | 316 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string); |
327 #endif | |
328 } | 317 } |
329 | 318 |
330 void CommandLine::AppendSwitchASCII(const std::string& switch_string, | 319 void CommandLine::AppendSwitchASCII(const std::string& switch_string, |
331 const std::string& value_string) { | 320 const std::string& value_string) { |
332 #if defined(OS_WIN) | 321 #if defined(OS_WIN) |
333 AppendSwitchNative(switch_string, ASCIIToWide(value_string)); | 322 AppendSwitchNative(switch_string, ASCIIToWide(value_string)); |
334 #elif defined(OS_POSIX) | 323 #elif defined(OS_POSIX) |
335 AppendSwitchNative(switch_string, value_string); | 324 AppendSwitchNative(switch_string, value_string); |
336 #endif | 325 #endif |
337 } | 326 } |
338 | 327 |
339 void CommandLine::AppendSwitches(const CommandLine& other) { | |
340 SwitchMap::const_iterator i; | |
341 for (i = other.switches_.begin(); i != other.switches_.end(); ++i) | |
342 AppendSwitchNative(i->first, i->second); | |
343 } | |
344 | |
345 void CommandLine::CopySwitchesFrom(const CommandLine& source, | 328 void CommandLine::CopySwitchesFrom(const CommandLine& source, |
346 const char* const switches[], | 329 const char* const switches[], |
347 size_t count) { | 330 size_t count) { |
348 for (size_t i = 0; i < count; ++i) { | 331 for (size_t i = 0; i < count; ++i) { |
349 if (source.HasSwitch(switches[i])) { | 332 if (source.HasSwitch(switches[i])) |
350 StringType value = source.GetSwitchValueNative(switches[i]); | 333 AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i])); |
351 AppendSwitchNative(switches[i], value); | |
352 } | |
353 } | 334 } |
354 } | 335 } |
355 | 336 |
| 337 CommandLine::StringVector CommandLine::args() const { |
| 338 // Gather all arguments after the last switch (may include kSwitchTerminator). |
| 339 StringVector args(argv_.begin() + begin_args_, argv_.end()); |
| 340 // Erase only the first kSwitchTerminator (maybe "--" is a legitimate page?) |
| 341 StringVector::iterator switch_terminator = |
| 342 std::find(args.begin(), args.end(), kSwitchTerminator); |
| 343 if (switch_terminator != args.end()) |
| 344 args.erase(switch_terminator); |
| 345 return args; |
| 346 } |
| 347 |
356 void CommandLine::AppendArg(const std::string& value) { | 348 void CommandLine::AppendArg(const std::string& value) { |
357 #if defined(OS_WIN) | 349 #if defined(OS_WIN) |
358 DCHECK(IsStringUTF8(value)); | 350 DCHECK(IsStringUTF8(value)); |
359 AppendArgNative(UTF8ToWide(value)); | 351 AppendArgNative(UTF8ToWide(value)); |
360 #elif defined(OS_POSIX) | 352 #elif defined(OS_POSIX) |
361 AppendArgNative(value); | 353 AppendArgNative(value); |
362 #endif | 354 #endif |
363 } | 355 } |
364 | 356 |
365 void CommandLine::AppendArgPath(const FilePath& path) { | 357 void CommandLine::AppendArgPath(const FilePath& path) { |
366 AppendArgNative(path.value()); | 358 AppendArgNative(path.value()); |
367 } | 359 } |
368 | 360 |
369 void CommandLine::AppendArgNative(const CommandLine::StringType& value) { | 361 void CommandLine::AppendArgNative(const CommandLine::StringType& value) { |
370 #if defined(OS_WIN) | |
371 command_line_string_.append(L" "); | |
372 command_line_string_.append(WindowsStyleQuote(value)); | |
373 args_.push_back(value); | |
374 #elif defined(OS_POSIX) | |
375 DCHECK(IsStringUTF8(value)); | |
376 argv_.push_back(value); | 362 argv_.push_back(value); |
377 #endif | |
378 } | |
379 | |
380 void CommandLine::AppendArgs(const CommandLine& other) { | |
381 if(other.args_.size() <= 0) | |
382 return; | |
383 #if defined(OS_WIN) | |
384 command_line_string_.append(L" --"); | |
385 #endif // OS_WIN | |
386 StringVector::const_iterator i; | |
387 for (i = other.args_.begin(); i != other.args_.end(); ++i) | |
388 AppendArgNative(*i); | |
389 } | 363 } |
390 | 364 |
391 void CommandLine::AppendArguments(const CommandLine& other, | 365 void CommandLine::AppendArguments(const CommandLine& other, |
392 bool include_program) { | 366 bool include_program) { |
393 #if defined(OS_WIN) | |
394 // Verify include_program is used correctly. | |
395 DCHECK(!include_program || !other.GetProgram().empty()); | |
396 if (include_program) | 367 if (include_program) |
397 program_ = other.program_; | 368 SetProgram(other.GetProgram()); |
398 | 369 AppendSwitchesAndArguments(*this, other.argv()); |
399 if (!command_line_string_.empty()) | |
400 command_line_string_ += L' '; | |
401 | |
402 command_line_string_ += other.command_line_string_; | |
403 #elif defined(OS_POSIX) | |
404 // Verify include_program is used correctly. | |
405 // Logic could be shorter but this is clearer. | |
406 DCHECK_EQ(include_program, !other.GetProgram().empty()); | |
407 | |
408 if (include_program) | |
409 argv_[0] = other.argv_[0]; | |
410 | |
411 // Skip the first arg when copying since it's the program but push all | |
412 // arguments to our arg vector. | |
413 for (size_t i = 1; i < other.argv_.size(); ++i) | |
414 argv_.push_back(other.argv_[i]); | |
415 #endif | |
416 | |
417 SwitchMap::const_iterator i; | |
418 for (i = other.switches_.begin(); i != other.switches_.end(); ++i) | |
419 switches_[i->first] = i->second; | |
420 } | 370 } |
421 | 371 |
422 void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) { | 372 void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) { |
| 373 if (wrapper.empty()) |
| 374 return; |
423 // The wrapper may have embedded arguments (like "gdb --args"). In this case, | 375 // The wrapper may have embedded arguments (like "gdb --args"). In this case, |
424 // we don't pretend to do anything fancy, we just split on spaces. | 376 // we don't pretend to do anything fancy, we just split on spaces. |
425 if (wrapper.empty()) | 377 StringVector wrapper_argv; |
426 return; | 378 base::SplitString(wrapper, FILE_PATH_LITERAL(' '), &wrapper_argv); |
427 StringVector wrapper_and_args; | 379 // Prepend the wrapper and update the switches/arguments |begin_args_|. |
428 #if defined(OS_WIN) | 380 argv_.insert(argv_.begin(), wrapper_argv.begin(), wrapper_argv.end()); |
429 base::SplitString(wrapper, ' ', &wrapper_and_args); | 381 begin_args_ += wrapper_argv.size(); |
430 program_ = wrapper_and_args[0]; | |
431 command_line_string_ = wrapper + L" " + command_line_string_; | |
432 #elif defined(OS_POSIX) | |
433 base::SplitString(wrapper, ' ', &wrapper_and_args); | |
434 argv_.insert(argv_.begin(), wrapper_and_args.begin(), wrapper_and_args.end()); | |
435 #endif | |
436 } | 382 } |
437 | 383 |
438 #if defined(OS_WIN) | 384 #if defined(OS_WIN) |
439 void CommandLine::ParseFromString(const std::wstring& command_line) { | 385 void CommandLine::ParseFromString(const std::wstring& command_line) { |
440 TrimWhitespace(command_line, TRIM_ALL, &command_line_string_); | 386 std::wstring command_line_string; |
441 | 387 TrimWhitespace(command_line, TRIM_ALL, &command_line_string); |
442 if (command_line_string_.empty()) | 388 if (command_line_string.empty()) |
443 return; | 389 return; |
444 | 390 |
445 int num_args = 0; | 391 int num_args = 0; |
446 wchar_t** args = NULL; | 392 wchar_t** args = NULL; |
| 393 args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args); |
447 | 394 |
448 args = CommandLineToArgvW(command_line_string_.c_str(), &num_args); | 395 PLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: " << |
449 | 396 command_line; |
450 // Populate program_ with the trimmed version of the first arg. | 397 InitFromArgv(num_args, args); |
451 TrimWhitespace(args[0], TRIM_ALL, &program_); | 398 LocalFree(args); |
452 | |
453 bool parse_switches = true; | |
454 for (int i = 1; i < num_args; ++i) { | |
455 std::wstring arg; | |
456 TrimWhitespace(args[i], TRIM_ALL, &arg); | |
457 | |
458 if (!parse_switches) { | |
459 args_.push_back(arg); | |
460 continue; | |
461 } | |
462 | |
463 if (arg == kSwitchTerminator) { | |
464 parse_switches = false; | |
465 continue; | |
466 } | |
467 | |
468 std::string switch_string; | |
469 std::wstring switch_value; | |
470 if (IsSwitch(arg, &switch_string, &switch_value)) { | |
471 switches_[switch_string] = switch_value; | |
472 } else { | |
473 args_.push_back(arg); | |
474 } | |
475 } | |
476 | |
477 if (args) | |
478 LocalFree(args); | |
479 } | 399 } |
480 #endif | 400 #endif |
481 | |
482 CommandLine::CommandLine() { | |
483 } | |
OLD | NEW |