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 #elif defined(OS_FREEBSD) | 10 #elif defined(OS_FREEBSD) |
(...skipping 27 matching lines...) Expand all Loading... |
38 // Unixes don't use slash as a switch. | 38 // Unixes don't use slash as a switch. |
39 const char* const kSwitchPrefixes[] = {"--", "-"}; | 39 const char* const kSwitchPrefixes[] = {"--", "-"}; |
40 const char kSwitchTerminator[] = "--"; | 40 const char kSwitchTerminator[] = "--"; |
41 const char kSwitchValueSeparator[] = "="; | 41 const char kSwitchValueSeparator[] = "="; |
42 #endif | 42 #endif |
43 | 43 |
44 #if defined(OS_WIN) | 44 #if defined(OS_WIN) |
45 // Lowercase a string. This is used to lowercase switch names. | 45 // Lowercase a string. This is used to lowercase switch names. |
46 // Is this what we really want? It seems crazy to me. I've left it in | 46 // Is this what we really want? It seems crazy to me. I've left it in |
47 // for backwards compatibility on Windows. | 47 // for backwards compatibility on Windows. |
48 static void Lowercase(std::wstring* parameter) { | 48 static void Lowercase(std::string* parameter) { |
49 transform(parameter->begin(), parameter->end(), parameter->begin(), | 49 transform(parameter->begin(), parameter->end(), parameter->begin(), |
50 tolower); | 50 tolower); |
51 } | 51 } |
52 #endif | 52 #endif |
53 | 53 |
54 #if defined(OS_WIN) | 54 #if defined(OS_WIN) |
55 void CommandLine::ParseFromString(const std::wstring& command_line) { | 55 void CommandLine::ParseFromString(const std::wstring& command_line) { |
56 TrimWhitespace(command_line, TRIM_ALL, &command_line_string_); | 56 TrimWhitespace(command_line, TRIM_ALL, &command_line_string_); |
57 | 57 |
58 if (command_line_string_.empty()) | 58 if (command_line_string_.empty()) |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 kSwitchValueSeparator, switch_start); | 168 kSwitchValueSeparator, switch_start); |
169 StringType switch_native; | 169 StringType switch_native; |
170 if (equals_position == StringType::npos) { | 170 if (equals_position == StringType::npos) { |
171 switch_native = parameter_string.substr(switch_start); | 171 switch_native = parameter_string.substr(switch_start); |
172 } else { | 172 } else { |
173 switch_native = parameter_string.substr( | 173 switch_native = parameter_string.substr( |
174 switch_start, equals_position - switch_start); | 174 switch_start, equals_position - switch_start); |
175 *switch_value = parameter_string.substr(equals_position + 1); | 175 *switch_value = parameter_string.substr(equals_position + 1); |
176 } | 176 } |
177 #if defined(OS_WIN) | 177 #if defined(OS_WIN) |
178 Lowercase(&switch_native); | |
179 *switch_string = WideToASCII(switch_native); | 178 *switch_string = WideToASCII(switch_native); |
| 179 Lowercase(switch_string); |
180 #else | 180 #else |
181 *switch_string = switch_native; | 181 *switch_string = switch_native; |
182 #endif | 182 #endif |
183 | 183 |
184 return true; | 184 return true; |
185 } | 185 } |
186 | 186 |
187 return false; | 187 return false; |
188 } | 188 } |
189 | 189 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 setproctitle("%s", title.c_str()); | 224 setproctitle("%s", title.c_str()); |
225 } | 225 } |
226 #endif | 226 #endif |
227 | 227 |
228 void CommandLine::Terminate() { | 228 void CommandLine::Terminate() { |
229 DCHECK(current_process_commandline_ != NULL); | 229 DCHECK(current_process_commandline_ != NULL); |
230 delete current_process_commandline_; | 230 delete current_process_commandline_; |
231 current_process_commandline_ = NULL; | 231 current_process_commandline_ = NULL; |
232 } | 232 } |
233 | 233 |
234 bool CommandLine::HasSwitch(const std::wstring& switch_string) const { | 234 bool CommandLine::HasSwitch(const std::string& switch_string) const { |
235 std::wstring lowercased_switch(switch_string); | 235 std::string lowercased_switch(switch_string); |
236 #if defined(OS_WIN) | 236 #if defined(OS_WIN) |
237 Lowercase(&lowercased_switch); | 237 Lowercase(&lowercased_switch); |
238 #endif | 238 #endif |
239 return switches_.find(WideToASCII(lowercased_switch)) != switches_.end(); | 239 return switches_.find(lowercased_switch) != switches_.end(); |
240 } | 240 } |
241 | 241 |
242 std::wstring CommandLine::GetSwitchValue( | 242 std::wstring CommandLine::GetSwitchValue( |
243 const std::wstring& switch_string) const { | 243 const std::string& switch_string) const { |
244 std::wstring lowercased_switch(switch_string); | 244 std::string lowercased_switch(switch_string); |
245 #if defined(OS_WIN) | 245 #if defined(OS_WIN) |
246 Lowercase(&lowercased_switch); | 246 Lowercase(&lowercased_switch); |
247 #endif | 247 #endif |
248 | 248 |
249 std::map<std::string, StringType>::const_iterator result = | 249 std::map<std::string, StringType>::const_iterator result = |
250 switches_.find(WideToASCII(lowercased_switch)); | 250 switches_.find(lowercased_switch); |
251 | 251 |
252 if (result == switches_.end()) { | 252 if (result == switches_.end()) { |
253 return L""; | 253 return L""; |
254 } else { | 254 } else { |
255 #if defined(OS_WIN) | 255 #if defined(OS_WIN) |
256 return result->second; | 256 return result->second; |
257 #else | 257 #else |
258 return base::SysNativeMBToWide(result->second); | 258 return base::SysNativeMBToWide(result->second); |
259 #endif | 259 #endif |
260 } | 260 } |
(...skipping 15 matching lines...) Expand all Loading... |
276 } | 276 } |
277 std::wstring CommandLine::program() const { | 277 std::wstring CommandLine::program() const { |
278 DCHECK_GT(argv_.size(), 0U); | 278 DCHECK_GT(argv_.size(), 0U); |
279 return base::SysNativeMBToWide(argv_[0]); | 279 return base::SysNativeMBToWide(argv_[0]); |
280 } | 280 } |
281 #endif | 281 #endif |
282 | 282 |
283 | 283 |
284 // static | 284 // static |
285 std::wstring CommandLine::PrefixedSwitchString( | 285 std::wstring CommandLine::PrefixedSwitchString( |
286 const std::wstring& switch_string) { | 286 const std::string& switch_string) { |
287 #if defined(OS_WIN) | 287 #if defined(OS_WIN) |
288 return kSwitchPrefixes[0] + switch_string; | 288 return kSwitchPrefixes[0] + ASCIIToWide(switch_string); |
289 #else | 289 #else |
290 return ASCIIToWide(kSwitchPrefixes[0]) + switch_string; | 290 return ASCIIToWide(kSwitchPrefixes[0] + switch_string); |
291 #endif | 291 #endif |
292 } | 292 } |
293 | 293 |
294 // static | 294 // static |
295 std::wstring CommandLine::PrefixedSwitchStringWithValue( | 295 std::wstring CommandLine::PrefixedSwitchStringWithValue( |
296 const std::wstring& switch_string, const std::wstring& value_string) { | 296 const std::string& switch_string, const std::wstring& value_string) { |
297 if (value_string.empty()) { | 297 if (value_string.empty()) { |
298 return PrefixedSwitchString(switch_string); | 298 return PrefixedSwitchString(switch_string); |
299 } | 299 } |
300 | 300 |
301 return PrefixedSwitchString(switch_string + | 301 return PrefixedSwitchString(switch_string + |
302 #if defined(OS_WIN) | 302 #if defined(OS_WIN) |
303 kSwitchValueSeparator + | 303 WideToASCII(kSwitchValueSeparator) |
304 #else | 304 #else |
305 ASCIIToWide(kSwitchValueSeparator) + | 305 kSwitchValueSeparator |
306 #endif | 306 #endif |
307 value_string); | 307 ) + value_string; |
308 } | 308 } |
309 | 309 |
310 #if defined(OS_WIN) | 310 #if defined(OS_WIN) |
311 void CommandLine::AppendSwitch(const std::wstring& switch_string) { | 311 void CommandLine::AppendSwitch(const std::string& switch_string) { |
312 std::wstring prefixed_switch_string = PrefixedSwitchString(switch_string); | 312 std::wstring prefixed_switch_string = PrefixedSwitchString(switch_string); |
313 command_line_string_.append(L" "); | 313 command_line_string_.append(L" "); |
314 command_line_string_.append(prefixed_switch_string); | 314 command_line_string_.append(prefixed_switch_string); |
315 switches_[WideToASCII(switch_string)] = L""; | 315 switches_[switch_string] = L""; |
316 } | 316 } |
317 | 317 |
318 void CommandLine::AppendSwitchWithValue(const std::wstring& switch_string, | 318 void CommandLine::AppendSwitchWithValue(const std::string& switch_string, |
319 const std::wstring& value_string) { | 319 const std::wstring& value_string) { |
320 std::wstring value_string_edit; | 320 std::wstring value_string_edit; |
321 | 321 |
322 // NOTE(jhughes): If the value contains a quotation mark at one | 322 // NOTE(jhughes): If the value contains a quotation mark at one |
323 // end but not both, you may get unusable output. | 323 // end but not both, you may get unusable output. |
324 if (!value_string.empty() && | 324 if (!value_string.empty() && |
325 (value_string.find(L" ") != std::wstring::npos) && | 325 (value_string.find(L" ") != std::wstring::npos) && |
326 (value_string[0] != L'"') && | 326 (value_string[0] != L'"') && |
327 (value_string[value_string.length() - 1] != L'"')) { | 327 (value_string[value_string.length() - 1] != L'"')) { |
328 // need to provide quotes | 328 // need to provide quotes |
329 value_string_edit = StringPrintf(L"\"%ls\"", value_string.c_str()); | 329 value_string_edit = StringPrintf(L"\"%ls\"", value_string.c_str()); |
330 } else { | 330 } else { |
331 value_string_edit = value_string; | 331 value_string_edit = value_string; |
332 } | 332 } |
333 | 333 |
334 std::wstring combined_switch_string = | 334 std::wstring combined_switch_string = |
335 PrefixedSwitchStringWithValue(switch_string, value_string_edit); | 335 PrefixedSwitchStringWithValue(switch_string, value_string_edit); |
336 | 336 |
337 command_line_string_.append(L" "); | 337 command_line_string_.append(L" "); |
338 command_line_string_.append(combined_switch_string); | 338 command_line_string_.append(combined_switch_string); |
339 | 339 |
340 switches_[WideToASCII(switch_string)] = value_string; | 340 switches_[switch_string] = value_string; |
341 } | 341 } |
342 | 342 |
343 void CommandLine::AppendLooseValue(const std::wstring& value) { | 343 void CommandLine::AppendLooseValue(const std::wstring& value) { |
344 // TODO(evan): quoting? | 344 // TODO(evan): quoting? |
345 command_line_string_.append(L" "); | 345 command_line_string_.append(L" "); |
346 command_line_string_.append(value); | 346 command_line_string_.append(value); |
347 } | 347 } |
348 | 348 |
349 void CommandLine::AppendArguments(const CommandLine& other, | 349 void CommandLine::AppendArguments(const CommandLine& other, |
350 bool include_program) { | 350 bool include_program) { |
351 // Verify include_program is used correctly. | 351 // Verify include_program is used correctly. |
352 // Logic could be shorter but this is clearer. | 352 // Logic could be shorter but this is clearer. |
353 DCHECK(include_program ? !other.program().empty() : other.program().empty()); | 353 DCHECK(include_program ? !other.program().empty() : other.program().empty()); |
354 command_line_string_ += L" " + other.command_line_string_; | 354 command_line_string_ += L" " + other.command_line_string_; |
355 std::map<std::string, StringType>::const_iterator i; | 355 std::map<std::string, StringType>::const_iterator i; |
356 for (i = other.switches_.begin(); i != other.switches_.end(); ++i) | 356 for (i = other.switches_.begin(); i != other.switches_.end(); ++i) |
357 switches_[i->first] = i->second; | 357 switches_[i->first] = i->second; |
358 } | 358 } |
359 | 359 |
360 void CommandLine::PrependWrapper(const std::wstring& wrapper) { | 360 void CommandLine::PrependWrapper(const std::wstring& wrapper) { |
361 // The wrapper may have embedded arguments (like "gdb --args"). In this case, | 361 // The wrapper may have embedded arguments (like "gdb --args"). In this case, |
362 // we don't pretend to do anything fancy, we just split on spaces. | 362 // we don't pretend to do anything fancy, we just split on spaces. |
363 std::vector<std::wstring> wrapper_and_args; | 363 std::vector<std::wstring> wrapper_and_args; |
364 SplitString(wrapper, ' ', &wrapper_and_args); | 364 SplitString(wrapper, ' ', &wrapper_and_args); |
365 program_ = wrapper_and_args[0]; | 365 program_ = wrapper_and_args[0]; |
366 command_line_string_ = wrapper + L" " + command_line_string_; | 366 command_line_string_ = wrapper + L" " + command_line_string_; |
367 } | 367 } |
368 | 368 |
369 #elif defined(OS_POSIX) | 369 #elif defined(OS_POSIX) |
370 void CommandLine::AppendSwitch(const std::wstring& switch_string) { | 370 void CommandLine::AppendSwitch(const std::string& switch_string) { |
371 std::string ascii_switch = WideToASCII(switch_string); | 371 argv_.push_back(kSwitchPrefixes[0] + switch_string); |
372 argv_.push_back(kSwitchPrefixes[0] + ascii_switch); | 372 switches_[switch_string] = ""; |
373 switches_[ascii_switch] = ""; | |
374 } | 373 } |
375 | 374 |
376 void CommandLine::AppendSwitchWithValue(const std::wstring& switch_string, | 375 void CommandLine::AppendSwitchWithValue(const std::string& switch_string, |
377 const std::wstring& value_string) { | 376 const std::wstring& value_string) { |
378 std::string ascii_switch = WideToASCII(switch_string); | |
379 std::string mb_value = base::SysWideToNativeMB(value_string); | 377 std::string mb_value = base::SysWideToNativeMB(value_string); |
380 | 378 |
381 argv_.push_back(kSwitchPrefixes[0] + ascii_switch + | 379 argv_.push_back(kSwitchPrefixes[0] + switch_string + |
382 kSwitchValueSeparator + mb_value); | 380 kSwitchValueSeparator + mb_value); |
383 switches_[ascii_switch] = mb_value; | 381 switches_[switch_string] = mb_value; |
384 } | 382 } |
385 | 383 |
386 void CommandLine::AppendLooseValue(const std::wstring& value) { | 384 void CommandLine::AppendLooseValue(const std::wstring& value) { |
387 argv_.push_back(base::SysWideToNativeMB(value)); | 385 argv_.push_back(base::SysWideToNativeMB(value)); |
388 } | 386 } |
389 | 387 |
390 void CommandLine::AppendArguments(const CommandLine& other, | 388 void CommandLine::AppendArguments(const CommandLine& other, |
391 bool include_program) { | 389 bool include_program) { |
392 // Verify include_program is used correctly. | 390 // Verify include_program is used correctly. |
393 // Logic could be shorter but this is clearer. | 391 // Logic could be shorter but this is clearer. |
(...skipping 10 matching lines...) Expand all Loading... |
404 void CommandLine::PrependWrapper(const std::wstring& wrapper_wide) { | 402 void CommandLine::PrependWrapper(const std::wstring& wrapper_wide) { |
405 // The wrapper may have embedded arguments (like "gdb --args"). In this case, | 403 // The wrapper may have embedded arguments (like "gdb --args"). In this case, |
406 // we don't pretend to do anything fancy, we just split on spaces. | 404 // we don't pretend to do anything fancy, we just split on spaces. |
407 const std::string wrapper = base::SysWideToNativeMB(wrapper_wide); | 405 const std::string wrapper = base::SysWideToNativeMB(wrapper_wide); |
408 std::vector<std::string> wrapper_and_args; | 406 std::vector<std::string> wrapper_and_args; |
409 SplitString(wrapper, ' ', &wrapper_and_args); | 407 SplitString(wrapper, ' ', &wrapper_and_args); |
410 argv_.insert(argv_.begin(), wrapper_and_args.begin(), wrapper_and_args.end()); | 408 argv_.insert(argv_.begin(), wrapper_and_args.begin(), wrapper_and_args.end()); |
411 } | 409 } |
412 | 410 |
413 #endif | 411 #endif |
OLD | NEW |