Adds a new layer animation element which owns an interpolated transform. This allows...
[chromium-blink-merge.git] / base / command_line.cc
blob983181fc7a613edd133905a4759369ac61fee3a0
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/command_line.h"
7 #include <algorithm>
8 #include <ostream>
10 #include "base/basictypes.h"
11 #include "base/file_path.h"
12 #include "base/logging.h"
13 #include "base/string_split.h"
14 #include "base/string_util.h"
15 #include "base/utf_string_conversions.h"
16 #include "build/build_config.h"
18 #if defined(OS_WIN)
19 #include <windows.h>
20 #include <shellapi.h>
21 #endif
23 CommandLine* CommandLine::current_process_commandline_ = NULL;
25 namespace {
26 const CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--");
27 const CommandLine::CharType kSwitchValueSeparator[] = FILE_PATH_LITERAL("=");
28 // Since we use a lazy match, make sure that longer versions (like "--") are
29 // listed before shorter versions (like "-") of similar prefixes.
30 #if defined(OS_WIN)
31 const CommandLine::CharType* const kSwitchPrefixes[] = {L"--", L"-", L"/"};
32 #elif defined(OS_POSIX)
33 // Unixes don't use slash as a switch.
34 const CommandLine::CharType* const kSwitchPrefixes[] = {"--", "-"};
35 #endif
37 size_t GetSwitchPrefixLength(const CommandLine::StringType& string) {
38 for (size_t i = 0; i < arraysize(kSwitchPrefixes); ++i) {
39 CommandLine::StringType prefix(kSwitchPrefixes[i]);
40 if (string.compare(0, prefix.length(), prefix) == 0)
41 return prefix.length();
43 return 0;
46 // 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|.
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;
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;
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);
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);
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
96 #if defined(OS_WIN)
97 // Quote a string as necessary for CommandLineToArgvW compatiblity *on Windows*.
98 std::wstring QuoteForCommandLineToArgvW(const std::wstring& arg) {
99 // 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;
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;
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;
123 for (size_t j = 0; j < backslash_count; ++j)
124 out.push_back('\\');
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]);
135 out.push_back('"');
137 return out;
139 #endif
141 } // namespace
143 CommandLine::CommandLine(NoProgram no_program)
144 : argv_(1),
145 begin_args_(1) {
148 CommandLine::CommandLine(const FilePath& program)
149 : argv_(1),
150 begin_args_(1) {
151 SetProgram(program);
154 CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv)
155 : argv_(1),
156 begin_args_(1) {
157 InitFromArgv(argc, argv);
160 CommandLine::CommandLine(const StringVector& argv)
161 : argv_(1),
162 begin_args_(1) {
163 InitFromArgv(argv);
166 CommandLine::~CommandLine() {
169 // static
170 bool CommandLine::Init(int argc, const char* const* argv) {
171 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 false;
178 current_process_commandline_ = new CommandLine(NO_PROGRAM);
179 #if defined(OS_WIN)
180 current_process_commandline_->ParseFromString(::GetCommandLineW());
181 #elif defined(OS_POSIX)
182 current_process_commandline_->InitFromArgv(argc, argv);
183 #endif
185 return true;
188 // static
189 void CommandLine::Reset() {
190 DCHECK(current_process_commandline_);
191 delete current_process_commandline_;
192 current_process_commandline_ = NULL;
195 // static
196 CommandLine* CommandLine::ForCurrentProcess() {
197 DCHECK(current_process_commandline_);
198 return current_process_commandline_;
201 #if defined(OS_WIN)
202 // static
203 CommandLine CommandLine::FromString(const std::wstring& command_line) {
204 CommandLine cmd(NO_PROGRAM);
205 cmd.ParseFromString(command_line);
206 return cmd;
208 #endif
210 void CommandLine::InitFromArgv(int argc,
211 const CommandLine::CharType* const* argv) {
212 StringVector new_argv;
213 for (int i = 0; i < argc; ++i)
214 new_argv.push_back(argv[i]);
215 InitFromArgv(new_argv);
218 void CommandLine::InitFromArgv(const StringVector& argv) {
219 argv_ = StringVector(1);
220 begin_args_ = 1;
221 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0]));
222 AppendSwitchesAndArguments(*this, argv);
225 CommandLine::StringType CommandLine::GetCommandLineString() const {
226 StringType string(argv_[0]);
227 #if defined(OS_WIN)
228 string = QuoteForCommandLineToArgvW(string);
229 #endif
230 // Append switches and arguments.
231 bool parse_switches = true;
232 for (size_t i = 1; i < argv_.size(); ++i) {
233 CommandLine::StringType arg = argv_[i];
234 CommandLine::StringType switch_string;
235 CommandLine::StringType switch_value;
236 parse_switches &= arg != kSwitchTerminator;
237 string.append(StringType(FILE_PATH_LITERAL(" ")));
238 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) {
239 string.append(switch_string);
240 if (!switch_value.empty()) {
241 #if defined(OS_WIN)
242 switch_value = QuoteForCommandLineToArgvW(switch_value);
243 #endif
244 string.append(kSwitchValueSeparator + switch_value);
247 else {
248 #if defined(OS_WIN)
249 arg = QuoteForCommandLineToArgvW(arg);
250 #endif
251 string.append(arg);
254 return string;
257 FilePath CommandLine::GetProgram() const {
258 return FilePath(argv_[0]);
261 void CommandLine::SetProgram(const FilePath& program) {
262 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]);
265 bool CommandLine::HasSwitch(const std::string& switch_string) const {
266 return switches_.find(LowerASCIIOnWindows(switch_string)) != switches_.end();
269 std::string CommandLine::GetSwitchValueASCII(
270 const std::string& switch_string) const {
271 StringType value = GetSwitchValueNative(switch_string);
272 if (!IsStringASCII(value)) {
273 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII.";
274 return std::string();
276 #if defined(OS_WIN)
277 return WideToASCII(value);
278 #else
279 return value;
280 #endif
283 FilePath CommandLine::GetSwitchValuePath(
284 const std::string& switch_string) const {
285 return FilePath(GetSwitchValueNative(switch_string));
288 CommandLine::StringType CommandLine::GetSwitchValueNative(
289 const std::string& switch_string) const {
290 SwitchMap::const_iterator result = switches_.end();
291 result = switches_.find(LowerASCIIOnWindows(switch_string));
292 return result == switches_.end() ? StringType() : result->second;
295 void CommandLine::AppendSwitch(const std::string& switch_string) {
296 AppendSwitchNative(switch_string, StringType());
299 void CommandLine::AppendSwitchPath(const std::string& switch_string,
300 const FilePath& path) {
301 AppendSwitchNative(switch_string, path.value());
304 void CommandLine::AppendSwitchNative(const std::string& switch_string,
305 const CommandLine::StringType& value) {
306 std::string switch_key(LowerASCIIOnWindows(switch_string));
307 #if defined(OS_WIN)
308 StringType combined_switch_string(ASCIIToWide(switch_key));
309 #elif defined(OS_POSIX)
310 StringType combined_switch_string(switch_string);
311 #endif
312 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string);
313 switches_[switch_key.substr(prefix_length)] = value;
314 // Preserve existing switch prefixes in |argv_|; only append one if necessary.
315 if (prefix_length == 0)
316 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string;
317 if (!value.empty())
318 combined_switch_string += kSwitchValueSeparator + value;
319 // Append the switch and update the switches/arguments divider |begin_args_|.
320 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string);
323 void CommandLine::AppendSwitchASCII(const std::string& switch_string,
324 const std::string& value_string) {
325 #if defined(OS_WIN)
326 AppendSwitchNative(switch_string, ASCIIToWide(value_string));
327 #elif defined(OS_POSIX)
328 AppendSwitchNative(switch_string, value_string);
329 #endif
332 void CommandLine::CopySwitchesFrom(const CommandLine& source,
333 const char* const switches[],
334 size_t count) {
335 for (size_t i = 0; i < count; ++i) {
336 if (source.HasSwitch(switches[i]))
337 AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i]));
341 CommandLine::StringVector CommandLine::GetArgs() const {
342 // Gather all arguments after the last switch (may include kSwitchTerminator).
343 StringVector args(argv_.begin() + begin_args_, argv_.end());
344 // Erase only the first kSwitchTerminator (maybe "--" is a legitimate page?)
345 StringVector::iterator switch_terminator =
346 std::find(args.begin(), args.end(), kSwitchTerminator);
347 if (switch_terminator != args.end())
348 args.erase(switch_terminator);
349 return args;
352 void CommandLine::AppendArg(const std::string& value) {
353 #if defined(OS_WIN)
354 DCHECK(IsStringUTF8(value));
355 AppendArgNative(UTF8ToWide(value));
356 #elif defined(OS_POSIX)
357 AppendArgNative(value);
358 #endif
361 void CommandLine::AppendArgPath(const FilePath& path) {
362 AppendArgNative(path.value());
365 void CommandLine::AppendArgNative(const CommandLine::StringType& value) {
366 argv_.push_back(value);
369 void CommandLine::AppendArguments(const CommandLine& other,
370 bool include_program) {
371 if (include_program)
372 SetProgram(other.GetProgram());
373 AppendSwitchesAndArguments(*this, other.argv());
376 void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) {
377 if (wrapper.empty())
378 return;
379 // The wrapper may have embedded arguments (like "gdb --args"). In this case,
380 // we don't pretend to do anything fancy, we just split on spaces.
381 StringVector wrapper_argv;
382 base::SplitString(wrapper, FILE_PATH_LITERAL(' '), &wrapper_argv);
383 // Prepend the wrapper and update the switches/arguments |begin_args_|.
384 argv_.insert(argv_.begin(), wrapper_argv.begin(), wrapper_argv.end());
385 begin_args_ += wrapper_argv.size();
388 #if defined(OS_WIN)
389 void CommandLine::ParseFromString(const std::wstring& command_line) {
390 std::wstring command_line_string;
391 TrimWhitespace(command_line, TRIM_ALL, &command_line_string);
392 if (command_line_string.empty())
393 return;
395 int num_args = 0;
396 wchar_t** args = NULL;
397 args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args);
399 DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: "
400 << command_line;
401 InitFromArgv(num_args, args);
402 LocalFree(args);
404 #endif