blob: 1c738460fda1f89f29a2002eeebc47ecf61e23f4 [file] [log] [blame]
[email protected]72e2e2422012-02-27 18:38:121// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]f3adb5c2008-08-07 20:07:325#include "base/command_line.h"
6
initial.commitd7cae122008-07-26 21:49:387#include <algorithm>
[email protected]2edc2862011-04-04 18:04:378#include <ostream>
initial.commitd7cae122008-07-26 21:49:389
[email protected]2edc2862011-04-04 18:04:3710#include "base/basictypes.h"
[email protected]8f681e42009-10-09 20:37:5611#include "base/file_path.h"
initial.commitd7cae122008-07-26 21:49:3812#include "base/logging.h"
initial.commitd7cae122008-07-26 21:49:3813#include "base/string_util.h"
[email protected]5ae0b763e2013-02-07 23:01:3914#include "base/strings/string_split.h"
[email protected]f1d81922010-07-31 17:47:0915#include "base/utf_string_conversions.h"
[email protected]74e9fa22010-12-29 21:06:4316#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3817
[email protected]74e9fa22010-12-29 21:06:4318#if defined(OS_WIN)
19#include <windows.h>
20#include <shellapi.h>
[email protected]7f113f32009-09-10 18:02:1721#endif
22
[email protected]04af979a2013-02-16 04:12:2623using base::FilePath;
24
[email protected]bb975362009-01-21 01:00:2225CommandLine* CommandLine::current_process_commandline_ = NULL;
initial.commitd7cae122008-07-26 21:49:3826
[email protected]06cc083a2011-03-01 02:28:4227namespace {
[email protected]a40ca4302011-05-14 01:10:2428const CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--");
29const CommandLine::CharType kSwitchValueSeparator[] = FILE_PATH_LITERAL("=");
[email protected]06cc083a2011-03-01 02:28:4230// Since we use a lazy match, make sure that longer versions (like "--") are
31// listed before shorter versions (like "-") of similar prefixes.
[email protected]5d426332008-08-08 20:46:2132#if defined(OS_WIN)
[email protected]a40ca4302011-05-14 01:10:2433const CommandLine::CharType* const kSwitchPrefixes[] = {L"--", L"-", L"/"};
[email protected]5d426332008-08-08 20:46:2134#elif defined(OS_POSIX)
[email protected]1a48f312008-08-12 01:14:3735// Unixes don't use slash as a switch.
[email protected]a40ca4302011-05-14 01:10:2436const CommandLine::CharType* const kSwitchPrefixes[] = {"--", "-"};
[email protected]5d426332008-08-08 20:46:2137#endif
initial.commitd7cae122008-07-26 21:49:3838
[email protected]a40ca4302011-05-14 01:10:2439size_t GetSwitchPrefixLength(const CommandLine::StringType& string) {
40 for (size_t i = 0; i < arraysize(kSwitchPrefixes); ++i) {
41 CommandLine::StringType prefix(kSwitchPrefixes[i]);
[email protected]1fa39f02011-09-13 15:45:3442 if (string.compare(0, prefix.length(), prefix) == 0)
[email protected]a40ca4302011-05-14 01:10:2443 return prefix.length();
44 }
45 return 0;
initial.commitd7cae122008-07-26 21:49:3846}
[email protected]0fd23af2011-02-20 06:33:0447
[email protected]a40ca4302011-05-14 01:10:2448// 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|.
50bool IsSwitch(const CommandLine::StringType& string,
51 CommandLine::StringType* switch_string,
52 CommandLine::StringType* switch_value) {
53 switch_string->clear();
54 switch_value->clear();
[email protected]21e342f2012-10-19 06:19:5955 size_t prefix_length = GetSwitchPrefixLength(string);
56 if (prefix_length == 0 || prefix_length == string.length())
[email protected]a40ca4302011-05-14 01:10:2457 return false;
58
59 const size_t equals_position = string.find(kSwitchValueSeparator);
60 *switch_string = string.substr(0, equals_position);
61 if (equals_position != CommandLine::StringType::npos)
62 *switch_value = string.substr(equals_position + 1);
63 return true;
64}
65
66// Append switches and arguments, keeping switches before arguments.
67void AppendSwitchesAndArguments(CommandLine& command_line,
68 const CommandLine::StringVector& argv) {
69 bool parse_switches = true;
70 for (size_t i = 1; i < argv.size(); ++i) {
71 CommandLine::StringType arg = argv[i];
72 TrimWhitespace(arg, TRIM_ALL, &arg);
73
74 CommandLine::StringType switch_string;
75 CommandLine::StringType switch_value;
76 parse_switches &= (arg != kSwitchTerminator);
77 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) {
78#if defined(OS_WIN)
79 command_line.AppendSwitchNative(WideToASCII(switch_string), switch_value);
80#elif defined(OS_POSIX)
81 command_line.AppendSwitchNative(switch_string, switch_value);
82#endif
83 } else {
84 command_line.AppendArgNative(arg);
85 }
86 }
87}
88
89// Lowercase switches for backwards compatiblity *on Windows*.
90std::string LowerASCIIOnWindows(const std::string& string) {
91#if defined(OS_WIN)
92 return StringToLowerASCII(string);
93#elif defined(OS_POSIX)
94 return string;
95#endif
96}
97
98
99#if defined(OS_WIN)
100// Quote a string as necessary for CommandLineToArgvW compatiblity *on Windows*.
101std::wstring QuoteForCommandLineToArgvW(const std::wstring& arg) {
[email protected]0fd23af2011-02-20 06:33:04102 // We follow the quoting rules of CommandLineToArgvW.
103 // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
104 if (arg.find_first_of(L" \\\"") == std::wstring::npos) {
105 // No quoting necessary.
106 return arg;
107 }
108
109 std::wstring out;
110 out.push_back(L'"');
111 for (size_t i = 0; i < arg.size(); ++i) {
112 if (arg[i] == '\\') {
113 // Find the extent of this run of backslashes.
114 size_t start = i, end = start + 1;
115 for (; end < arg.size() && arg[end] == '\\'; ++end)
116 /* empty */;
117 size_t backslash_count = end - start;
118
119 // Backslashes are escapes only if the run is followed by a double quote.
120 // Since we also will end the string with a double quote, we escape for
121 // either a double quote or the end of the string.
122 if (end == arg.size() || arg[end] == '"') {
123 // To quote, we need to output 2x as many backslashes.
124 backslash_count *= 2;
125 }
126 for (size_t j = 0; j < backslash_count; ++j)
127 out.push_back('\\');
128
129 // Advance i to one before the end to balance i++ in loop.
130 i = end - 1;
131 } else if (arg[i] == '"') {
132 out.push_back('\\');
133 out.push_back('"');
134 } else {
135 out.push_back(arg[i]);
136 }
137 }
138 out.push_back('"');
139
140 return out;
141}
[email protected]bb975362009-01-21 01:00:22142#endif
initial.commitd7cae122008-07-26 21:49:38143
[email protected]06cc083a2011-03-01 02:28:42144} // namespace
145
[email protected]a40ca4302011-05-14 01:10:24146CommandLine::CommandLine(NoProgram no_program)
147 : argv_(1),
148 begin_args_(1) {
[email protected]06cc083a2011-03-01 02:28:42149}
150
[email protected]a40ca4302011-05-14 01:10:24151CommandLine::CommandLine(const FilePath& program)
152 : argv_(1),
153 begin_args_(1) {
154 SetProgram(program);
[email protected]06cc083a2011-03-01 02:28:42155}
156
[email protected]a40ca4302011-05-14 01:10:24157CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv)
158 : argv_(1),
159 begin_args_(1) {
[email protected]06cc083a2011-03-01 02:28:42160 InitFromArgv(argc, argv);
161}
162
[email protected]a40ca4302011-05-14 01:10:24163CommandLine::CommandLine(const StringVector& argv)
164 : argv_(1),
165 begin_args_(1) {
[email protected]06cc083a2011-03-01 02:28:42166 InitFromArgv(argv);
167}
[email protected]06cc083a2011-03-01 02:28:42168
[email protected]acbeb3d2011-03-01 20:47:58169CommandLine::~CommandLine() {
170}
171
[email protected]06cc083a2011-03-01 02:28:42172// static
[email protected]72e2e2422012-02-27 18:38:12173bool CommandLine::Init(int argc, const char* const* argv) {
[email protected]f96fe2c42011-07-13 18:03:34174 if (current_process_commandline_) {
175 // 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
177 // shared libraries.
[email protected]72e2e2422012-02-27 18:38:12178 return false;
[email protected]f96fe2c42011-07-13 18:03:34179 }
180
[email protected]a40ca4302011-05-14 01:10:24181 current_process_commandline_ = new CommandLine(NO_PROGRAM);
[email protected]06cc083a2011-03-01 02:28:42182#if defined(OS_WIN)
183 current_process_commandline_->ParseFromString(::GetCommandLineW());
184#elif defined(OS_POSIX)
185 current_process_commandline_->InitFromArgv(argc, argv);
186#endif
[email protected]72e2e2422012-02-27 18:38:12187
188 return true;
[email protected]06cc083a2011-03-01 02:28:42189}
190
191// static
192void CommandLine::Reset() {
193 DCHECK(current_process_commandline_);
194 delete current_process_commandline_;
195 current_process_commandline_ = NULL;
196}
197
198// static
199CommandLine* CommandLine::ForCurrentProcess() {
200 DCHECK(current_process_commandline_);
201 return current_process_commandline_;
[email protected]3a3d47472010-07-15 21:03:54202}
203
[email protected]f3adb5c2008-08-07 20:07:32204#if defined(OS_WIN)
[email protected]06cc083a2011-03-01 02:28:42205// static
206CommandLine CommandLine::FromString(const std::wstring& command_line) {
[email protected]a40ca4302011-05-14 01:10:24207 CommandLine cmd(NO_PROGRAM);
[email protected]06cc083a2011-03-01 02:28:42208 cmd.ParseFromString(command_line);
209 return cmd;
210}
[email protected]a40ca4302011-05-14 01:10:24211#endif
[email protected]06cc083a2011-03-01 02:28:42212
[email protected]a40ca4302011-05-14 01:10:24213void CommandLine::InitFromArgv(int argc,
214 const CommandLine::CharType* const* argv) {
215 StringVector new_argv;
[email protected]06cc083a2011-03-01 02:28:42216 for (int i = 0; i < argc; ++i)
[email protected]a40ca4302011-05-14 01:10:24217 new_argv.push_back(argv[i]);
218 InitFromArgv(new_argv);
[email protected]51343d5a2009-10-26 22:39:33219}
220
[email protected]06cc083a2011-03-01 02:28:42221void CommandLine::InitFromArgv(const StringVector& argv) {
[email protected]a40ca4302011-05-14 01:10:24222 argv_ = StringVector(1);
223 begin_args_ = 1;
224 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0]));
225 AppendSwitchesAndArguments(*this, argv);
[email protected]06cc083a2011-03-01 02:28:42226}
[email protected]06cc083a2011-03-01 02:28:42227
[email protected]61a4c6f2011-07-20 04:54:52228CommandLine::StringType CommandLine::GetCommandLineString() const {
[email protected]a40ca4302011-05-14 01:10:24229 StringType string(argv_[0]);
[email protected]06cc083a2011-03-01 02:28:42230#if defined(OS_WIN)
[email protected]a40ca4302011-05-14 01:10:24231 string = QuoteForCommandLineToArgvW(string);
[email protected]06cc083a2011-03-01 02:28:42232#endif
[email protected]45f982e2012-10-29 21:31:31233 StringType params(GetArgumentsString());
234 if (!params.empty()) {
235 string.append(StringType(FILE_PATH_LITERAL(" ")));
236 string.append(params);
237 }
238 return string;
239}
240
241CommandLine::StringType CommandLine::GetArgumentsString() const {
242 StringType params;
[email protected]a40ca4302011-05-14 01:10:24243 // Append switches and arguments.
244 bool parse_switches = true;
245 for (size_t i = 1; i < argv_.size(); ++i) {
[email protected]45f982e2012-10-29 21:31:31246 StringType arg = argv_[i];
247 StringType switch_string;
248 StringType switch_value;
[email protected]a40ca4302011-05-14 01:10:24249 parse_switches &= arg != kSwitchTerminator;
[email protected]45f982e2012-10-29 21:31:31250 if (i > 1)
251 params.append(StringType(FILE_PATH_LITERAL(" ")));
[email protected]a40ca4302011-05-14 01:10:24252 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) {
[email protected]45f982e2012-10-29 21:31:31253 params.append(switch_string);
[email protected]a40ca4302011-05-14 01:10:24254 if (!switch_value.empty()) {
255#if defined(OS_WIN)
256 switch_value = QuoteForCommandLineToArgvW(switch_value);
257#endif
[email protected]45f982e2012-10-29 21:31:31258 params.append(kSwitchValueSeparator + switch_value);
[email protected]a40ca4302011-05-14 01:10:24259 }
260 }
261 else {
262#if defined(OS_WIN)
263 arg = QuoteForCommandLineToArgvW(arg);
264#endif
[email protected]45f982e2012-10-29 21:31:31265 params.append(arg);
[email protected]a40ca4302011-05-14 01:10:24266 }
267 }
[email protected]45f982e2012-10-29 21:31:31268 return params;
[email protected]06cc083a2011-03-01 02:28:42269}
270
271FilePath CommandLine::GetProgram() const {
[email protected]06cc083a2011-03-01 02:28:42272 return FilePath(argv_[0]);
[email protected]a40ca4302011-05-14 01:10:24273}
274
275void CommandLine::SetProgram(const FilePath& program) {
276 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]);
[email protected]06cc083a2011-03-01 02:28:42277}
278
279bool CommandLine::HasSwitch(const std::string& switch_string) const {
[email protected]a40ca4302011-05-14 01:10:24280 return switches_.find(LowerASCIIOnWindows(switch_string)) != switches_.end();
[email protected]06cc083a2011-03-01 02:28:42281}
282
283std::string CommandLine::GetSwitchValueASCII(
284 const std::string& switch_string) const {
[email protected]a40ca4302011-05-14 01:10:24285 StringType value = GetSwitchValueNative(switch_string);
[email protected]06cc083a2011-03-01 02:28:42286 if (!IsStringASCII(value)) {
[email protected]a42d4632011-10-26 21:48:00287 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII.";
288 return std::string();
[email protected]06cc083a2011-03-01 02:28:42289 }
290#if defined(OS_WIN)
291 return WideToASCII(value);
292#else
293 return value;
294#endif
295}
296
297FilePath CommandLine::GetSwitchValuePath(
298 const std::string& switch_string) const {
299 return FilePath(GetSwitchValueNative(switch_string));
300}
301
302CommandLine::StringType CommandLine::GetSwitchValueNative(
303 const std::string& switch_string) const {
[email protected]a40ca4302011-05-14 01:10:24304 SwitchMap::const_iterator result = switches_.end();
305 result = switches_.find(LowerASCIIOnWindows(switch_string));
306 return result == switches_.end() ? StringType() : result->second;
[email protected]06cc083a2011-03-01 02:28:42307}
308
[email protected]06cc083a2011-03-01 02:28:42309void CommandLine::AppendSwitch(const std::string& switch_string) {
[email protected]a40ca4302011-05-14 01:10:24310 AppendSwitchNative(switch_string, StringType());
[email protected]06cc083a2011-03-01 02:28:42311}
312
313void CommandLine::AppendSwitchPath(const std::string& switch_string,
314 const FilePath& path) {
315 AppendSwitchNative(switch_string, path.value());
316}
317
318void CommandLine::AppendSwitchNative(const std::string& switch_string,
319 const CommandLine::StringType& value) {
[email protected]a40ca4302011-05-14 01:10:24320 std::string switch_key(LowerASCIIOnWindows(switch_string));
[email protected]06cc083a2011-03-01 02:28:42321#if defined(OS_WIN)
[email protected]a40ca4302011-05-14 01:10:24322 StringType combined_switch_string(ASCIIToWide(switch_key));
[email protected]06cc083a2011-03-01 02:28:42323#elif defined(OS_POSIX)
[email protected]a40ca4302011-05-14 01:10:24324 StringType combined_switch_string(switch_string);
325#endif
326 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string);
327 switches_[switch_key.substr(prefix_length)] = value;
328 // Preserve existing switch prefixes in |argv_|; only append one if necessary.
329 if (prefix_length == 0)
330 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string;
[email protected]06cc083a2011-03-01 02:28:42331 if (!value.empty())
332 combined_switch_string += kSwitchValueSeparator + value;
[email protected]a40ca4302011-05-14 01:10:24333 // Append the switch and update the switches/arguments divider |begin_args_|.
334 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string);
[email protected]06cc083a2011-03-01 02:28:42335}
336
337void CommandLine::AppendSwitchASCII(const std::string& switch_string,
338 const std::string& value_string) {
339#if defined(OS_WIN)
340 AppendSwitchNative(switch_string, ASCIIToWide(value_string));
341#elif defined(OS_POSIX)
342 AppendSwitchNative(switch_string, value_string);
343#endif
344}
345
[email protected]06cc083a2011-03-01 02:28:42346void CommandLine::CopySwitchesFrom(const CommandLine& source,
347 const char* const switches[],
348 size_t count) {
349 for (size_t i = 0; i < count; ++i) {
[email protected]a40ca4302011-05-14 01:10:24350 if (source.HasSwitch(switches[i]))
351 AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i]));
[email protected]06cc083a2011-03-01 02:28:42352 }
353}
354
[email protected]75f1c782011-07-13 23:41:22355CommandLine::StringVector CommandLine::GetArgs() const {
[email protected]a40ca4302011-05-14 01:10:24356 // Gather all arguments after the last switch (may include kSwitchTerminator).
357 StringVector args(argv_.begin() + begin_args_, argv_.end());
358 // Erase only the first kSwitchTerminator (maybe "--" is a legitimate page?)
359 StringVector::iterator switch_terminator =
360 std::find(args.begin(), args.end(), kSwitchTerminator);
361 if (switch_terminator != args.end())
362 args.erase(switch_terminator);
363 return args;
364}
365
[email protected]06cc083a2011-03-01 02:28:42366void CommandLine::AppendArg(const std::string& value) {
367#if defined(OS_WIN)
368 DCHECK(IsStringUTF8(value));
369 AppendArgNative(UTF8ToWide(value));
370#elif defined(OS_POSIX)
371 AppendArgNative(value);
372#endif
373}
374
375void CommandLine::AppendArgPath(const FilePath& path) {
376 AppendArgNative(path.value());
377}
378
379void CommandLine::AppendArgNative(const CommandLine::StringType& value) {
[email protected]06cc083a2011-03-01 02:28:42380 argv_.push_back(value);
[email protected]06cc083a2011-03-01 02:28:42381}
382
383void CommandLine::AppendArguments(const CommandLine& other,
384 bool include_program) {
[email protected]06cc083a2011-03-01 02:28:42385 if (include_program)
[email protected]a40ca4302011-05-14 01:10:24386 SetProgram(other.GetProgram());
387 AppendSwitchesAndArguments(*this, other.argv());
[email protected]06cc083a2011-03-01 02:28:42388}
389
390void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) {
[email protected]06cc083a2011-03-01 02:28:42391 if (wrapper.empty())
392 return;
[email protected]a40ca4302011-05-14 01:10:24393 // The wrapper may have embedded arguments (like "gdb --args"). In this case,
394 // we don't pretend to do anything fancy, we just split on spaces.
395 StringVector wrapper_argv;
396 base::SplitString(wrapper, FILE_PATH_LITERAL(' '), &wrapper_argv);
397 // Prepend the wrapper and update the switches/arguments |begin_args_|.
398 argv_.insert(argv_.begin(), wrapper_argv.begin(), wrapper_argv.end());
399 begin_args_ += wrapper_argv.size();
[email protected]06cc083a2011-03-01 02:28:42400}
401
402#if defined(OS_WIN)
[email protected]bb975362009-01-21 01:00:22403void CommandLine::ParseFromString(const std::wstring& command_line) {
[email protected]a40ca4302011-05-14 01:10:24404 std::wstring command_line_string;
405 TrimWhitespace(command_line, TRIM_ALL, &command_line_string);
406 if (command_line_string.empty())
[email protected]bb975362009-01-21 01:00:22407 return;
408
409 int num_args = 0;
410 wchar_t** args = NULL;
[email protected]a40ca4302011-05-14 01:10:24411 args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args);
[email protected]bb975362009-01-21 01:00:22412
[email protected]a42d4632011-10-26 21:48:00413 DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: "
414 << command_line;
[email protected]a40ca4302011-05-14 01:10:24415 InitFromArgv(num_args, args);
416 LocalFree(args);
[email protected]bb975362009-01-21 01:00:22417}
[email protected]f3adb5c2008-08-07 20:07:32418#endif