blob: 0237ffeb104509dfa91653cf231d2b90ba75dff7 [file] [log] [blame]
[email protected]0fd23af2011-02-20 06:33:041// Copyright (c) 2011 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"
[email protected]4e5ae20f2010-09-24 04:52:1113#include "base/string_split.h"
initial.commitd7cae122008-07-26 21:49:3814#include "base/string_util.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]bb975362009-01-21 01:00:2223CommandLine* CommandLine::current_process_commandline_ = NULL;
initial.commitd7cae122008-07-26 21:49:3824
[email protected]06cc083a2011-03-01 02:28:4225namespace {
[email protected]a40ca4302011-05-14 01:10:2426const CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--");
27const CommandLine::CharType kSwitchValueSeparator[] = FILE_PATH_LITERAL("=");
[email protected]06cc083a2011-03-01 02:28:4228// Since we use a lazy match, make sure that longer versions (like "--") are
29// listed before shorter versions (like "-") of similar prefixes.
[email protected]5d426332008-08-08 20:46:2130#if defined(OS_WIN)
[email protected]a40ca4302011-05-14 01:10:2431const CommandLine::CharType* const kSwitchPrefixes[] = {L"--", L"-", L"/"};
[email protected]5d426332008-08-08 20:46:2132#elif defined(OS_POSIX)
[email protected]1a48f312008-08-12 01:14:3733// Unixes don't use slash as a switch.
[email protected]a40ca4302011-05-14 01:10:2434const CommandLine::CharType* const kSwitchPrefixes[] = {"--", "-"};
[email protected]5d426332008-08-08 20:46:2135#endif
initial.commitd7cae122008-07-26 21:49:3836
[email protected]a40ca4302011-05-14 01:10:2437size_t GetSwitchPrefixLength(const CommandLine::StringType& string) {
38 for (size_t i = 0; i < arraysize(kSwitchPrefixes); ++i) {
39 CommandLine::StringType prefix(kSwitchPrefixes[i]);
[email protected]1fa39f02011-09-13 15:45:3440 if (string.compare(0, prefix.length(), prefix) == 0)
[email protected]a40ca4302011-05-14 01:10:2441 return prefix.length();
42 }
43 return 0;
initial.commitd7cae122008-07-26 21:49:3844}
[email protected]0fd23af2011-02-20 06:33:0445
[email protected]a40ca4302011-05-14 01:10:2446// Fills in |switch_string| and |switch_value| if |string| is a switch.
47// This will preserve the input switch prefix in the output |switch_string|.
48bool 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.
64void 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*.
87std::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*.
98std::wstring QuoteForCommandLineToArgvW(const std::wstring& arg) {
[email protected]0fd23af2011-02-20 06:33:0499 // We follow the quoting rules of CommandLineToArgvW.
100 // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
101 if (arg.find_first_of(L" \\\"") == std::wstring::npos) {
102 // No quoting necessary.
103 return arg;
104 }
105
106 std::wstring out;
107 out.push_back(L'"');
108 for (size_t i = 0; i < arg.size(); ++i) {
109 if (arg[i] == '\\') {
110 // Find the extent of this run of backslashes.
111 size_t start = i, end = start + 1;
112 for (; end < arg.size() && arg[end] == '\\'; ++end)
113 /* empty */;
114 size_t backslash_count = end - start;
115
116 // Backslashes are escapes only if the run is followed by a double quote.
117 // Since we also will end the string with a double quote, we escape for
118 // either a double quote or the end of the string.
119 if (end == arg.size() || arg[end] == '"') {
120 // To quote, we need to output 2x as many backslashes.
121 backslash_count *= 2;
122 }
123 for (size_t j = 0; j < backslash_count; ++j)
124 out.push_back('\\');
125
126 // Advance i to one before the end to balance i++ in loop.
127 i = end - 1;
128 } else if (arg[i] == '"') {
129 out.push_back('\\');
130 out.push_back('"');
131 } else {
132 out.push_back(arg[i]);
133 }
134 }
135 out.push_back('"');
136
137 return out;
138}
[email protected]bb975362009-01-21 01:00:22139#endif
initial.commitd7cae122008-07-26 21:49:38140
[email protected]06cc083a2011-03-01 02:28:42141} // namespace
142
[email protected]a40ca4302011-05-14 01:10:24143CommandLine::CommandLine(NoProgram no_program)
144 : argv_(1),
145 begin_args_(1) {
[email protected]06cc083a2011-03-01 02:28:42146}
147
[email protected]a40ca4302011-05-14 01:10:24148CommandLine::CommandLine(const FilePath& program)
149 : argv_(1),
150 begin_args_(1) {
151 SetProgram(program);
[email protected]06cc083a2011-03-01 02:28:42152}
153
[email protected]a40ca4302011-05-14 01:10:24154CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv)
155 : argv_(1),
156 begin_args_(1) {
[email protected]06cc083a2011-03-01 02:28:42157 InitFromArgv(argc, argv);
158}
159
[email protected]a40ca4302011-05-14 01:10:24160CommandLine::CommandLine(const StringVector& argv)
161 : argv_(1),
162 begin_args_(1) {
[email protected]06cc083a2011-03-01 02:28:42163 InitFromArgv(argv);
164}
[email protected]06cc083a2011-03-01 02:28:42165
[email protected]acbeb3d2011-03-01 20:47:58166CommandLine::~CommandLine() {
167}
168
[email protected]06cc083a2011-03-01 02:28:42169// static
170void CommandLine::Init(int argc, const char* const* argv) {
[email protected]f96fe2c42011-07-13 18:03:34171 if (current_process_commandline_) {
172 // If this is intentional, Reset() must be called first. If we are using
173 // the shared build mode, we have to share a single object across multiple
174 // shared libraries.
175 return;
176 }
177
[email protected]a40ca4302011-05-14 01:10:24178 current_process_commandline_ = new CommandLine(NO_PROGRAM);
[email protected]06cc083a2011-03-01 02:28:42179#if defined(OS_WIN)
180 current_process_commandline_->ParseFromString(::GetCommandLineW());
181#elif defined(OS_POSIX)
182 current_process_commandline_->InitFromArgv(argc, argv);
183#endif
184}
185
186// static
187void CommandLine::Reset() {
188 DCHECK(current_process_commandline_);
189 delete current_process_commandline_;
190 current_process_commandline_ = NULL;
191}
192
193// static
194CommandLine* CommandLine::ForCurrentProcess() {
195 DCHECK(current_process_commandline_);
196 return current_process_commandline_;
[email protected]3a3d47472010-07-15 21:03:54197}
198
[email protected]f3adb5c2008-08-07 20:07:32199#if defined(OS_WIN)
[email protected]06cc083a2011-03-01 02:28:42200// static
201CommandLine CommandLine::FromString(const std::wstring& command_line) {
[email protected]a40ca4302011-05-14 01:10:24202 CommandLine cmd(NO_PROGRAM);
[email protected]06cc083a2011-03-01 02:28:42203 cmd.ParseFromString(command_line);
204 return cmd;
205}
[email protected]a40ca4302011-05-14 01:10:24206#endif
[email protected]06cc083a2011-03-01 02:28:42207
[email protected]a40ca4302011-05-14 01:10:24208void CommandLine::InitFromArgv(int argc,
209 const CommandLine::CharType* const* argv) {
210 StringVector new_argv;
[email protected]06cc083a2011-03-01 02:28:42211 for (int i = 0; i < argc; ++i)
[email protected]a40ca4302011-05-14 01:10:24212 new_argv.push_back(argv[i]);
213 InitFromArgv(new_argv);
[email protected]51343d5a2009-10-26 22:39:33214}
215
[email protected]06cc083a2011-03-01 02:28:42216void CommandLine::InitFromArgv(const StringVector& argv) {
[email protected]a40ca4302011-05-14 01:10:24217 argv_ = StringVector(1);
218 begin_args_ = 1;
219 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0]));
220 AppendSwitchesAndArguments(*this, argv);
[email protected]06cc083a2011-03-01 02:28:42221}
[email protected]06cc083a2011-03-01 02:28:42222
[email protected]61a4c6f2011-07-20 04:54:52223CommandLine::StringType CommandLine::GetCommandLineString() const {
[email protected]a40ca4302011-05-14 01:10:24224 StringType string(argv_[0]);
[email protected]06cc083a2011-03-01 02:28:42225#if defined(OS_WIN)
[email protected]a40ca4302011-05-14 01:10:24226 string = QuoteForCommandLineToArgvW(string);
[email protected]06cc083a2011-03-01 02:28:42227#endif
[email protected]a40ca4302011-05-14 01:10:24228 // Append switches and arguments.
229 bool parse_switches = true;
230 for (size_t i = 1; i < argv_.size(); ++i) {
231 CommandLine::StringType arg = argv_[i];
232 CommandLine::StringType switch_string;
233 CommandLine::StringType switch_value;
234 parse_switches &= arg != kSwitchTerminator;
235 string.append(StringType(FILE_PATH_LITERAL(" ")));
236 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) {
237 string.append(switch_string);
238 if (!switch_value.empty()) {
239#if defined(OS_WIN)
240 switch_value = QuoteForCommandLineToArgvW(switch_value);
241#endif
242 string.append(kSwitchValueSeparator + switch_value);
243 }
244 }
245 else {
246#if defined(OS_WIN)
247 arg = QuoteForCommandLineToArgvW(arg);
248#endif
249 string.append(arg);
250 }
251 }
252 return string;
[email protected]06cc083a2011-03-01 02:28:42253}
254
255FilePath CommandLine::GetProgram() const {
[email protected]06cc083a2011-03-01 02:28:42256 return FilePath(argv_[0]);
[email protected]a40ca4302011-05-14 01:10:24257}
258
259void CommandLine::SetProgram(const FilePath& program) {
260 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]);
[email protected]06cc083a2011-03-01 02:28:42261}
262
263bool CommandLine::HasSwitch(const std::string& switch_string) const {
[email protected]a40ca4302011-05-14 01:10:24264 return switches_.find(LowerASCIIOnWindows(switch_string)) != switches_.end();
[email protected]06cc083a2011-03-01 02:28:42265}
266
267std::string CommandLine::GetSwitchValueASCII(
268 const std::string& switch_string) const {
[email protected]a40ca4302011-05-14 01:10:24269 StringType value = GetSwitchValueNative(switch_string);
[email protected]06cc083a2011-03-01 02:28:42270 if (!IsStringASCII(value)) {
[email protected]a42d4632011-10-26 21:48:00271 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII.";
272 return std::string();
[email protected]06cc083a2011-03-01 02:28:42273 }
274#if defined(OS_WIN)
275 return WideToASCII(value);
276#else
277 return value;
278#endif
279}
280
281FilePath CommandLine::GetSwitchValuePath(
282 const std::string& switch_string) const {
283 return FilePath(GetSwitchValueNative(switch_string));
284}
285
286CommandLine::StringType CommandLine::GetSwitchValueNative(
287 const std::string& switch_string) const {
[email protected]a40ca4302011-05-14 01:10:24288 SwitchMap::const_iterator result = switches_.end();
289 result = switches_.find(LowerASCIIOnWindows(switch_string));
290 return result == switches_.end() ? StringType() : result->second;
[email protected]06cc083a2011-03-01 02:28:42291}
292
[email protected]06cc083a2011-03-01 02:28:42293void CommandLine::AppendSwitch(const std::string& switch_string) {
[email protected]a40ca4302011-05-14 01:10:24294 AppendSwitchNative(switch_string, StringType());
[email protected]06cc083a2011-03-01 02:28:42295}
296
297void CommandLine::AppendSwitchPath(const std::string& switch_string,
298 const FilePath& path) {
299 AppendSwitchNative(switch_string, path.value());
300}
301
302void CommandLine::AppendSwitchNative(const std::string& switch_string,
303 const CommandLine::StringType& value) {
[email protected]a40ca4302011-05-14 01:10:24304 std::string switch_key(LowerASCIIOnWindows(switch_string));
[email protected]06cc083a2011-03-01 02:28:42305#if defined(OS_WIN)
[email protected]a40ca4302011-05-14 01:10:24306 StringType combined_switch_string(ASCIIToWide(switch_key));
[email protected]06cc083a2011-03-01 02:28:42307#elif defined(OS_POSIX)
[email protected]a40ca4302011-05-14 01:10:24308 StringType combined_switch_string(switch_string);
309#endif
310 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string);
311 switches_[switch_key.substr(prefix_length)] = value;
312 // Preserve existing switch prefixes in |argv_|; only append one if necessary.
313 if (prefix_length == 0)
314 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string;
[email protected]06cc083a2011-03-01 02:28:42315 if (!value.empty())
316 combined_switch_string += kSwitchValueSeparator + value;
[email protected]a40ca4302011-05-14 01:10:24317 // Append the switch and update the switches/arguments divider |begin_args_|.
318 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string);
[email protected]06cc083a2011-03-01 02:28:42319}
320
321void CommandLine::AppendSwitchASCII(const std::string& switch_string,
322 const std::string& value_string) {
323#if defined(OS_WIN)
324 AppendSwitchNative(switch_string, ASCIIToWide(value_string));
325#elif defined(OS_POSIX)
326 AppendSwitchNative(switch_string, value_string);
327#endif
328}
329
[email protected]06cc083a2011-03-01 02:28:42330void CommandLine::CopySwitchesFrom(const CommandLine& source,
331 const char* const switches[],
332 size_t count) {
333 for (size_t i = 0; i < count; ++i) {
[email protected]a40ca4302011-05-14 01:10:24334 if (source.HasSwitch(switches[i]))
335 AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i]));
[email protected]06cc083a2011-03-01 02:28:42336 }
337}
338
[email protected]75f1c782011-07-13 23:41:22339CommandLine::StringVector CommandLine::GetArgs() const {
[email protected]a40ca4302011-05-14 01:10:24340 // Gather all arguments after the last switch (may include kSwitchTerminator).
341 StringVector args(argv_.begin() + begin_args_, argv_.end());
342 // Erase only the first kSwitchTerminator (maybe "--" is a legitimate page?)
343 StringVector::iterator switch_terminator =
344 std::find(args.begin(), args.end(), kSwitchTerminator);
345 if (switch_terminator != args.end())
346 args.erase(switch_terminator);
347 return args;
348}
349
[email protected]06cc083a2011-03-01 02:28:42350void CommandLine::AppendArg(const std::string& value) {
351#if defined(OS_WIN)
352 DCHECK(IsStringUTF8(value));
353 AppendArgNative(UTF8ToWide(value));
354#elif defined(OS_POSIX)
355 AppendArgNative(value);
356#endif
357}
358
359void CommandLine::AppendArgPath(const FilePath& path) {
360 AppendArgNative(path.value());
361}
362
363void CommandLine::AppendArgNative(const CommandLine::StringType& value) {
[email protected]06cc083a2011-03-01 02:28:42364 argv_.push_back(value);
[email protected]06cc083a2011-03-01 02:28:42365}
366
367void CommandLine::AppendArguments(const CommandLine& other,
368 bool include_program) {
[email protected]06cc083a2011-03-01 02:28:42369 if (include_program)
[email protected]a40ca4302011-05-14 01:10:24370 SetProgram(other.GetProgram());
371 AppendSwitchesAndArguments(*this, other.argv());
[email protected]06cc083a2011-03-01 02:28:42372}
373
374void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) {
[email protected]06cc083a2011-03-01 02:28:42375 if (wrapper.empty())
376 return;
[email protected]a40ca4302011-05-14 01:10:24377 // The wrapper may have embedded arguments (like "gdb --args"). In this case,
378 // we don't pretend to do anything fancy, we just split on spaces.
379 StringVector wrapper_argv;
380 base::SplitString(wrapper, FILE_PATH_LITERAL(' '), &wrapper_argv);
381 // Prepend the wrapper and update the switches/arguments |begin_args_|.
382 argv_.insert(argv_.begin(), wrapper_argv.begin(), wrapper_argv.end());
383 begin_args_ += wrapper_argv.size();
[email protected]06cc083a2011-03-01 02:28:42384}
385
386#if defined(OS_WIN)
[email protected]bb975362009-01-21 01:00:22387void CommandLine::ParseFromString(const std::wstring& command_line) {
[email protected]a40ca4302011-05-14 01:10:24388 std::wstring command_line_string;
389 TrimWhitespace(command_line, TRIM_ALL, &command_line_string);
390 if (command_line_string.empty())
[email protected]bb975362009-01-21 01:00:22391 return;
392
393 int num_args = 0;
394 wchar_t** args = NULL;
[email protected]a40ca4302011-05-14 01:10:24395 args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args);
[email protected]bb975362009-01-21 01:00:22396
[email protected]a42d4632011-10-26 21:48:00397 DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: "
398 << command_line;
[email protected]a40ca4302011-05-14 01:10:24399 InitFromArgv(num_args, args);
400 LocalFree(args);
[email protected]bb975362009-01-21 01:00:22401}
[email protected]f3adb5c2008-08-07 20:07:32402#endif