xref: /third_party/gn/src/base/command_line.cc (revision 6d528ed9)
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.
4
5#include "base/command_line.h"
6
7#include <algorithm>
8#include <iterator>
9#include <ostream>
10#include <string_view>
11
12#include "base/files/file_path.h"
13#include "base/logging.h"
14#include "base/stl_util.h"
15#include "base/strings/string_split.h"
16#include "base/strings/string_tokenizer.h"
17#include "base/strings/string_util.h"
18#include "base/strings/utf_string_conversions.h"
19#include "util/build_config.h"
20
21#if defined(OS_WIN)
22#include <windows.h>
23
24#include <shellapi.h>
25#endif
26
27namespace base {
28
29CommandLine* CommandLine::current_process_commandline_ = nullptr;
30
31namespace {
32
33const CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--");
34const CommandLine::CharType kSwitchValueSeparator[] = FILE_PATH_LITERAL("=");
35
36// Since we use a lazy match, make sure that longer versions (like "--") are
37// listed before shorter versions (like "-") of similar prefixes.
38#if defined(OS_WIN)
39// By putting slash last, we can control whether it is treaded as a switch
40// value by changing the value of switch_prefix_count to be one less than
41// the array size.
42const CommandLine::CharType* const kSwitchPrefixes[] = {u"--", u"-", u"/"};
43#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
44// Unixes don't use slash as a switch.
45const CommandLine::CharType* const kSwitchPrefixes[] = {"--", "-"};
46#endif
47size_t switch_prefix_count = std::size(kSwitchPrefixes);
48
49size_t GetSwitchPrefixLength(const CommandLine::StringType& string) {
50  for (size_t i = 0; i < switch_prefix_count; ++i) {
51    CommandLine::StringType prefix(kSwitchPrefixes[i]);
52    if (string.compare(0, prefix.length(), prefix) == 0)
53      return prefix.length();
54  }
55  return 0;
56}
57
58// Fills in |switch_string| and |switch_value| if |string| is a switch.
59// This will preserve the input switch prefix in the output |switch_string|.
60bool IsSwitch(const CommandLine::StringType& string,
61              CommandLine::StringType* switch_string,
62              CommandLine::StringType* switch_value) {
63  switch_string->clear();
64  switch_value->clear();
65  size_t prefix_length = GetSwitchPrefixLength(string);
66  if (prefix_length == 0 || prefix_length == string.length())
67    return false;
68
69  const size_t equals_position = string.find(kSwitchValueSeparator);
70  *switch_string = string.substr(0, equals_position);
71  if (equals_position != CommandLine::StringType::npos)
72    *switch_value = string.substr(equals_position + 1);
73  return true;
74}
75
76// Append switches and arguments, keeping switches before arguments
77// if handle_switches is true.
78void AppendSwitchesAndArguments(CommandLine* command_line,
79                                const CommandLine::StringVector& argv,
80                                bool handle_switches) {
81  bool parse_switches = handle_switches;
82  for (size_t i = 1; i < argv.size(); ++i) {
83    CommandLine::StringType arg = argv[i];
84#if defined(OS_WIN)
85    TrimWhitespace(arg, TRIM_ALL, &arg);
86#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
87    TrimWhitespaceASCII(arg, TRIM_ALL, &arg);
88#endif
89
90    CommandLine::StringType switch_string;
91    CommandLine::StringType switch_value;
92    parse_switches &= (arg != kSwitchTerminator);
93    if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) {
94#if defined(OS_WIN)
95      command_line->AppendSwitchNative(UTF16ToASCII(switch_string),
96                                       switch_value);
97#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
98      command_line->AppendSwitchNative(switch_string, switch_value);
99#else
100#error Unsupported platform
101#endif
102    } else {
103      command_line->AppendArgNative(arg);
104    }
105  }
106}
107
108#if defined(OS_WIN)
109// Quote a string as necessary for CommandLineToArgvW compatibility *on
110// Windows*.
111std::u16string QuoteForCommandLineToArgvW(const std::u16string& arg,
112                                          bool quote_placeholders) {
113  // We follow the quoting rules of CommandLineToArgvW.
114  // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
115  std::u16string quotable_chars(u" \\\"");
116  // We may also be required to quote '%', which is commonly used in a command
117  // line as a placeholder. (It may be substituted for a string with spaces.)
118  if (quote_placeholders)
119    quotable_chars.push_back('%');
120  if (arg.find_first_of(quotable_chars) == std::u16string::npos) {
121    // No quoting necessary.
122    return arg;
123  }
124
125  std::u16string out;
126  out.push_back('"');
127  for (size_t i = 0; i < arg.size(); ++i) {
128    if (arg[i] == '\\') {
129      // Find the extent of this run of backslashes.
130      size_t start = i, end = start + 1;
131      for (; end < arg.size() && arg[end] == '\\'; ++end) {
132      }
133      size_t backslash_count = end - start;
134
135      // Backslashes are escapes only if the run is followed by a double quote.
136      // Since we also will end the string with a double quote, we escape for
137      // either a double quote or the end of the string.
138      if (end == arg.size() || arg[end] == '"') {
139        // To quote, we need to output 2x as many backslashes.
140        backslash_count *= 2;
141      }
142      for (size_t j = 0; j < backslash_count; ++j)
143        out.push_back('\\');
144
145      // Advance i to one before the end to balance i++ in loop.
146      i = end - 1;
147    } else if (arg[i] == '"') {
148      out.push_back('\\');
149      out.push_back('"');
150    } else {
151      out.push_back(arg[i]);
152    }
153  }
154  out.push_back('"');
155
156  return out;
157}
158#endif
159
160}  // namespace
161
162CommandLine::CommandLine(NoProgram no_program)
163    : argv_(1), begin_args_(1), parse_switches_(true) {}
164
165CommandLine::CommandLine(const FilePath& program)
166    : argv_(1), begin_args_(1), parse_switches_(true) {
167  SetProgram(program);
168}
169
170CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv)
171    : argv_(1), begin_args_(1), parse_switches_(true) {
172  InitFromArgv(argc, argv);
173}
174
175CommandLine::CommandLine(const StringVector& argv)
176    : argv_(1), begin_args_(1), parse_switches_(true) {
177  InitFromArgv(argv);
178}
179
180CommandLine::CommandLine(const CommandLine& other) = default;
181
182CommandLine& CommandLine::operator=(const CommandLine& other) = default;
183
184CommandLine::~CommandLine() = default;
185
186#if defined(OS_WIN)
187
188// static
189std::string CommandLine::StringTypeToUTF8(const StringType& input) {
190  return UTF16ToUTF8(input);
191}
192
193// static
194CommandLine::StringType CommandLine::UTF8ToStringType(std::string_view input) {
195  return UTF8ToUTF16(input);
196}
197
198// static
199void CommandLine::set_slash_is_not_a_switch() {
200  // The last switch prefix should be slash, so adjust the size to skip it.
201  DCHECK(std::u16string_view(kSwitchPrefixes[std::size(kSwitchPrefixes) - 1]) ==
202         std::u16string_view(u"/"));
203  switch_prefix_count = std::size(kSwitchPrefixes) - 1;
204}
205
206// static
207void CommandLine::InitUsingArgvForTesting(int argc, const char* const* argv) {
208  DCHECK(!current_process_commandline_);
209  current_process_commandline_ = new CommandLine(NO_PROGRAM);
210  // On Windows we need to convert the command line arguments to std::u16string.
211  base::CommandLine::StringVector argv_vector;
212  for (int i = 0; i < argc; ++i)
213    argv_vector.push_back(UTF8ToUTF16(argv[i]));
214  current_process_commandline_->InitFromArgv(argv_vector);
215}
216
217#else
218
219// static
220std::string CommandLine::StringTypeToUTF8(const StringType& input) {
221  return input;
222}
223
224// static
225CommandLine::StringType CommandLine::UTF8ToStringType(std::string_view input) {
226  return CommandLine::StringType(input);
227}
228
229#endif
230
231// static
232bool CommandLine::Init(int argc, const char* const* argv) {
233  if (current_process_commandline_) {
234    // If this is intentional, Reset() must be called first. If we are using
235    // the shared build mode, we have to share a single object across multiple
236    // shared libraries.
237    return false;
238  }
239
240  current_process_commandline_ = new CommandLine(NO_PROGRAM);
241#if defined(OS_WIN)
242  current_process_commandline_->ParseFromString(
243      reinterpret_cast<const char16_t*>(::GetCommandLineW()));
244#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
245  current_process_commandline_->InitFromArgv(argc, argv);
246#else
247#error Unsupported platform
248#endif
249
250  return true;
251}
252
253// static
254void CommandLine::Reset() {
255  DCHECK(current_process_commandline_);
256  delete current_process_commandline_;
257  current_process_commandline_ = nullptr;
258}
259
260// static
261CommandLine* CommandLine::ForCurrentProcess() {
262  DCHECK(current_process_commandline_);
263  return current_process_commandline_;
264}
265
266// static
267bool CommandLine::InitializedForCurrentProcess() {
268  return !!current_process_commandline_;
269}
270
271#if defined(OS_WIN)
272// static
273CommandLine CommandLine::FromString(const std::u16string& command_line) {
274  CommandLine cmd(NO_PROGRAM);
275  cmd.ParseFromString(command_line);
276  return cmd;
277}
278#endif
279
280void CommandLine::InitFromArgv(int argc,
281                               const CommandLine::CharType* const* argv) {
282  StringVector new_argv;
283  for (int i = 0; i < argc; ++i)
284    new_argv.push_back(argv[i]);
285  InitFromArgv(new_argv);
286}
287
288void CommandLine::InitFromArgv(const StringVector& argv) {
289  argv_ = StringVector(1);
290  switches_.clear();
291  begin_args_ = 1;
292  SetProgram(argv.empty() ? FilePath() : FilePath(argv[0]));
293  AppendSwitchesAndArguments(this, argv, parse_switches_);
294}
295
296FilePath CommandLine::GetProgram() const {
297  return FilePath(argv_[0]);
298}
299
300void CommandLine::SetProgram(const FilePath& program) {
301#if defined(OS_WIN)
302  TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]);
303#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
304  TrimWhitespaceASCII(program.value(), TRIM_ALL, &argv_[0]);
305#else
306#error Unsupported platform
307#endif
308}
309
310bool CommandLine::HasSwitch(std::string_view switch_string) const {
311  DCHECK_EQ(ToLowerASCII(switch_string), switch_string);
312  return ContainsKey(switches_, switch_string);
313}
314
315bool CommandLine::HasSwitch(const char switch_constant[]) const {
316  return HasSwitch(std::string_view(switch_constant));
317}
318
319std::string CommandLine::GetSwitchValueString(
320    std::string_view switch_string) const {
321  return StringTypeToUTF8(GetSwitchValueNative(switch_string));
322}
323
324FilePath CommandLine::GetSwitchValuePath(std::string_view switch_string) const {
325  return FilePath(GetSwitchValueNative(switch_string));
326}
327
328CommandLine::StringType CommandLine::GetSwitchValueNative(
329    std::string_view switch_string) const {
330  DCHECK_EQ(ToLowerASCII(switch_string), switch_string);
331
332  // There can be multiple matches, we want to find the last one.
333  auto iter = switches_.upper_bound(switch_string);
334  if (iter == switches_.begin())
335    return StringType();
336
337  // We want the item right before the upper bound, if it's a match.
338  --iter;
339  if (iter->first == switch_string)
340    return iter->second;
341  return StringType();
342}
343
344std::vector<std::string> CommandLine::GetSwitchValueStrings(
345    std::string_view switch_string) const {
346  std::vector<StringType> matches = GetSwitchValuesNative(switch_string);
347
348  std::vector<std::string> result;
349  result.reserve(matches.size());
350
351  for (const StringType& cur : matches) {
352    result.push_back(StringTypeToUTF8(cur));
353  }
354  return result;
355}
356
357std::vector<CommandLine::StringType> CommandLine::GetSwitchValuesNative(
358    std::string_view switch_string) const {
359  std::vector<StringType> result;
360
361  auto [iter, end] = switches_.equal_range(switch_string);
362  while (iter != end) {
363    result.push_back(iter->second);
364    ++iter;
365  }
366
367  return result;
368}
369
370void CommandLine::AppendSwitch(const std::string& switch_string) {
371  AppendSwitchNative(switch_string, StringType());
372}
373
374void CommandLine::AppendSwitchPath(const std::string& switch_string,
375                                   const FilePath& path) {
376  AppendSwitchNative(switch_string, path.value());
377}
378
379void CommandLine::AppendSwitchNative(const std::string& switch_string,
380                                     const CommandLine::StringType& value) {
381#if defined(OS_WIN)
382  const std::string switch_key = ToLowerASCII(switch_string);
383  StringType combined_switch_string(ASCIIToUTF16(switch_key));
384#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
385  const std::string& switch_key = switch_string;
386  StringType combined_switch_string(switch_key);
387#endif
388
389  size_t prefix_length = GetSwitchPrefixLength(combined_switch_string);
390  switches_.insert(make_pair(switch_key.substr(prefix_length), value));
391
392  // Preserve existing switch prefixes in |argv_|; only append one if necessary.
393  if (prefix_length == 0)
394    combined_switch_string = kSwitchPrefixes[0] + combined_switch_string;
395  if (!value.empty())
396    combined_switch_string += kSwitchValueSeparator + value;
397  // Append the switch and update the switches/arguments divider |begin_args_|.
398  argv_.insert(argv_.begin() + begin_args_++, combined_switch_string);
399}
400
401void CommandLine::AppendSwitch(const std::string& switch_string,
402                               const std::string& value_string) {
403  AppendSwitchNative(switch_string, UTF8ToStringType(value_string));
404}
405
406void CommandLine::CopySwitchesFrom(const CommandLine& source,
407                                   const char* const switches[],
408                                   size_t count) {
409  for (size_t i = 0; i < count; ++i) {
410    if (source.HasSwitch(switches[i]))
411      AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i]));
412  }
413}
414
415CommandLine::StringVector CommandLine::GetArgs() const {
416  // Gather all arguments after the last switch (may include kSwitchTerminator).
417  StringVector args(argv_.begin() + begin_args_, argv_.end());
418  // Erase only the first kSwitchTerminator (maybe "--" is a legitimate page?)
419  StringVector::iterator switch_terminator =
420      std::find(args.begin(), args.end(), kSwitchTerminator);
421  if (switch_terminator != args.end())
422    args.erase(switch_terminator);
423  return args;
424}
425
426void CommandLine::AppendArg(const std::string& value) {
427#if defined(OS_WIN)
428  DCHECK(IsStringUTF8(value));
429  AppendArgNative(UTF8ToUTF16(value));
430#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
431  AppendArgNative(value);
432#else
433#error Unsupported platform
434#endif
435}
436
437void CommandLine::AppendArgPath(const FilePath& path) {
438  AppendArgNative(path.value());
439}
440
441void CommandLine::AppendArgNative(const CommandLine::StringType& value) {
442  argv_.push_back(value);
443}
444
445void CommandLine::AppendArguments(const CommandLine& other,
446                                  bool include_program) {
447  if (include_program)
448    SetProgram(other.GetProgram());
449  AppendSwitchesAndArguments(this, other.argv(), parse_switches_);
450}
451
452void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) {
453  if (wrapper.empty())
454    return;
455  // Split the wrapper command based on whitespace (with quoting).
456  using CommandLineTokenizer =
457      StringTokenizerT<StringType, StringType::const_iterator>;
458  CommandLineTokenizer tokenizer(wrapper, FILE_PATH_LITERAL(" "));
459  tokenizer.set_quote_chars(FILE_PATH_LITERAL("'\""));
460  std::vector<StringType> wrapper_argv;
461  while (tokenizer.GetNext())
462    wrapper_argv.emplace_back(tokenizer.token());
463
464  // Prepend the wrapper and update the switches/arguments |begin_args_|.
465  argv_.insert(argv_.begin(), wrapper_argv.begin(), wrapper_argv.end());
466  begin_args_ += wrapper_argv.size();
467}
468
469#if defined(OS_WIN)
470void CommandLine::ParseFromString(const std::u16string& command_line) {
471  std::u16string command_line_string;
472  TrimWhitespace(command_line, TRIM_ALL, &command_line_string);
473  if (command_line_string.empty())
474    return;
475
476  int num_args = 0;
477  char16_t** args = NULL;
478  args = reinterpret_cast<char16_t**>(::CommandLineToArgvW(
479      reinterpret_cast<LPCWSTR>(command_line_string.c_str()), &num_args));
480
481  DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: "
482                         << UTF16ToUTF8(command_line);
483  InitFromArgv(num_args, args);
484  LocalFree(args);
485}
486#endif
487
488CommandLine::StringType CommandLine::GetCommandLineStringInternal(
489    bool quote_placeholders) const {
490  StringType string(argv_[0]);
491#if defined(OS_WIN)
492  string = QuoteForCommandLineToArgvW(string, quote_placeholders);
493#endif
494  StringType params(GetArgumentsStringInternal(quote_placeholders));
495  if (!params.empty()) {
496    string.append(StringType(FILE_PATH_LITERAL(" ")));
497    string.append(params);
498  }
499  return string;
500}
501
502CommandLine::StringType CommandLine::GetArgumentsStringInternal(
503    bool quote_placeholders) const {
504  StringType params;
505  // Append switches and arguments.
506  bool parse_switches = parse_switches_;
507  for (size_t i = 1; i < argv_.size(); ++i) {
508    StringType arg = argv_[i];
509    StringType switch_string;
510    StringType switch_value;
511    parse_switches &= arg != kSwitchTerminator;
512    if (i > 1)
513      params.append(StringType(FILE_PATH_LITERAL(" ")));
514    if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) {
515      params.append(switch_string);
516      if (!switch_value.empty()) {
517#if defined(OS_WIN)
518        switch_value =
519            QuoteForCommandLineToArgvW(switch_value, quote_placeholders);
520#endif
521        params.append(kSwitchValueSeparator + switch_value);
522      }
523    } else {
524#if defined(OS_WIN)
525      arg = QuoteForCommandLineToArgvW(arg, quote_placeholders);
526#endif
527      params.append(arg);
528    }
529  }
530  return params;
531}
532
533}  // namespace base
534