xref: /aosp_15_r20/external/cronet/base/command_line.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // This class works with command lines: building and parsing.
6 // Arguments with prefixes ('--', '-', and on Windows, '/') are switches.
7 // Switches will precede all other arguments without switch prefixes.
8 // Switches can optionally have values, delimited by '=', e.g., "-switch=value".
9 // If a switch is specified multiple times, only the last value is used.
10 // An argument of "--" will terminate switch parsing during initialization,
11 // interpreting subsequent tokens as non-switch arguments, regardless of prefix.
12 
13 // There is a singleton read-only CommandLine that represents the command line
14 // that the current process was started with.  It must be initialized in main().
15 
16 #ifndef BASE_COMMAND_LINE_H_
17 #define BASE_COMMAND_LINE_H_
18 
19 #include <stddef.h>
20 
21 #include <functional>
22 #include <map>
23 #include <memory>
24 #include <string>
25 #include <string_view>
26 #include <vector>
27 
28 #include "base/base_export.h"
29 #include "base/containers/span.h"
30 #include "base/debug/debugging_buildflags.h"
31 #include "build/build_config.h"
32 
33 #if BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS)
34 #include "base/sequence_checker.h"
35 #endif  // BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS)
36 
37 namespace base {
38 
39 class DuplicateSwitchHandler;
40 class FilePath;
41 
42 class BASE_EXPORT CommandLine {
43  public:
44 #if BUILDFLAG(IS_WIN)
45   // The native command line string type.
46   using StringType = std::wstring;
47 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
48   using StringType = std::string;
49 #endif
50 
51   using CharType = StringType::value_type;
52   using StringPieceType = std::basic_string_view<CharType>;
53   using StringVector = std::vector<StringType>;
54   using SwitchMap = std::map<std::string, StringType, std::less<>>;
55 
56   // Returns CommandLine object constructed with switches and keys alone.
57   // NOTE: `argv` must NOT include the program path, and the switch arguments
58   // must start from the index 0.
59   static CommandLine FromArgvWithoutProgram(const StringVector& argv);
60 
61 #if BUILDFLAG(IS_WIN)
62   static CommandLine FromString(StringPieceType command_line);
63 #endif
64 
65   // A constructor for CommandLines that only carry switches and arguments.
66   enum NoProgram { NO_PROGRAM };
67   explicit CommandLine(NoProgram no_program);
68 
69   // Construct a new command line with |program| as argv[0].
70   explicit CommandLine(const FilePath& program);
71 
72   // Construct a new command line from an argument list.
73   CommandLine(int argc, const CharType* const* argv);
74   explicit CommandLine(const StringVector& argv);
75 
76   // Allow the copy constructor. A common pattern is to copy of the current
77   // process's command line and then add some flags to it. For example:
78   //   CommandLine cl(*CommandLine::ForCurrentProcess());
79   //   cl.AppendSwitch(...);
80   CommandLine(const CommandLine& other);
81   CommandLine& operator=(const CommandLine& other);
82 
83   CommandLine(CommandLine&& other) noexcept;
84   CommandLine& operator=(CommandLine&& other) noexcept;
85 
86   ~CommandLine();
87 
88 #if BUILDFLAG(IS_WIN)
89   // By default this class will treat command-line arguments beginning with
90   // slashes as switches on Windows, but not other platforms.
91   //
92   // If this behavior is inappropriate for your application, you can call this
93   // function BEFORE initializing the current process' global command line
94   // object and the behavior will be the same as Posix systems (only hyphens
95   // begin switches, everything else will be an arg).
96   static void set_slash_is_not_a_switch();
97 
98   // Normally when the CommandLine singleton is initialized it gets the command
99   // line via the GetCommandLineW API and then uses the shell32 API
100   // CommandLineToArgvW to parse the command line and convert it back to
101   // argc and argv. Tests who don't want this dependency on shell32 and need
102   // to honor the arguments passed in should use this function.
103   static void InitUsingArgvForTesting(int argc, const char* const* argv);
104 #endif
105 
106   // Initialize the current process CommandLine singleton. On Windows, ignores
107   // its arguments (we instead parse GetCommandLineW() directly) because we
108   // don't trust the CRT's parsing of the command line, but it still must be
109   // called to set up the command line. Returns false if initialization has
110   // already occurred, and true otherwise. Only the caller receiving a 'true'
111   // return value should take responsibility for calling Reset.
112   static bool Init(int argc, const char* const* argv);
113 
114   // Destroys the current process CommandLine singleton. This is necessary if
115   // you want to reset the base library to its initial state (for example, in an
116   // outer library that needs to be able to terminate, and be re-initialized).
117   // If Init is called only once, as in main(), Reset() is not necessary.
118   // Do not call this in tests. Use base::test::ScopedCommandLine instead.
119   static void Reset();
120 
121   // Get the singleton CommandLine representing the current process's
122   // command line. Note: returned value is mutable, but not thread safe;
123   // only mutate if you know what you're doing!
124   static CommandLine* ForCurrentProcess();
125 
126   // Returns true if the CommandLine has been initialized for the given process.
127   static bool InitializedForCurrentProcess();
128 
129   // Initialize from an argv vector.
130   void InitFromArgv(int argc, const CharType* const* argv);
131   void InitFromArgv(const StringVector& argv);
132 
133   // Constructs and returns the represented command line string.
134   // CAUTION! This should be avoided on POSIX because quoting behavior is
135   // unclear.
136   // CAUTION! If writing a command line to the Windows registry, use
137   // GetCommandLineStringForShell() instead.
138   StringType GetCommandLineString() const;
139 
140 #if BUILDFLAG(IS_WIN)
141   // Quotes and escapes `arg` if necessary so that it will be interpreted as a
142   // single command-line parameter according to the following rules in line with
143   // `::CommandLineToArgvW` and C++ `main`:
144   // * Returns `arg` unchanged if `arg` does not include any characters that may
145   // need encoding, which is spaces, tabs, backslashes, and double-quotes.
146   // * Otherwise, double-quotes `arg` and in addition:
147   //   * Escapes any double-quotes in `arg` with backslashes.
148   //   * Escapes backslashes in `arg` if:
149   //     * `arg` ends with backslashes , or
150   //     * the backslashes end in a pre-existing double quote.
151   //
152   // https://learn.microsoft.com/en-us/search/?terms=CommandLineToArgvW and
153   // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx#parsing-c-command-line-arguments.
154   static std::wstring QuoteForCommandLineToArgvW(const std::wstring& arg);
155 
156   // Returns the command-line string in the proper format for the Windows shell,
157   // ending with the argument placeholder "--single-argument %1". The single-
158   // argument switch prevents unexpected parsing of arguments from other
159   // software that cannot be trusted to escape double quotes when substituting
160   // into a placeholder (e.g., "%1" insert sequences populated by the Windows
161   // shell).
162   // NOTE: this must be used to generate the command-line string for the shell
163   // even if this command line was parsed from a string with the proper syntax,
164   // because the --single-argument switch is not preserved during parsing.
165   StringType GetCommandLineStringForShell() const;
166 
167   // Returns the represented command-line string. Allows the use of unsafe
168   // Windows insert sequences like "%1". Only use this method if
169   // GetCommandLineStringForShell() is not adequate AND the processor inserting
170   // the arguments is known to do so securely (i.e., is not the Windows shell).
171   // If in doubt, do not use.
172   StringType GetCommandLineStringWithUnsafeInsertSequences() const;
173 #endif
174 
175   // Constructs and returns the represented arguments string.
176   // CAUTION! This should be avoided on POSIX because quoting behavior is
177   // unclear.
178   StringType GetArgumentsString() const;
179 
180   // Returns the original command line string as a vector of strings.
argv()181   const StringVector& argv() const { return argv_; }
182 
183   // Get and Set the program part of the command line string (the first item).
184   FilePath GetProgram() const;
185   void SetProgram(const FilePath& program);
186 
187   // Returns true if this command line contains the given switch.
188   // Switch names must be lowercase.
189   // The second override provides an optimized version to avoid inlining codegen
190   // at every callsite to find the length of the constant and construct a
191   // std::string_view.
192   bool HasSwitch(std::string_view switch_string) const;
193   bool HasSwitch(const char switch_constant[]) const;
194 
195   // Returns the value associated with the given switch. If the switch has no
196   // value or isn't present, this method returns the empty string.
197   // Switch names must be lowercase.
198   std::string GetSwitchValueASCII(std::string_view switch_string) const;
199   FilePath GetSwitchValuePath(std::string_view switch_string) const;
200   StringType GetSwitchValueNative(std::string_view switch_string) const;
201 
202   // Get a copy of all switches, along with their values.
GetSwitches()203   const SwitchMap& GetSwitches() const { return switches_; }
204 
205   // Append a switch [with optional value] to the command line.
206   // Note: Switches will precede arguments regardless of appending order.
207   void AppendSwitch(std::string_view switch_string);
208   void AppendSwitchPath(std::string_view switch_string, const FilePath& path);
209   void AppendSwitchNative(std::string_view switch_string,
210                           StringPieceType value);
211   void AppendSwitchASCII(std::string_view switch_string,
212                          std::string_view value);
213 
214   // Removes the switch that matches |switch_key_without_prefix|, regardless of
215   // prefix and value. If no such switch is present, this has no effect.
216   void RemoveSwitch(std::string_view switch_key_without_prefix);
217 
218   // Copies a set of switches (and any values) from another command line.
219   // Commonly used when launching a subprocess.
220   // If an entry in `switches` does not exist in `source`, then it is ignored.
221   void CopySwitchesFrom(const CommandLine& source,
222                         span<const char* const> switches);
223 
224   // Get the remaining arguments to the command.
225   StringVector GetArgs() const;
226 
227   // Append an argument to the command line. Note that the argument is quoted
228   // properly such that it is interpreted as one argument to the target command.
229   // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8.
230   // Note: Switches will precede arguments regardless of appending order.
231   void AppendArg(std::string_view value);
232   void AppendArgPath(const FilePath& value);
233   void AppendArgNative(StringPieceType value);
234 
235   // Append the switches and arguments from another command line to this one.
236   // If `include_program` is true, program will be overwritten by other's.
237   void AppendArguments(const CommandLine& other, bool include_program);
238 
239   // Insert a command before the current command.
240   // Common for debuggers, like "gdb --args".
241   void PrependWrapper(StringPieceType wrapper);
242 
243 #if BUILDFLAG(IS_WIN)
244   // Initialize by parsing the given command line string.
245   // The program name is assumed to be the first item in the string.
246   void ParseFromString(StringPieceType command_line);
247 
248   // Returns true if the command line had the --single-argument switch, and
249   // thus likely came from a Windows shell registration. This is only set if the
250   // command line is parsed, and is not changed after it is parsed.
HasSingleArgumentSwitch()251   bool HasSingleArgumentSwitch() const { return has_single_argument_switch_; }
252 #endif
253 
254   // Detaches this object from the current sequence in preparation for a move to
255   // a different sequence.
256   void DetachFromCurrentSequence();
257 
258   // Sets a delegate that's called when we encounter a duplicate switch
259   static void SetDuplicateSwitchHandler(
260       std::unique_ptr<DuplicateSwitchHandler>);
261 
262  private:
263 #if BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS)
264   // A helper class that encapsulates a SEQUENCE_CHECKER but allows copy.
265   // Copying this class will detach the sequence checker from the owning object.
266   class InstanceBoundSequenceChecker {
267    public:
268     InstanceBoundSequenceChecker() = default;
269 
InstanceBoundSequenceChecker(const InstanceBoundSequenceChecker & other)270     InstanceBoundSequenceChecker(const InstanceBoundSequenceChecker& other) {}
271 
272     InstanceBoundSequenceChecker& operator=(
273         const InstanceBoundSequenceChecker& other) {
274       return *this;
275     }
276 
277     // Allow move as per SequenceChecker.
278     InstanceBoundSequenceChecker(InstanceBoundSequenceChecker&&) = default;
279     InstanceBoundSequenceChecker& operator=(InstanceBoundSequenceChecker&&) =
280         default;
281 
Detach()282     void Detach() { DETACH_FROM_SEQUENCE(sequence_checker_); }
Check()283     void Check() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); }
284 
285    private:
286     SEQUENCE_CHECKER(sequence_checker_);
287   };
288 #endif  // BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS)
289 
290   // Disallow default constructor; a program name must be explicitly specified.
291   CommandLine() = delete;
292 
293   // Append switches and arguments, keeping switches before arguments.
294   // NOTE: `argv` should not include the "program" element.
295   void AppendSwitchesAndArguments(span<const StringType> argv);
296 
297   // Internal version of GetArgumentsString to support allowing unsafe insert
298   // sequences in rare cases (see
299   // GetCommandLineStringWithUnsafeInsertSequences).
300   StringType GetArgumentsStringInternal(
301       bool allow_unsafe_insert_sequences) const;
302 
303 #if BUILDFLAG(IS_WIN)
304   // Initializes by parsing |raw_command_line_string_|, treating everything
305   // after |single_arg_switch_string| + <a single character> as the command
306   // line's single argument, and dropping any arguments previously parsed. The
307   // command line must contain |single_arg_switch_string|, and the argument, if
308   // present, must be separated from |single_arg_switch_string| by one
309   // character.
310   // NOTE: the single-argument switch is not preserved after parsing;
311   // GetCommandLineStringForShell() must be used to reproduce the original
312   // command-line string with single-argument switch.
313   void ParseAsSingleArgument(const StringType& single_arg_switch_string);
314 
315   // The string returned by GetCommandLineW(), to be parsed via
316   // ParseFromString(). Empty if this command line was not parsed from a string,
317   // or if ParseFromString() has finished executing.
318   StringPieceType raw_command_line_string_;
319 
320   // Set to true if the command line had --single-argument when initially
321   // parsed. It does not change if the command line mutates after initial
322   // parsing.
323   bool has_single_argument_switch_ = false;
324 #endif
325 
326   // The singleton CommandLine representing the current process's command line.
327   static CommandLine* current_process_commandline_;
328 
329   // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* }
330   StringVector argv_;
331 
332   // Parsed-out switch keys and values.
333   SwitchMap switches_;
334 
335   // The index after the program and switches, any arguments start here.
336   ptrdiff_t begin_args_;
337 
338 #if BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS)
339   InstanceBoundSequenceChecker sequence_checker_;
340 #endif
341 };
342 
343 class BASE_EXPORT DuplicateSwitchHandler {
344  public:
345   // out_value contains the existing value of the switch
346   virtual void ResolveDuplicate(std::string_view key,
347                                 CommandLine::StringPieceType new_value,
348                                 CommandLine::StringType& out_value) = 0;
349   virtual ~DuplicateSwitchHandler() = default;
350 };
351 
352 }  // namespace base
353 
354 #endif  // BASE_COMMAND_LINE_H_
355