security/sandbox/chromium/base/command_line.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 // This class works with command lines: building and parsing.
michael@0 6 // Arguments with prefixes ('--', '-', and on Windows, '/') are switches.
michael@0 7 // Switches will precede all other arguments without switch prefixes.
michael@0 8 // Switches can optionally have values, delimited by '=', e.g., "-switch=value".
michael@0 9 // An argument of "--" will terminate switch parsing during initialization,
michael@0 10 // interpreting subsequent tokens as non-switch arguments, regardless of prefix.
michael@0 11
michael@0 12 // There is a singleton read-only CommandLine that represents the command line
michael@0 13 // that the current process was started with. It must be initialized in main().
michael@0 14
michael@0 15 #ifndef BASE_COMMAND_LINE_H_
michael@0 16 #define BASE_COMMAND_LINE_H_
michael@0 17
michael@0 18 #include <stddef.h>
michael@0 19 #include <map>
michael@0 20 #include <string>
michael@0 21 #include <vector>
michael@0 22
michael@0 23 #include "base/base_export.h"
michael@0 24 #include "build/build_config.h"
michael@0 25
michael@0 26 namespace base {
michael@0 27 class FilePath;
michael@0 28 }
michael@0 29
michael@0 30 class BASE_EXPORT CommandLine {
michael@0 31 public:
michael@0 32 #if defined(OS_WIN)
michael@0 33 // The native command line string type.
michael@0 34 typedef std::wstring StringType;
michael@0 35 #elif defined(OS_POSIX)
michael@0 36 typedef std::string StringType;
michael@0 37 #endif
michael@0 38
michael@0 39 typedef StringType::value_type CharType;
michael@0 40 typedef std::vector<StringType> StringVector;
michael@0 41 typedef std::map<std::string, StringType> SwitchMap;
michael@0 42
michael@0 43 // A constructor for CommandLines that only carry switches and arguments.
michael@0 44 enum NoProgram { NO_PROGRAM };
michael@0 45 explicit CommandLine(NoProgram no_program);
michael@0 46
michael@0 47 // Construct a new command line with |program| as argv[0].
michael@0 48 explicit CommandLine(const base::FilePath& program);
michael@0 49
michael@0 50 // Construct a new command line from an argument list.
michael@0 51 CommandLine(int argc, const CharType* const* argv);
michael@0 52 explicit CommandLine(const StringVector& argv);
michael@0 53
michael@0 54 ~CommandLine();
michael@0 55
michael@0 56 // Initialize the current process CommandLine singleton. On Windows, ignores
michael@0 57 // its arguments (we instead parse GetCommandLineW() directly) because we
michael@0 58 // don't trust the CRT's parsing of the command line, but it still must be
michael@0 59 // called to set up the command line. Returns false if initialization has
michael@0 60 // already occurred, and true otherwise. Only the caller receiving a 'true'
michael@0 61 // return value should take responsibility for calling Reset.
michael@0 62 static bool Init(int argc, const char* const* argv);
michael@0 63
michael@0 64 // Destroys the current process CommandLine singleton. This is necessary if
michael@0 65 // you want to reset the base library to its initial state (for example, in an
michael@0 66 // outer library that needs to be able to terminate, and be re-initialized).
michael@0 67 // If Init is called only once, as in main(), Reset() is not necessary.
michael@0 68 static void Reset();
michael@0 69
michael@0 70 // Get the singleton CommandLine representing the current process's
michael@0 71 // command line. Note: returned value is mutable, but not thread safe;
michael@0 72 // only mutate if you know what you're doing!
michael@0 73 static CommandLine* ForCurrentProcess();
michael@0 74
michael@0 75 // Returns true if the CommandLine has been initialized for the given process.
michael@0 76 static bool InitializedForCurrentProcess();
michael@0 77
michael@0 78 #if defined(OS_WIN)
michael@0 79 static CommandLine FromString(const std::wstring& command_line);
michael@0 80 #endif
michael@0 81
michael@0 82 // Initialize from an argv vector.
michael@0 83 void InitFromArgv(int argc, const CharType* const* argv);
michael@0 84 void InitFromArgv(const StringVector& argv);
michael@0 85
michael@0 86 // Constructs and returns the represented command line string.
michael@0 87 // CAUTION! This should be avoided on POSIX because quoting behavior is
michael@0 88 // unclear.
michael@0 89 StringType GetCommandLineString() const;
michael@0 90
michael@0 91 // Constructs and returns the represented arguments string.
michael@0 92 // CAUTION! This should be avoided on POSIX because quoting behavior is
michael@0 93 // unclear.
michael@0 94 StringType GetArgumentsString() const;
michael@0 95
michael@0 96 // Returns the original command line string as a vector of strings.
michael@0 97 const StringVector& argv() const { return argv_; }
michael@0 98
michael@0 99 // Get and Set the program part of the command line string (the first item).
michael@0 100 base::FilePath GetProgram() const;
michael@0 101 void SetProgram(const base::FilePath& program);
michael@0 102
michael@0 103 // Returns true if this command line contains the given switch.
michael@0 104 // (Switch names are case-insensitive).
michael@0 105 bool HasSwitch(const std::string& switch_string) const;
michael@0 106
michael@0 107 // Returns the value associated with the given switch. If the switch has no
michael@0 108 // value or isn't present, this method returns the empty string.
michael@0 109 std::string GetSwitchValueASCII(const std::string& switch_string) const;
michael@0 110 base::FilePath GetSwitchValuePath(const std::string& switch_string) const;
michael@0 111 StringType GetSwitchValueNative(const std::string& switch_string) const;
michael@0 112
michael@0 113 // Get a copy of all switches, along with their values.
michael@0 114 const SwitchMap& GetSwitches() const { return switches_; }
michael@0 115
michael@0 116 // Append a switch [with optional value] to the command line.
michael@0 117 // Note: Switches will precede arguments regardless of appending order.
michael@0 118 void AppendSwitch(const std::string& switch_string);
michael@0 119 void AppendSwitchPath(const std::string& switch_string,
michael@0 120 const base::FilePath& path);
michael@0 121 void AppendSwitchNative(const std::string& switch_string,
michael@0 122 const StringType& value);
michael@0 123 void AppendSwitchASCII(const std::string& switch_string,
michael@0 124 const std::string& value);
michael@0 125
michael@0 126 // Copy a set of switches (and any values) from another command line.
michael@0 127 // Commonly used when launching a subprocess.
michael@0 128 void CopySwitchesFrom(const CommandLine& source,
michael@0 129 const char* const switches[],
michael@0 130 size_t count);
michael@0 131
michael@0 132 // Get the remaining arguments to the command.
michael@0 133 StringVector GetArgs() const;
michael@0 134
michael@0 135 // Append an argument to the command line. Note that the argument is quoted
michael@0 136 // properly such that it is interpreted as one argument to the target command.
michael@0 137 // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8.
michael@0 138 // Note: Switches will precede arguments regardless of appending order.
michael@0 139 void AppendArg(const std::string& value);
michael@0 140 void AppendArgPath(const base::FilePath& value);
michael@0 141 void AppendArgNative(const StringType& value);
michael@0 142
michael@0 143 // Append the switches and arguments from another command line to this one.
michael@0 144 // If |include_program| is true, include |other|'s program as well.
michael@0 145 void AppendArguments(const CommandLine& other, bool include_program);
michael@0 146
michael@0 147 // Insert a command before the current command.
michael@0 148 // Common for debuggers, like "valgrind" or "gdb --args".
michael@0 149 void PrependWrapper(const StringType& wrapper);
michael@0 150
michael@0 151 #if defined(OS_WIN)
michael@0 152 // Initialize by parsing the given command line string.
michael@0 153 // The program name is assumed to be the first item in the string.
michael@0 154 void ParseFromString(const std::wstring& command_line);
michael@0 155 #endif
michael@0 156
michael@0 157 private:
michael@0 158 // Disallow default constructor; a program name must be explicitly specified.
michael@0 159 CommandLine();
michael@0 160 // Allow the copy constructor. A common pattern is to copy of the current
michael@0 161 // process's command line and then add some flags to it. For example:
michael@0 162 // CommandLine cl(*CommandLine::ForCurrentProcess());
michael@0 163 // cl.AppendSwitch(...);
michael@0 164
michael@0 165 // The singleton CommandLine representing the current process's command line.
michael@0 166 static CommandLine* current_process_commandline_;
michael@0 167
michael@0 168 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* }
michael@0 169 StringVector argv_;
michael@0 170
michael@0 171 // Parsed-out switch keys and values.
michael@0 172 SwitchMap switches_;
michael@0 173
michael@0 174 // The index after the program and switches, any arguments start here.
michael@0 175 size_t begin_args_;
michael@0 176 };
michael@0 177
michael@0 178 #endif // BASE_COMMAND_LINE_H_

mercurial