security/sandbox/chromium/base/command_line.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "base/command_line.h"
michael@0 6
michael@0 7 #include <algorithm>
michael@0 8 #include <ostream>
michael@0 9
michael@0 10 #include "base/basictypes.h"
michael@0 11 #include "base/files/file_path.h"
michael@0 12 #include "base/logging.h"
michael@0 13 #include "base/strings/string_split.h"
michael@0 14 #include "base/strings/string_util.h"
michael@0 15 #include "base/strings/utf_string_conversions.h"
michael@0 16 #include "build/build_config.h"
michael@0 17
michael@0 18 #if defined(OS_WIN)
michael@0 19 #include <windows.h>
michael@0 20 #include <shellapi.h>
michael@0 21 #endif
michael@0 22
michael@0 23 using base::FilePath;
michael@0 24
michael@0 25 CommandLine* CommandLine::current_process_commandline_ = NULL;
michael@0 26
michael@0 27 namespace {
michael@0 28 const CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--");
michael@0 29 const CommandLine::CharType kSwitchValueSeparator[] = FILE_PATH_LITERAL("=");
michael@0 30 // Since we use a lazy match, make sure that longer versions (like "--") are
michael@0 31 // listed before shorter versions (like "-") of similar prefixes.
michael@0 32 #if defined(OS_WIN)
michael@0 33 const CommandLine::CharType* const kSwitchPrefixes[] = {L"--", L"-", L"/"};
michael@0 34 #elif defined(OS_POSIX)
michael@0 35 // Unixes don't use slash as a switch.
michael@0 36 const CommandLine::CharType* const kSwitchPrefixes[] = {"--", "-"};
michael@0 37 #endif
michael@0 38
michael@0 39 size_t GetSwitchPrefixLength(const CommandLine::StringType& string) {
michael@0 40 for (size_t i = 0; i < arraysize(kSwitchPrefixes); ++i) {
michael@0 41 CommandLine::StringType prefix(kSwitchPrefixes[i]);
michael@0 42 if (string.compare(0, prefix.length(), prefix) == 0)
michael@0 43 return prefix.length();
michael@0 44 }
michael@0 45 return 0;
michael@0 46 }
michael@0 47
michael@0 48 // Fills in |switch_string| and |switch_value| if |string| is a switch.
michael@0 49 // This will preserve the input switch prefix in the output |switch_string|.
michael@0 50 bool IsSwitch(const CommandLine::StringType& string,
michael@0 51 CommandLine::StringType* switch_string,
michael@0 52 CommandLine::StringType* switch_value) {
michael@0 53 switch_string->clear();
michael@0 54 switch_value->clear();
michael@0 55 size_t prefix_length = GetSwitchPrefixLength(string);
michael@0 56 if (prefix_length == 0 || prefix_length == string.length())
michael@0 57 return false;
michael@0 58
michael@0 59 const size_t equals_position = string.find(kSwitchValueSeparator);
michael@0 60 *switch_string = string.substr(0, equals_position);
michael@0 61 if (equals_position != CommandLine::StringType::npos)
michael@0 62 *switch_value = string.substr(equals_position + 1);
michael@0 63 return true;
michael@0 64 }
michael@0 65
michael@0 66 // Append switches and arguments, keeping switches before arguments.
michael@0 67 void AppendSwitchesAndArguments(CommandLine& command_line,
michael@0 68 const CommandLine::StringVector& argv) {
michael@0 69 bool parse_switches = true;
michael@0 70 for (size_t i = 1; i < argv.size(); ++i) {
michael@0 71 CommandLine::StringType arg = argv[i];
michael@0 72 TrimWhitespace(arg, TRIM_ALL, &arg);
michael@0 73
michael@0 74 CommandLine::StringType switch_string;
michael@0 75 CommandLine::StringType switch_value;
michael@0 76 parse_switches &= (arg != kSwitchTerminator);
michael@0 77 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) {
michael@0 78 #if defined(OS_WIN)
michael@0 79 command_line.AppendSwitchNative(WideToASCII(switch_string), switch_value);
michael@0 80 #elif defined(OS_POSIX)
michael@0 81 command_line.AppendSwitchNative(switch_string, switch_value);
michael@0 82 #endif
michael@0 83 } else {
michael@0 84 command_line.AppendArgNative(arg);
michael@0 85 }
michael@0 86 }
michael@0 87 }
michael@0 88
michael@0 89 // Lowercase switches for backwards compatiblity *on Windows*.
michael@0 90 std::string LowerASCIIOnWindows(const std::string& string) {
michael@0 91 #if defined(OS_WIN)
michael@0 92 return StringToLowerASCII(string);
michael@0 93 #elif defined(OS_POSIX)
michael@0 94 return string;
michael@0 95 #endif
michael@0 96 }
michael@0 97
michael@0 98
michael@0 99 #if defined(OS_WIN)
michael@0 100 // Quote a string as necessary for CommandLineToArgvW compatiblity *on Windows*.
michael@0 101 std::wstring QuoteForCommandLineToArgvW(const std::wstring& arg) {
michael@0 102 // We follow the quoting rules of CommandLineToArgvW.
michael@0 103 // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
michael@0 104 if (arg.find_first_of(L" \\\"") == std::wstring::npos) {
michael@0 105 // No quoting necessary.
michael@0 106 return arg;
michael@0 107 }
michael@0 108
michael@0 109 std::wstring out;
michael@0 110 out.push_back(L'"');
michael@0 111 for (size_t i = 0; i < arg.size(); ++i) {
michael@0 112 if (arg[i] == '\\') {
michael@0 113 // Find the extent of this run of backslashes.
michael@0 114 size_t start = i, end = start + 1;
michael@0 115 for (; end < arg.size() && arg[end] == '\\'; ++end)
michael@0 116 /* empty */;
michael@0 117 size_t backslash_count = end - start;
michael@0 118
michael@0 119 // Backslashes are escapes only if the run is followed by a double quote.
michael@0 120 // Since we also will end the string with a double quote, we escape for
michael@0 121 // either a double quote or the end of the string.
michael@0 122 if (end == arg.size() || arg[end] == '"') {
michael@0 123 // To quote, we need to output 2x as many backslashes.
michael@0 124 backslash_count *= 2;
michael@0 125 }
michael@0 126 for (size_t j = 0; j < backslash_count; ++j)
michael@0 127 out.push_back('\\');
michael@0 128
michael@0 129 // Advance i to one before the end to balance i++ in loop.
michael@0 130 i = end - 1;
michael@0 131 } else if (arg[i] == '"') {
michael@0 132 out.push_back('\\');
michael@0 133 out.push_back('"');
michael@0 134 } else {
michael@0 135 out.push_back(arg[i]);
michael@0 136 }
michael@0 137 }
michael@0 138 out.push_back('"');
michael@0 139
michael@0 140 return out;
michael@0 141 }
michael@0 142 #endif
michael@0 143
michael@0 144 } // namespace
michael@0 145
michael@0 146 CommandLine::CommandLine(NoProgram no_program)
michael@0 147 : argv_(1),
michael@0 148 begin_args_(1) {
michael@0 149 }
michael@0 150
michael@0 151 CommandLine::CommandLine(const FilePath& program)
michael@0 152 : argv_(1),
michael@0 153 begin_args_(1) {
michael@0 154 SetProgram(program);
michael@0 155 }
michael@0 156
michael@0 157 CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv)
michael@0 158 : argv_(1),
michael@0 159 begin_args_(1) {
michael@0 160 InitFromArgv(argc, argv);
michael@0 161 }
michael@0 162
michael@0 163 CommandLine::CommandLine(const StringVector& argv)
michael@0 164 : argv_(1),
michael@0 165 begin_args_(1) {
michael@0 166 InitFromArgv(argv);
michael@0 167 }
michael@0 168
michael@0 169 CommandLine::~CommandLine() {
michael@0 170 }
michael@0 171
michael@0 172 // static
michael@0 173 bool CommandLine::Init(int argc, const char* const* argv) {
michael@0 174 if (current_process_commandline_) {
michael@0 175 // If this is intentional, Reset() must be called first. If we are using
michael@0 176 // the shared build mode, we have to share a single object across multiple
michael@0 177 // shared libraries.
michael@0 178 return false;
michael@0 179 }
michael@0 180
michael@0 181 current_process_commandline_ = new CommandLine(NO_PROGRAM);
michael@0 182 #if defined(OS_WIN)
michael@0 183 current_process_commandline_->ParseFromString(::GetCommandLineW());
michael@0 184 #elif defined(OS_POSIX)
michael@0 185 current_process_commandline_->InitFromArgv(argc, argv);
michael@0 186 #endif
michael@0 187
michael@0 188 return true;
michael@0 189 }
michael@0 190
michael@0 191 // static
michael@0 192 void CommandLine::Reset() {
michael@0 193 DCHECK(current_process_commandline_);
michael@0 194 delete current_process_commandline_;
michael@0 195 current_process_commandline_ = NULL;
michael@0 196 }
michael@0 197
michael@0 198 // static
michael@0 199 CommandLine* CommandLine::ForCurrentProcess() {
michael@0 200 DCHECK(current_process_commandline_);
michael@0 201 return current_process_commandline_;
michael@0 202 }
michael@0 203
michael@0 204 // static
michael@0 205 bool CommandLine::InitializedForCurrentProcess() {
michael@0 206 return !!current_process_commandline_;
michael@0 207 }
michael@0 208
michael@0 209 #if defined(OS_WIN)
michael@0 210 // static
michael@0 211 CommandLine CommandLine::FromString(const std::wstring& command_line) {
michael@0 212 CommandLine cmd(NO_PROGRAM);
michael@0 213 cmd.ParseFromString(command_line);
michael@0 214 return cmd;
michael@0 215 }
michael@0 216 #endif
michael@0 217
michael@0 218 void CommandLine::InitFromArgv(int argc,
michael@0 219 const CommandLine::CharType* const* argv) {
michael@0 220 StringVector new_argv;
michael@0 221 for (int i = 0; i < argc; ++i)
michael@0 222 new_argv.push_back(argv[i]);
michael@0 223 InitFromArgv(new_argv);
michael@0 224 }
michael@0 225
michael@0 226 void CommandLine::InitFromArgv(const StringVector& argv) {
michael@0 227 argv_ = StringVector(1);
michael@0 228 switches_.clear();
michael@0 229 begin_args_ = 1;
michael@0 230 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0]));
michael@0 231 AppendSwitchesAndArguments(*this, argv);
michael@0 232 }
michael@0 233
michael@0 234 CommandLine::StringType CommandLine::GetCommandLineString() const {
michael@0 235 StringType string(argv_[0]);
michael@0 236 #if defined(OS_WIN)
michael@0 237 string = QuoteForCommandLineToArgvW(string);
michael@0 238 #endif
michael@0 239 StringType params(GetArgumentsString());
michael@0 240 if (!params.empty()) {
michael@0 241 string.append(StringType(FILE_PATH_LITERAL(" ")));
michael@0 242 string.append(params);
michael@0 243 }
michael@0 244 return string;
michael@0 245 }
michael@0 246
michael@0 247 CommandLine::StringType CommandLine::GetArgumentsString() const {
michael@0 248 StringType params;
michael@0 249 // Append switches and arguments.
michael@0 250 bool parse_switches = true;
michael@0 251 for (size_t i = 1; i < argv_.size(); ++i) {
michael@0 252 StringType arg = argv_[i];
michael@0 253 StringType switch_string;
michael@0 254 StringType switch_value;
michael@0 255 parse_switches &= arg != kSwitchTerminator;
michael@0 256 if (i > 1)
michael@0 257 params.append(StringType(FILE_PATH_LITERAL(" ")));
michael@0 258 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) {
michael@0 259 params.append(switch_string);
michael@0 260 if (!switch_value.empty()) {
michael@0 261 #if defined(OS_WIN)
michael@0 262 switch_value = QuoteForCommandLineToArgvW(switch_value);
michael@0 263 #endif
michael@0 264 params.append(kSwitchValueSeparator + switch_value);
michael@0 265 }
michael@0 266 }
michael@0 267 else {
michael@0 268 #if defined(OS_WIN)
michael@0 269 arg = QuoteForCommandLineToArgvW(arg);
michael@0 270 #endif
michael@0 271 params.append(arg);
michael@0 272 }
michael@0 273 }
michael@0 274 return params;
michael@0 275 }
michael@0 276
michael@0 277 FilePath CommandLine::GetProgram() const {
michael@0 278 return FilePath(argv_[0]);
michael@0 279 }
michael@0 280
michael@0 281 void CommandLine::SetProgram(const FilePath& program) {
michael@0 282 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]);
michael@0 283 }
michael@0 284
michael@0 285 bool CommandLine::HasSwitch(const std::string& switch_string) const {
michael@0 286 return switches_.find(LowerASCIIOnWindows(switch_string)) != switches_.end();
michael@0 287 }
michael@0 288
michael@0 289 std::string CommandLine::GetSwitchValueASCII(
michael@0 290 const std::string& switch_string) const {
michael@0 291 StringType value = GetSwitchValueNative(switch_string);
michael@0 292 if (!IsStringASCII(value)) {
michael@0 293 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII.";
michael@0 294 return std::string();
michael@0 295 }
michael@0 296 #if defined(OS_WIN)
michael@0 297 return WideToASCII(value);
michael@0 298 #else
michael@0 299 return value;
michael@0 300 #endif
michael@0 301 }
michael@0 302
michael@0 303 FilePath CommandLine::GetSwitchValuePath(
michael@0 304 const std::string& switch_string) const {
michael@0 305 return FilePath(GetSwitchValueNative(switch_string));
michael@0 306 }
michael@0 307
michael@0 308 CommandLine::StringType CommandLine::GetSwitchValueNative(
michael@0 309 const std::string& switch_string) const {
michael@0 310 SwitchMap::const_iterator result = switches_.end();
michael@0 311 result = switches_.find(LowerASCIIOnWindows(switch_string));
michael@0 312 return result == switches_.end() ? StringType() : result->second;
michael@0 313 }
michael@0 314
michael@0 315 void CommandLine::AppendSwitch(const std::string& switch_string) {
michael@0 316 AppendSwitchNative(switch_string, StringType());
michael@0 317 }
michael@0 318
michael@0 319 void CommandLine::AppendSwitchPath(const std::string& switch_string,
michael@0 320 const FilePath& path) {
michael@0 321 AppendSwitchNative(switch_string, path.value());
michael@0 322 }
michael@0 323
michael@0 324 void CommandLine::AppendSwitchNative(const std::string& switch_string,
michael@0 325 const CommandLine::StringType& value) {
michael@0 326 std::string switch_key(LowerASCIIOnWindows(switch_string));
michael@0 327 #if defined(OS_WIN)
michael@0 328 StringType combined_switch_string(ASCIIToWide(switch_key));
michael@0 329 #elif defined(OS_POSIX)
michael@0 330 StringType combined_switch_string(switch_string);
michael@0 331 #endif
michael@0 332 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string);
michael@0 333 switches_[switch_key.substr(prefix_length)] = value;
michael@0 334 // Preserve existing switch prefixes in |argv_|; only append one if necessary.
michael@0 335 if (prefix_length == 0)
michael@0 336 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string;
michael@0 337 if (!value.empty())
michael@0 338 combined_switch_string += kSwitchValueSeparator + value;
michael@0 339 // Append the switch and update the switches/arguments divider |begin_args_|.
michael@0 340 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string);
michael@0 341 }
michael@0 342
michael@0 343 void CommandLine::AppendSwitchASCII(const std::string& switch_string,
michael@0 344 const std::string& value_string) {
michael@0 345 #if defined(OS_WIN)
michael@0 346 AppendSwitchNative(switch_string, ASCIIToWide(value_string));
michael@0 347 #elif defined(OS_POSIX)
michael@0 348 AppendSwitchNative(switch_string, value_string);
michael@0 349 #endif
michael@0 350 }
michael@0 351
michael@0 352 void CommandLine::CopySwitchesFrom(const CommandLine& source,
michael@0 353 const char* const switches[],
michael@0 354 size_t count) {
michael@0 355 for (size_t i = 0; i < count; ++i) {
michael@0 356 if (source.HasSwitch(switches[i]))
michael@0 357 AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i]));
michael@0 358 }
michael@0 359 }
michael@0 360
michael@0 361 CommandLine::StringVector CommandLine::GetArgs() const {
michael@0 362 // Gather all arguments after the last switch (may include kSwitchTerminator).
michael@0 363 StringVector args(argv_.begin() + begin_args_, argv_.end());
michael@0 364 // Erase only the first kSwitchTerminator (maybe "--" is a legitimate page?)
michael@0 365 StringVector::iterator switch_terminator =
michael@0 366 std::find(args.begin(), args.end(), kSwitchTerminator);
michael@0 367 if (switch_terminator != args.end())
michael@0 368 args.erase(switch_terminator);
michael@0 369 return args;
michael@0 370 }
michael@0 371
michael@0 372 void CommandLine::AppendArg(const std::string& value) {
michael@0 373 #if defined(OS_WIN)
michael@0 374 DCHECK(IsStringUTF8(value));
michael@0 375 AppendArgNative(UTF8ToWide(value));
michael@0 376 #elif defined(OS_POSIX)
michael@0 377 AppendArgNative(value);
michael@0 378 #endif
michael@0 379 }
michael@0 380
michael@0 381 void CommandLine::AppendArgPath(const FilePath& path) {
michael@0 382 AppendArgNative(path.value());
michael@0 383 }
michael@0 384
michael@0 385 void CommandLine::AppendArgNative(const CommandLine::StringType& value) {
michael@0 386 argv_.push_back(value);
michael@0 387 }
michael@0 388
michael@0 389 void CommandLine::AppendArguments(const CommandLine& other,
michael@0 390 bool include_program) {
michael@0 391 if (include_program)
michael@0 392 SetProgram(other.GetProgram());
michael@0 393 AppendSwitchesAndArguments(*this, other.argv());
michael@0 394 }
michael@0 395
michael@0 396 void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) {
michael@0 397 if (wrapper.empty())
michael@0 398 return;
michael@0 399 // The wrapper may have embedded arguments (like "gdb --args"). In this case,
michael@0 400 // we don't pretend to do anything fancy, we just split on spaces.
michael@0 401 StringVector wrapper_argv;
michael@0 402 base::SplitString(wrapper, FILE_PATH_LITERAL(' '), &wrapper_argv);
michael@0 403 // Prepend the wrapper and update the switches/arguments |begin_args_|.
michael@0 404 argv_.insert(argv_.begin(), wrapper_argv.begin(), wrapper_argv.end());
michael@0 405 begin_args_ += wrapper_argv.size();
michael@0 406 }
michael@0 407
michael@0 408 #if defined(OS_WIN)
michael@0 409 void CommandLine::ParseFromString(const std::wstring& command_line) {
michael@0 410 std::wstring command_line_string;
michael@0 411 TrimWhitespace(command_line, TRIM_ALL, &command_line_string);
michael@0 412 if (command_line_string.empty())
michael@0 413 return;
michael@0 414
michael@0 415 int num_args = 0;
michael@0 416 wchar_t** args = NULL;
michael@0 417 args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args);
michael@0 418
michael@0 419 DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: "
michael@0 420 << command_line;
michael@0 421 InitFromArgv(num_args, args);
michael@0 422 LocalFree(args);
michael@0 423 }
michael@0 424 #endif

mercurial