1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/command_line.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,178 @@ 1.4 +// Copyright (c) 2012 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +// This class works with command lines: building and parsing. 1.9 +// Arguments with prefixes ('--', '-', and on Windows, '/') are switches. 1.10 +// Switches will precede all other arguments without switch prefixes. 1.11 +// Switches can optionally have values, delimited by '=', e.g., "-switch=value". 1.12 +// An argument of "--" will terminate switch parsing during initialization, 1.13 +// interpreting subsequent tokens as non-switch arguments, regardless of prefix. 1.14 + 1.15 +// There is a singleton read-only CommandLine that represents the command line 1.16 +// that the current process was started with. It must be initialized in main(). 1.17 + 1.18 +#ifndef BASE_COMMAND_LINE_H_ 1.19 +#define BASE_COMMAND_LINE_H_ 1.20 + 1.21 +#include <stddef.h> 1.22 +#include <map> 1.23 +#include <string> 1.24 +#include <vector> 1.25 + 1.26 +#include "base/base_export.h" 1.27 +#include "build/build_config.h" 1.28 + 1.29 +namespace base { 1.30 +class FilePath; 1.31 +} 1.32 + 1.33 +class BASE_EXPORT CommandLine { 1.34 + public: 1.35 +#if defined(OS_WIN) 1.36 + // The native command line string type. 1.37 + typedef std::wstring StringType; 1.38 +#elif defined(OS_POSIX) 1.39 + typedef std::string StringType; 1.40 +#endif 1.41 + 1.42 + typedef StringType::value_type CharType; 1.43 + typedef std::vector<StringType> StringVector; 1.44 + typedef std::map<std::string, StringType> SwitchMap; 1.45 + 1.46 + // A constructor for CommandLines that only carry switches and arguments. 1.47 + enum NoProgram { NO_PROGRAM }; 1.48 + explicit CommandLine(NoProgram no_program); 1.49 + 1.50 + // Construct a new command line with |program| as argv[0]. 1.51 + explicit CommandLine(const base::FilePath& program); 1.52 + 1.53 + // Construct a new command line from an argument list. 1.54 + CommandLine(int argc, const CharType* const* argv); 1.55 + explicit CommandLine(const StringVector& argv); 1.56 + 1.57 + ~CommandLine(); 1.58 + 1.59 + // Initialize the current process CommandLine singleton. On Windows, ignores 1.60 + // its arguments (we instead parse GetCommandLineW() directly) because we 1.61 + // don't trust the CRT's parsing of the command line, but it still must be 1.62 + // called to set up the command line. Returns false if initialization has 1.63 + // already occurred, and true otherwise. Only the caller receiving a 'true' 1.64 + // return value should take responsibility for calling Reset. 1.65 + static bool Init(int argc, const char* const* argv); 1.66 + 1.67 + // Destroys the current process CommandLine singleton. This is necessary if 1.68 + // you want to reset the base library to its initial state (for example, in an 1.69 + // outer library that needs to be able to terminate, and be re-initialized). 1.70 + // If Init is called only once, as in main(), Reset() is not necessary. 1.71 + static void Reset(); 1.72 + 1.73 + // Get the singleton CommandLine representing the current process's 1.74 + // command line. Note: returned value is mutable, but not thread safe; 1.75 + // only mutate if you know what you're doing! 1.76 + static CommandLine* ForCurrentProcess(); 1.77 + 1.78 + // Returns true if the CommandLine has been initialized for the given process. 1.79 + static bool InitializedForCurrentProcess(); 1.80 + 1.81 +#if defined(OS_WIN) 1.82 + static CommandLine FromString(const std::wstring& command_line); 1.83 +#endif 1.84 + 1.85 + // Initialize from an argv vector. 1.86 + void InitFromArgv(int argc, const CharType* const* argv); 1.87 + void InitFromArgv(const StringVector& argv); 1.88 + 1.89 + // Constructs and returns the represented command line string. 1.90 + // CAUTION! This should be avoided on POSIX because quoting behavior is 1.91 + // unclear. 1.92 + StringType GetCommandLineString() const; 1.93 + 1.94 + // Constructs and returns the represented arguments string. 1.95 + // CAUTION! This should be avoided on POSIX because quoting behavior is 1.96 + // unclear. 1.97 + StringType GetArgumentsString() const; 1.98 + 1.99 + // Returns the original command line string as a vector of strings. 1.100 + const StringVector& argv() const { return argv_; } 1.101 + 1.102 + // Get and Set the program part of the command line string (the first item). 1.103 + base::FilePath GetProgram() const; 1.104 + void SetProgram(const base::FilePath& program); 1.105 + 1.106 + // Returns true if this command line contains the given switch. 1.107 + // (Switch names are case-insensitive). 1.108 + bool HasSwitch(const std::string& switch_string) const; 1.109 + 1.110 + // Returns the value associated with the given switch. If the switch has no 1.111 + // value or isn't present, this method returns the empty string. 1.112 + std::string GetSwitchValueASCII(const std::string& switch_string) const; 1.113 + base::FilePath GetSwitchValuePath(const std::string& switch_string) const; 1.114 + StringType GetSwitchValueNative(const std::string& switch_string) const; 1.115 + 1.116 + // Get a copy of all switches, along with their values. 1.117 + const SwitchMap& GetSwitches() const { return switches_; } 1.118 + 1.119 + // Append a switch [with optional value] to the command line. 1.120 + // Note: Switches will precede arguments regardless of appending order. 1.121 + void AppendSwitch(const std::string& switch_string); 1.122 + void AppendSwitchPath(const std::string& switch_string, 1.123 + const base::FilePath& path); 1.124 + void AppendSwitchNative(const std::string& switch_string, 1.125 + const StringType& value); 1.126 + void AppendSwitchASCII(const std::string& switch_string, 1.127 + const std::string& value); 1.128 + 1.129 + // Copy a set of switches (and any values) from another command line. 1.130 + // Commonly used when launching a subprocess. 1.131 + void CopySwitchesFrom(const CommandLine& source, 1.132 + const char* const switches[], 1.133 + size_t count); 1.134 + 1.135 + // Get the remaining arguments to the command. 1.136 + StringVector GetArgs() const; 1.137 + 1.138 + // Append an argument to the command line. Note that the argument is quoted 1.139 + // properly such that it is interpreted as one argument to the target command. 1.140 + // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8. 1.141 + // Note: Switches will precede arguments regardless of appending order. 1.142 + void AppendArg(const std::string& value); 1.143 + void AppendArgPath(const base::FilePath& value); 1.144 + void AppendArgNative(const StringType& value); 1.145 + 1.146 + // Append the switches and arguments from another command line to this one. 1.147 + // If |include_program| is true, include |other|'s program as well. 1.148 + void AppendArguments(const CommandLine& other, bool include_program); 1.149 + 1.150 + // Insert a command before the current command. 1.151 + // Common for debuggers, like "valgrind" or "gdb --args". 1.152 + void PrependWrapper(const StringType& wrapper); 1.153 + 1.154 +#if defined(OS_WIN) 1.155 + // Initialize by parsing the given command line string. 1.156 + // The program name is assumed to be the first item in the string. 1.157 + void ParseFromString(const std::wstring& command_line); 1.158 +#endif 1.159 + 1.160 + private: 1.161 + // Disallow default constructor; a program name must be explicitly specified. 1.162 + CommandLine(); 1.163 + // Allow the copy constructor. A common pattern is to copy of the current 1.164 + // process's command line and then add some flags to it. For example: 1.165 + // CommandLine cl(*CommandLine::ForCurrentProcess()); 1.166 + // cl.AppendSwitch(...); 1.167 + 1.168 + // The singleton CommandLine representing the current process's command line. 1.169 + static CommandLine* current_process_commandline_; 1.170 + 1.171 + // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } 1.172 + StringVector argv_; 1.173 + 1.174 + // Parsed-out switch keys and values. 1.175 + SwitchMap switches_; 1.176 + 1.177 + // The index after the program and switches, any arguments start here. 1.178 + size_t begin_args_; 1.179 +}; 1.180 + 1.181 +#endif // BASE_COMMAND_LINE_H_