blob: 873da813483651bc9e74b0510e17fa925225b5bd [file] [log] [blame]
erikwright@chromium.org506c72b2012-02-28 03:38:12 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
evanm@google.com91cdff82008-08-08 05:07:32 +09005#include "base/command_line.h"
6
initial.commit3f4a7322008-07-27 06:49:38 +09007#include <algorithm>
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +09008#include <ostream>
initial.commit3f4a7322008-07-27 06:49:38 +09009
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090010#include "base/files/file_path.h"
initial.commit3f4a7322008-07-27 06:49:38 +090011#include "base/logging.h"
avia6a6a682015-12-27 07:15:14 +090012#include "base/macros.h"
tfarina@chromium.org63aaf3f2013-02-08 08:01:39 +090013#include "base/strings/string_split.h"
skyostil09e952b2017-03-30 02:38:35 +090014#include "base/strings/string_tokenizer.h"
avi@chromium.org94bd5732013-06-11 22:36:37 +090015#include "base/strings/string_util.h"
avi@chromium.org17f60622013-06-08 03:37:07 +090016#include "base/strings/utf_string_conversions.h"
brettw@chromium.org6a3802c2010-12-30 06:06:43 +090017#include "build/build_config.h"
initial.commit3f4a7322008-07-27 06:49:38 +090018
brettw@chromium.org6a3802c2010-12-30 06:06:43 +090019#if defined(OS_WIN)
20#include <windows.h>
21#include <shellapi.h>
mdm@chromium.org18175a02009-09-11 03:02:17 +090022#endif
23
brettw@chromium.org37273892014-03-18 08:07:15 +090024namespace base {
brettw@chromium.orgc02b6032013-02-16 13:12:26 +090025
evan@chromium.org4bbc6132009-01-21 10:00:22 +090026CommandLine* CommandLine::current_process_commandline_ = NULL;
initial.commit3f4a7322008-07-27 06:49:38 +090027
msw@chromium.org7b2af612011-03-01 11:28:42 +090028namespace {
brettw@chromium.org37273892014-03-18 08:07:15 +090029
msw@chromium.org53fbac12011-05-14 10:10:24 +090030const CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--");
31const CommandLine::CharType kSwitchValueSeparator[] = FILE_PATH_LITERAL("=");
brettw@chromium.org40b2d582013-09-26 08:36:00 +090032
msw@chromium.org7b2af612011-03-01 11:28:42 +090033// Since we use a lazy match, make sure that longer versions (like "--") are
34// listed before shorter versions (like "-") of similar prefixes.
pinkerton@google.com0c0e01c2008-08-09 05:46:21 +090035#if defined(OS_WIN)
brettw@chromium.org40b2d582013-09-26 08:36:00 +090036// By putting slash last, we can control whether it is treaded as a switch
37// value by changing the value of switch_prefix_count to be one less than
38// the array size.
msw@chromium.org53fbac12011-05-14 10:10:24 +090039const CommandLine::CharType* const kSwitchPrefixes[] = {L"--", L"-", L"/"};
pinkerton@google.com0c0e01c2008-08-09 05:46:21 +090040#elif defined(OS_POSIX)
evanm@google.com638e9fb2008-08-12 10:14:37 +090041// Unixes don't use slash as a switch.
msw@chromium.org53fbac12011-05-14 10:10:24 +090042const CommandLine::CharType* const kSwitchPrefixes[] = {"--", "-"};
pinkerton@google.com0c0e01c2008-08-09 05:46:21 +090043#endif
brettw@chromium.org40b2d582013-09-26 08:36:00 +090044size_t switch_prefix_count = arraysize(kSwitchPrefixes);
initial.commit3f4a7322008-07-27 06:49:38 +090045
msw@chromium.org53fbac12011-05-14 10:10:24 +090046size_t GetSwitchPrefixLength(const CommandLine::StringType& string) {
brettw@chromium.org40b2d582013-09-26 08:36:00 +090047 for (size_t i = 0; i < switch_prefix_count; ++i) {
msw@chromium.org53fbac12011-05-14 10:10:24 +090048 CommandLine::StringType prefix(kSwitchPrefixes[i]);
joth@chromium.orgb23d1842011-09-14 00:45:34 +090049 if (string.compare(0, prefix.length(), prefix) == 0)
msw@chromium.org53fbac12011-05-14 10:10:24 +090050 return prefix.length();
51 }
52 return 0;
initial.commit3f4a7322008-07-27 06:49:38 +090053}
msw@chromium.orgda7a5482011-02-20 15:33:04 +090054
msw@chromium.org53fbac12011-05-14 10:10:24 +090055// Fills in |switch_string| and |switch_value| if |string| is a switch.
56// This will preserve the input switch prefix in the output |switch_string|.
57bool IsSwitch(const CommandLine::StringType& string,
58 CommandLine::StringType* switch_string,
59 CommandLine::StringType* switch_value) {
60 switch_string->clear();
61 switch_value->clear();
jochen@chromium.org1e9efb12012-10-19 15:19:59 +090062 size_t prefix_length = GetSwitchPrefixLength(string);
63 if (prefix_length == 0 || prefix_length == string.length())
msw@chromium.org53fbac12011-05-14 10:10:24 +090064 return false;
65
66 const size_t equals_position = string.find(kSwitchValueSeparator);
67 *switch_string = string.substr(0, equals_position);
68 if (equals_position != CommandLine::StringType::npos)
69 *switch_value = string.substr(equals_position + 1);
70 return true;
71}
72
73// Append switches and arguments, keeping switches before arguments.
thestig2d5b2e32014-12-05 07:14:22 +090074void AppendSwitchesAndArguments(CommandLine* command_line,
msw@chromium.org53fbac12011-05-14 10:10:24 +090075 const CommandLine::StringVector& argv) {
76 bool parse_switches = true;
77 for (size_t i = 1; i < argv.size(); ++i) {
78 CommandLine::StringType arg = argv[i];
tfarinab6894c12015-12-06 22:25:41 +090079#if defined(OS_WIN)
brettw@chromium.org37273892014-03-18 08:07:15 +090080 TrimWhitespace(arg, TRIM_ALL, &arg);
tfarinab6894c12015-12-06 22:25:41 +090081#else
82 TrimWhitespaceASCII(arg, TRIM_ALL, &arg);
83#endif
msw@chromium.org53fbac12011-05-14 10:10:24 +090084
85 CommandLine::StringType switch_string;
86 CommandLine::StringType switch_value;
87 parse_switches &= (arg != kSwitchTerminator);
88 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) {
89#if defined(OS_WIN)
thestig2d5b2e32014-12-05 07:14:22 +090090 command_line->AppendSwitchNative(UTF16ToASCII(switch_string),
91 switch_value);
msw@chromium.org53fbac12011-05-14 10:10:24 +090092#elif defined(OS_POSIX)
thestig2d5b2e32014-12-05 07:14:22 +090093 command_line->AppendSwitchNative(switch_string, switch_value);
msw@chromium.org53fbac12011-05-14 10:10:24 +090094#endif
95 } else {
thestig2d5b2e32014-12-05 07:14:22 +090096 command_line->AppendArgNative(arg);
msw@chromium.org53fbac12011-05-14 10:10:24 +090097 }
98 }
99}
100
msw@chromium.org53fbac12011-05-14 10:10:24 +0900101#if defined(OS_WIN)
102// Quote a string as necessary for CommandLineToArgvW compatiblity *on Windows*.
thestig2d5b2e32014-12-05 07:14:22 +0900103string16 QuoteForCommandLineToArgvW(const string16& arg,
104 bool quote_placeholders) {
msw@chromium.orgda7a5482011-02-20 15:33:04 +0900105 // We follow the quoting rules of CommandLineToArgvW.
106 // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
thestig2d5b2e32014-12-05 07:14:22 +0900107 string16 quotable_chars(L" \\\"");
mgiuca90d74492014-10-01 18:24:51 +0900108 // We may also be required to quote '%', which is commonly used in a command
109 // line as a placeholder. (It may be substituted for a string with spaces.)
110 if (quote_placeholders)
111 quotable_chars.push_back(L'%');
thestig2d5b2e32014-12-05 07:14:22 +0900112 if (arg.find_first_of(quotable_chars) == string16::npos) {
msw@chromium.orgda7a5482011-02-20 15:33:04 +0900113 // No quoting necessary.
114 return arg;
115 }
116
thestig2d5b2e32014-12-05 07:14:22 +0900117 string16 out;
msw@chromium.orgda7a5482011-02-20 15:33:04 +0900118 out.push_back(L'"');
119 for (size_t i = 0; i < arg.size(); ++i) {
120 if (arg[i] == '\\') {
121 // Find the extent of this run of backslashes.
122 size_t start = i, end = start + 1;
thestig2d5b2e32014-12-05 07:14:22 +0900123 for (; end < arg.size() && arg[end] == '\\'; ++end) {}
msw@chromium.orgda7a5482011-02-20 15:33:04 +0900124 size_t backslash_count = end - start;
125
126 // Backslashes are escapes only if the run is followed by a double quote.
127 // Since we also will end the string with a double quote, we escape for
128 // either a double quote or the end of the string.
129 if (end == arg.size() || arg[end] == '"') {
130 // To quote, we need to output 2x as many backslashes.
131 backslash_count *= 2;
132 }
133 for (size_t j = 0; j < backslash_count; ++j)
134 out.push_back('\\');
135
136 // Advance i to one before the end to balance i++ in loop.
137 i = end - 1;
138 } else if (arg[i] == '"') {
139 out.push_back('\\');
140 out.push_back('"');
141 } else {
142 out.push_back(arg[i]);
143 }
144 }
145 out.push_back('"');
146
147 return out;
148}
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900149#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900150
msw@chromium.org7b2af612011-03-01 11:28:42 +0900151} // namespace
152
msw@chromium.org53fbac12011-05-14 10:10:24 +0900153CommandLine::CommandLine(NoProgram no_program)
154 : argv_(1),
155 begin_args_(1) {
msw@chromium.org7b2af612011-03-01 11:28:42 +0900156}
157
msw@chromium.org53fbac12011-05-14 10:10:24 +0900158CommandLine::CommandLine(const FilePath& program)
159 : argv_(1),
160 begin_args_(1) {
161 SetProgram(program);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900162}
163
msw@chromium.org53fbac12011-05-14 10:10:24 +0900164CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv)
165 : argv_(1),
166 begin_args_(1) {
msw@chromium.org7b2af612011-03-01 11:28:42 +0900167 InitFromArgv(argc, argv);
168}
169
msw@chromium.org53fbac12011-05-14 10:10:24 +0900170CommandLine::CommandLine(const StringVector& argv)
171 : argv_(1),
172 begin_args_(1) {
msw@chromium.org7b2af612011-03-01 11:28:42 +0900173 InitFromArgv(argv);
174}
msw@chromium.org7b2af612011-03-01 11:28:42 +0900175
jackhoua5995af2015-05-21 13:48:00 +0900176CommandLine::CommandLine(const CommandLine& other)
177 : argv_(other.argv_),
178 switches_(other.switches_),
179 begin_args_(other.begin_args_) {
180 ResetStringPieces();
181}
182
183CommandLine& CommandLine::operator=(const CommandLine& other) {
184 argv_ = other.argv_;
185 switches_ = other.switches_;
186 begin_args_ = other.begin_args_;
187 ResetStringPieces();
188 return *this;
189}
190
msw@chromium.org96b695b2011-03-02 05:47:58 +0900191CommandLine::~CommandLine() {
192}
193
brettw@chromium.org40b2d582013-09-26 08:36:00 +0900194#if defined(OS_WIN)
195// static
196void CommandLine::set_slash_is_not_a_switch() {
197 // The last switch prefix should be slash, so adjust the size to skip it.
danakja7ea6662015-03-10 07:27:25 +0900198 DCHECK_EQ(wcscmp(kSwitchPrefixes[arraysize(kSwitchPrefixes) - 1], L"/"), 0);
brettw@chromium.org40b2d582013-09-26 08:36:00 +0900199 switch_prefix_count = arraysize(kSwitchPrefixes) - 1;
200}
anantad52b7112016-06-23 06:40:31 +0900201
202// static
203void CommandLine::InitUsingArgvForTesting(int argc, const char* const* argv) {
204 DCHECK(!current_process_commandline_);
205 current_process_commandline_ = new CommandLine(NO_PROGRAM);
206 // On Windows we need to convert the command line arguments to string16.
207 base::CommandLine::StringVector argv_vector;
208 for (int i = 0; i < argc; ++i)
209 argv_vector.push_back(UTF8ToUTF16(argv[i]));
210 current_process_commandline_->InitFromArgv(argv_vector);
211}
brettw@chromium.org40b2d582013-09-26 08:36:00 +0900212#endif
213
msw@chromium.org7b2af612011-03-01 11:28:42 +0900214// static
erikwright@chromium.org506c72b2012-02-28 03:38:12 +0900215bool CommandLine::Init(int argc, const char* const* argv) {
rvargas@google.com58a11842011-07-14 03:03:34 +0900216 if (current_process_commandline_) {
217 // If this is intentional, Reset() must be called first. If we are using
218 // the shared build mode, we have to share a single object across multiple
219 // shared libraries.
erikwright@chromium.org506c72b2012-02-28 03:38:12 +0900220 return false;
rvargas@google.com58a11842011-07-14 03:03:34 +0900221 }
222
msw@chromium.org53fbac12011-05-14 10:10:24 +0900223 current_process_commandline_ = new CommandLine(NO_PROGRAM);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900224#if defined(OS_WIN)
225 current_process_commandline_->ParseFromString(::GetCommandLineW());
226#elif defined(OS_POSIX)
227 current_process_commandline_->InitFromArgv(argc, argv);
228#endif
erikwright@chromium.org506c72b2012-02-28 03:38:12 +0900229
230 return true;
msw@chromium.org7b2af612011-03-01 11:28:42 +0900231}
232
233// static
234void CommandLine::Reset() {
235 DCHECK(current_process_commandline_);
236 delete current_process_commandline_;
237 current_process_commandline_ = NULL;
238}
239
240// static
241CommandLine* CommandLine::ForCurrentProcess() {
242 DCHECK(current_process_commandline_);
243 return current_process_commandline_;
erg@chromium.org493f5f62010-07-16 06:03:54 +0900244}
245
vollick@chromium.org4ff9adc2013-07-12 08:10:40 +0900246// static
247bool CommandLine::InitializedForCurrentProcess() {
248 return !!current_process_commandline_;
249}
250
evanm@google.com91cdff82008-08-08 05:07:32 +0900251#if defined(OS_WIN)
msw@chromium.org7b2af612011-03-01 11:28:42 +0900252// static
thestig2d5b2e32014-12-05 07:14:22 +0900253CommandLine CommandLine::FromString(const string16& command_line) {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900254 CommandLine cmd(NO_PROGRAM);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900255 cmd.ParseFromString(command_line);
256 return cmd;
257}
msw@chromium.org53fbac12011-05-14 10:10:24 +0900258#endif
msw@chromium.org7b2af612011-03-01 11:28:42 +0900259
msw@chromium.org53fbac12011-05-14 10:10:24 +0900260void CommandLine::InitFromArgv(int argc,
261 const CommandLine::CharType* const* argv) {
262 StringVector new_argv;
msw@chromium.org7b2af612011-03-01 11:28:42 +0900263 for (int i = 0; i < argc; ++i)
msw@chromium.org53fbac12011-05-14 10:10:24 +0900264 new_argv.push_back(argv[i]);
265 InitFromArgv(new_argv);
evan@chromium.orgc4fee2c2009-10-27 07:39:33 +0900266}
267
msw@chromium.org7b2af612011-03-01 11:28:42 +0900268void CommandLine::InitFromArgv(const StringVector& argv) {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900269 argv_ = StringVector(1);
bartfab@chromium.orgad4cf182013-06-18 17:19:18 +0900270 switches_.clear();
jackhoua5995af2015-05-21 13:48:00 +0900271 switches_by_stringpiece_.clear();
msw@chromium.org53fbac12011-05-14 10:10:24 +0900272 begin_args_ = 1;
273 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0]));
thestig2d5b2e32014-12-05 07:14:22 +0900274 AppendSwitchesAndArguments(this, argv);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900275}
msw@chromium.org7b2af612011-03-01 11:28:42 +0900276
msw@chromium.org7b2af612011-03-01 11:28:42 +0900277FilePath CommandLine::GetProgram() const {
msw@chromium.org7b2af612011-03-01 11:28:42 +0900278 return FilePath(argv_[0]);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900279}
280
281void CommandLine::SetProgram(const FilePath& program) {
tfarinab6894c12015-12-06 22:25:41 +0900282#if defined(OS_WIN)
brettw@chromium.org37273892014-03-18 08:07:15 +0900283 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]);
tfarinab6894c12015-12-06 22:25:41 +0900284#else
285 TrimWhitespaceASCII(program.value(), TRIM_ALL, &argv_[0]);
286#endif
msw@chromium.org7b2af612011-03-01 11:28:42 +0900287}
288
jackhoua5995af2015-05-21 13:48:00 +0900289bool CommandLine::HasSwitch(const base::StringPiece& switch_string) const {
brettw200ab412015-08-11 04:07:51 +0900290 DCHECK_EQ(ToLowerASCII(switch_string), switch_string);
jackhoua5995af2015-05-21 13:48:00 +0900291 return switches_by_stringpiece_.find(switch_string) !=
292 switches_by_stringpiece_.end();
msw@chromium.org7b2af612011-03-01 11:28:42 +0900293}
294
jackhoua5995af2015-05-21 13:48:00 +0900295bool CommandLine::HasSwitch(const char switch_constant[]) const {
296 return HasSwitch(base::StringPiece(switch_constant));
tapted5762d502015-03-30 12:57:10 +0900297}
298
msw@chromium.org7b2af612011-03-01 11:28:42 +0900299std::string CommandLine::GetSwitchValueASCII(
jackhoua5995af2015-05-21 13:48:00 +0900300 const base::StringPiece& switch_string) const {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900301 StringType value = GetSwitchValueNative(switch_string);
brettw@chromium.org93397402014-03-18 08:55:43 +0900302 if (!IsStringASCII(value)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900303 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII.";
304 return std::string();
msw@chromium.org7b2af612011-03-01 11:28:42 +0900305 }
306#if defined(OS_WIN)
brettw@chromium.org37273892014-03-18 08:07:15 +0900307 return UTF16ToASCII(value);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900308#else
309 return value;
310#endif
311}
312
313FilePath CommandLine::GetSwitchValuePath(
jackhoua5995af2015-05-21 13:48:00 +0900314 const base::StringPiece& switch_string) const {
msw@chromium.org7b2af612011-03-01 11:28:42 +0900315 return FilePath(GetSwitchValueNative(switch_string));
316}
317
318CommandLine::StringType CommandLine::GetSwitchValueNative(
jackhoua5995af2015-05-21 13:48:00 +0900319 const base::StringPiece& switch_string) const {
brettw200ab412015-08-11 04:07:51 +0900320 DCHECK_EQ(ToLowerASCII(switch_string), switch_string);
jackhoua5995af2015-05-21 13:48:00 +0900321 auto result = switches_by_stringpiece_.find(switch_string);
322 return result == switches_by_stringpiece_.end() ? StringType()
323 : *(result->second);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900324}
325
msw@chromium.org7b2af612011-03-01 11:28:42 +0900326void CommandLine::AppendSwitch(const std::string& switch_string) {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900327 AppendSwitchNative(switch_string, StringType());
msw@chromium.org7b2af612011-03-01 11:28:42 +0900328}
329
330void CommandLine::AppendSwitchPath(const std::string& switch_string,
331 const FilePath& path) {
332 AppendSwitchNative(switch_string, path.value());
333}
334
335void CommandLine::AppendSwitchNative(const std::string& switch_string,
336 const CommandLine::StringType& value) {
337#if defined(OS_WIN)
brettw200ab412015-08-11 04:07:51 +0900338 const std::string switch_key = ToLowerASCII(switch_string);
thestig2d5b2e32014-12-05 07:14:22 +0900339 StringType combined_switch_string(ASCIIToUTF16(switch_key));
msw@chromium.org7b2af612011-03-01 11:28:42 +0900340#elif defined(OS_POSIX)
jackhoua5995af2015-05-21 13:48:00 +0900341 const std::string& switch_key = switch_string;
342 StringType combined_switch_string(switch_key);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900343#endif
344 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string);
jackhoua5995af2015-05-21 13:48:00 +0900345 auto insertion =
346 switches_.insert(make_pair(switch_key.substr(prefix_length), value));
347 if (!insertion.second)
348 insertion.first->second = value;
349 switches_by_stringpiece_[insertion.first->first] = &(insertion.first->second);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900350 // Preserve existing switch prefixes in |argv_|; only append one if necessary.
351 if (prefix_length == 0)
352 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string;
msw@chromium.org7b2af612011-03-01 11:28:42 +0900353 if (!value.empty())
354 combined_switch_string += kSwitchValueSeparator + value;
msw@chromium.org53fbac12011-05-14 10:10:24 +0900355 // Append the switch and update the switches/arguments divider |begin_args_|.
356 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900357}
358
359void CommandLine::AppendSwitchASCII(const std::string& switch_string,
360 const std::string& value_string) {
361#if defined(OS_WIN)
thestig2d5b2e32014-12-05 07:14:22 +0900362 AppendSwitchNative(switch_string, ASCIIToUTF16(value_string));
msw@chromium.org7b2af612011-03-01 11:28:42 +0900363#elif defined(OS_POSIX)
364 AppendSwitchNative(switch_string, value_string);
365#endif
366}
367
msw@chromium.org7b2af612011-03-01 11:28:42 +0900368void CommandLine::CopySwitchesFrom(const CommandLine& source,
369 const char* const switches[],
370 size_t count) {
371 for (size_t i = 0; i < count; ++i) {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900372 if (source.HasSwitch(switches[i]))
373 AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i]));
msw@chromium.org7b2af612011-03-01 11:28:42 +0900374 }
375}
376
msw@chromium.orga25adf42011-07-14 08:41:22 +0900377CommandLine::StringVector CommandLine::GetArgs() const {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900378 // Gather all arguments after the last switch (may include kSwitchTerminator).
379 StringVector args(argv_.begin() + begin_args_, argv_.end());
380 // Erase only the first kSwitchTerminator (maybe "--" is a legitimate page?)
381 StringVector::iterator switch_terminator =
382 std::find(args.begin(), args.end(), kSwitchTerminator);
383 if (switch_terminator != args.end())
384 args.erase(switch_terminator);
385 return args;
386}
387
msw@chromium.org7b2af612011-03-01 11:28:42 +0900388void CommandLine::AppendArg(const std::string& value) {
389#if defined(OS_WIN)
brettw@chromium.org93397402014-03-18 08:55:43 +0900390 DCHECK(IsStringUTF8(value));
391 AppendArgNative(UTF8ToWide(value));
msw@chromium.org7b2af612011-03-01 11:28:42 +0900392#elif defined(OS_POSIX)
393 AppendArgNative(value);
394#endif
395}
396
397void CommandLine::AppendArgPath(const FilePath& path) {
398 AppendArgNative(path.value());
399}
400
401void CommandLine::AppendArgNative(const CommandLine::StringType& value) {
msw@chromium.org7b2af612011-03-01 11:28:42 +0900402 argv_.push_back(value);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900403}
404
405void CommandLine::AppendArguments(const CommandLine& other,
406 bool include_program) {
msw@chromium.org7b2af612011-03-01 11:28:42 +0900407 if (include_program)
msw@chromium.org53fbac12011-05-14 10:10:24 +0900408 SetProgram(other.GetProgram());
thestig2d5b2e32014-12-05 07:14:22 +0900409 AppendSwitchesAndArguments(this, other.argv());
msw@chromium.org7b2af612011-03-01 11:28:42 +0900410}
411
412void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) {
msw@chromium.org7b2af612011-03-01 11:28:42 +0900413 if (wrapper.empty())
414 return;
skyostil09e952b2017-03-30 02:38:35 +0900415 // Split the wrapper command based on whitespace (with quoting).
416 using CommandLineTokenizer =
417 StringTokenizerT<StringType, StringType::const_iterator>;
418 CommandLineTokenizer tokenizer(wrapper, FILE_PATH_LITERAL(" "));
419 tokenizer.set_quote_chars(FILE_PATH_LITERAL("'\""));
420 std::vector<StringType> wrapper_argv;
421 while (tokenizer.GetNext())
422 wrapper_argv.emplace_back(tokenizer.token());
423
msw@chromium.org53fbac12011-05-14 10:10:24 +0900424 // Prepend the wrapper and update the switches/arguments |begin_args_|.
425 argv_.insert(argv_.begin(), wrapper_argv.begin(), wrapper_argv.end());
426 begin_args_ += wrapper_argv.size();
msw@chromium.org7b2af612011-03-01 11:28:42 +0900427}
428
429#if defined(OS_WIN)
thestig2d5b2e32014-12-05 07:14:22 +0900430void CommandLine::ParseFromString(const string16& command_line) {
431 string16 command_line_string;
brettw@chromium.org37273892014-03-18 08:07:15 +0900432 TrimWhitespace(command_line, TRIM_ALL, &command_line_string);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900433 if (command_line_string.empty())
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900434 return;
435
436 int num_args = 0;
437 wchar_t** args = NULL;
msw@chromium.org53fbac12011-05-14 10:10:24 +0900438 args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args);
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900439
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900440 DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: "
brettw@chromium.org37273892014-03-18 08:07:15 +0900441 << UTF16ToUTF8(command_line);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900442 InitFromArgv(num_args, args);
443 LocalFree(args);
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900444}
evanm@google.com91cdff82008-08-08 05:07:32 +0900445#endif
brettw@chromium.org37273892014-03-18 08:07:15 +0900446
mgiuca90d74492014-10-01 18:24:51 +0900447CommandLine::StringType CommandLine::GetCommandLineStringInternal(
448 bool quote_placeholders) const {
449 StringType string(argv_[0]);
450#if defined(OS_WIN)
451 string = QuoteForCommandLineToArgvW(string, quote_placeholders);
452#endif
453 StringType params(GetArgumentsStringInternal(quote_placeholders));
454 if (!params.empty()) {
455 string.append(StringType(FILE_PATH_LITERAL(" ")));
456 string.append(params);
457 }
458 return string;
459}
460
461CommandLine::StringType CommandLine::GetArgumentsStringInternal(
462 bool quote_placeholders) const {
463 StringType params;
464 // Append switches and arguments.
465 bool parse_switches = true;
466 for (size_t i = 1; i < argv_.size(); ++i) {
467 StringType arg = argv_[i];
468 StringType switch_string;
469 StringType switch_value;
470 parse_switches &= arg != kSwitchTerminator;
471 if (i > 1)
472 params.append(StringType(FILE_PATH_LITERAL(" ")));
473 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) {
474 params.append(switch_string);
475 if (!switch_value.empty()) {
476#if defined(OS_WIN)
477 switch_value =
478 QuoteForCommandLineToArgvW(switch_value, quote_placeholders);
479#endif
480 params.append(kSwitchValueSeparator + switch_value);
481 }
thestig2d5b2e32014-12-05 07:14:22 +0900482 } else {
mgiuca90d74492014-10-01 18:24:51 +0900483#if defined(OS_WIN)
484 arg = QuoteForCommandLineToArgvW(arg, quote_placeholders);
485#endif
486 params.append(arg);
487 }
488 }
489 return params;
490}
491
jackhoua5995af2015-05-21 13:48:00 +0900492void CommandLine::ResetStringPieces() {
493 switches_by_stringpiece_.clear();
494 for (const auto& entry : switches_)
495 switches_by_stringpiece_[entry.first] = &(entry.second);
496}
497
brettw@chromium.org37273892014-03-18 08:07:15 +0900498} // namespace base