| Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
| license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| [email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 5 | #include "base/command_line.h" |
| 6 | |
| Peter Kasting | 025a9425 | 2025-01-29 21:28:37 | [diff] [blame] | 7 | #include <algorithm> |
| Arthur Sonzogni | 6718b70 | 2025-01-09 10:49:10 | [diff] [blame] | 8 | #include <array> |
| [email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 9 | #include <ostream> |
| Helmut Januschka | 6e9d8fc | 2024-04-04 16:09:55 | [diff] [blame] | 10 | #include <string_view> |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | |
| David Dorwin | 3f503b8 | 2022-04-20 04:07:03 | [diff] [blame] | 12 | #include "base/check_op.h" |
| Tom Sepez | 8dce0f0f | 2025-02-14 20:37:03 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
| Lei Zhang | c0f9fc5 | 2021-05-22 08:00:53 | [diff] [blame] | 14 | #include "base/containers/contains.h" |
| jdoerrie | 5c4dc4e | 2019-02-01 18:02:33 | [diff] [blame] | 15 | #include "base/containers/span.h" |
| Will Harris | ee9f0a2 | 2023-03-02 21:38:58 | [diff] [blame] | 16 | #include "base/debug/debugging_buildflags.h" |
| [email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 17 | #include "base/files/file_path.h" |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 18 | #include "base/logging.h" |
| Hans Wennborg | afeb390 | 2020-06-17 14:42:29 | [diff] [blame] | 19 | #include "base/notreached.h" |
| Peter Kasting | de85e74 | 2022-06-01 17:41:54 | [diff] [blame] | 20 | #include "base/numerics/checked_math.h" |
| Jan Wilken Dörrie | bd953ac | 2020-07-10 23:28:54 | [diff] [blame] | 21 | #include "base/strings/strcat.h" |
| [email protected] | 5ae0b763e | 2013-02-07 23:01:39 | [diff] [blame] | 22 | #include "base/strings/string_split.h" |
| skyostil | d851aa1 | 2017-03-29 17:38:35 | [diff] [blame] | 23 | #include "base/strings/string_tokenizer.h" |
| [email protected] | 251cd6e5 | 2013-06-11 13:36:37 | [diff] [blame] | 24 | #include "base/strings/string_util.h" |
| [email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 25 | #include "base/strings/utf_string_conversions.h" |
| [email protected] | 74e9fa2 | 2010-12-29 21:06:43 | [diff] [blame] | 26 | #include "build/build_config.h" |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 27 | |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 28 | #if BUILDFLAG(IS_WIN) |
| [email protected] | 74e9fa2 | 2010-12-29 21:06:43 | [diff] [blame] | 29 | #include <windows.h> |
| Bruce Dawson | a1e1cfcb | 2022-11-22 20:04:35 | [diff] [blame] | 30 | |
| [email protected] | 74e9fa2 | 2010-12-29 21:06:43 | [diff] [blame] | 31 | #include <shellapi.h> |
| Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 32 | |
| 33 | #include "base/strings/string_util_win.h" |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 34 | #endif // BUILDFLAG(IS_WIN) |
| [email protected] | 7f113f3 | 2009-09-10 18:02:17 | [diff] [blame] | 35 | |
| [email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 36 | namespace base { |
| [email protected] | 04af979a | 2013-02-16 04:12:26 | [diff] [blame] | 37 | |
| Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 38 | CommandLine* CommandLine::current_process_commandline_ = nullptr; |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 39 | |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 40 | namespace { |
| [email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 41 | |
| Greg Guterman | e605119 | 2021-10-15 21:14:36 | [diff] [blame] | 42 | DuplicateSwitchHandler* g_duplicate_switch_handler = nullptr; |
| 43 | |
| Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 44 | constexpr CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--"); |
| 45 | constexpr CommandLine::CharType kSwitchValueSeparator[] = |
| 46 | FILE_PATH_LITERAL("="); |
| [email protected] | bf98a0e1 | 2013-09-25 23:36:00 | [diff] [blame] | 47 | |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 48 | // Since we use a lazy match, make sure that longer versions (like "--") are |
| 49 | // listed before shorter versions (like "-") of similar prefixes. |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 50 | #if BUILDFLAG(IS_WIN) |
| [email protected] | bf98a0e1 | 2013-09-25 23:36:00 | [diff] [blame] | 51 | // By putting slash last, we can control whether it is treaded as a switch |
| 52 | // value by changing the value of switch_prefix_count to be one less than |
| 53 | // the array size. |
| Arthur Sonzogni | 6718b70 | 2025-01-09 10:49:10 | [diff] [blame] | 54 | constexpr auto kSwitchPrefixes = std::to_array<CommandLine::StringViewType>({ |
| 55 | L"--", |
| 56 | L"-", |
| 57 | L"/", |
| 58 | }); |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 59 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
| [email protected] | 1a48f31 | 2008-08-12 01:14:37 | [diff] [blame] | 60 | // Unixes don't use slash as a switch. |
| Arthur Sonzogni | 6718b70 | 2025-01-09 10:49:10 | [diff] [blame] | 61 | constexpr auto kSwitchPrefixes = std::to_array<CommandLine::StringViewType>({ |
| 62 | "--", |
| 63 | "-", |
| 64 | }); |
| [email protected] | 5d42633 | 2008-08-08 20:46:21 | [diff] [blame] | 65 | #endif |
| Daniel Cheng | f45f4760 | 2022-02-28 22:38:32 | [diff] [blame] | 66 | size_t switch_prefix_count = std::size(kSwitchPrefixes); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 67 | |
| Lei Zhang | a562bca | 2024-12-09 23:42:41 | [diff] [blame] | 68 | bool IsSwitchNameValid(std::string_view switch_name) { |
| 69 | return ToLowerASCII(switch_name) == switch_name; |
| 70 | } |
| 71 | |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 72 | #if BUILDFLAG(IS_WIN) |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 73 | // Switch string that specifies the single argument to the command line. |
| 74 | // If present, everything after this switch is interpreted as a single |
| 75 | // argument regardless of whitespace, quotes, etc. Used for launches from the |
| 76 | // Windows shell, which may have arguments with unencoded quotes that could |
| 77 | // otherwise unexpectedly be split into multiple arguments |
| 78 | // (https://crbug.com/937179). |
| 79 | constexpr CommandLine::CharType kSingleArgument[] = |
| 80 | FILE_PATH_LITERAL("single-argument"); |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 81 | #endif // BUILDFLAG(IS_WIN) |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 82 | |
| Lei Zhang | d638812 | 2024-07-30 20:39:19 | [diff] [blame] | 83 | size_t GetSwitchPrefixLength(CommandLine::StringViewType string) { |
| [email protected] | bf98a0e1 | 2013-09-25 23:36:00 | [diff] [blame] | 84 | for (size_t i = 0; i < switch_prefix_count; ++i) { |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 85 | CommandLine::StringType prefix(kSwitchPrefixes[i]); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 86 | if (string.substr(0, prefix.length()) == prefix) { |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 87 | return prefix.length(); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 88 | } |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 89 | } |
| 90 | return 0; |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 91 | } |
| [email protected] | 0fd23af | 2011-02-20 06:33:04 | [diff] [blame] | 92 | |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 93 | // Fills in |switch_string| and |switch_value| if |string| is a switch. |
| 94 | // This will preserve the input switch prefix in the output |switch_string|. |
| 95 | bool IsSwitch(const CommandLine::StringType& string, |
| 96 | CommandLine::StringType* switch_string, |
| 97 | CommandLine::StringType* switch_value) { |
| 98 | switch_string->clear(); |
| 99 | switch_value->clear(); |
| [email protected] | 21e342f | 2012-10-19 06:19:59 | [diff] [blame] | 100 | size_t prefix_length = GetSwitchPrefixLength(string); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 101 | if (prefix_length == 0 || prefix_length == string.length()) { |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 102 | return false; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 103 | } |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 104 | |
| 105 | const size_t equals_position = string.find(kSwitchValueSeparator); |
| 106 | *switch_string = string.substr(0, equals_position); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 107 | if (equals_position != CommandLine::StringType::npos) { |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 108 | *switch_value = string.substr(equals_position + 1); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 109 | } |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 110 | return true; |
| 111 | } |
| 112 | |
| Pavol Marko | bf16b81 | 2019-06-14 00:53:12 | [diff] [blame] | 113 | // Returns true iff |string| represents a switch with key |
| 114 | // |switch_key_without_prefix|, regardless of value. |
| Lei Zhang | d638812 | 2024-07-30 20:39:19 | [diff] [blame] | 115 | bool IsSwitchWithKey(CommandLine::StringViewType string, |
| 116 | CommandLine::StringViewType switch_key_without_prefix) { |
| Pavol Marko | bf16b81 | 2019-06-14 00:53:12 | [diff] [blame] | 117 | size_t prefix_length = GetSwitchPrefixLength(string); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 118 | if (prefix_length == 0 || prefix_length == string.length()) { |
| Pavol Marko | bf16b81 | 2019-06-14 00:53:12 | [diff] [blame] | 119 | return false; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 120 | } |
| Pavol Marko | bf16b81 | 2019-06-14 00:53:12 | [diff] [blame] | 121 | |
| 122 | const size_t equals_position = string.find(kSwitchValueSeparator); |
| 123 | return string.substr(prefix_length, equals_position - prefix_length) == |
| 124 | switch_key_without_prefix; |
| 125 | } |
| 126 | |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 127 | #if BUILDFLAG(IS_WIN) |
| S. Ganesh | f2c7cb73 | 2022-12-16 22:54:42 | [diff] [blame] | 128 | // Quotes a string as necessary for CommandLineToArgvW compatibility *on |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 129 | // Windows*. |
| S. Ganesh | f2c7cb73 | 2022-12-16 22:54:42 | [diff] [blame] | 130 | // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx#parsing-c-command-line-arguments. |
| 131 | std::wstring QuoteForCommandLineToArgvWInternal( |
| 132 | const std::wstring& arg, |
| 133 | bool allow_unsafe_insert_sequences) { |
| 134 | // Ensures that GetCommandLineString isn't used to generate command-line |
| Jesse McKenna | 185ceda2 | 2021-03-10 06:47:55 | [diff] [blame] | 135 | // strings for the Windows shell by checking for Windows insert sequences like |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 136 | // "%1". GetCommandLineStringForShell should be used instead to get a string |
| 137 | // with the correct placeholder format for the shell. |
| Jesse McKenna | 185ceda2 | 2021-03-10 06:47:55 | [diff] [blame] | 138 | DCHECK(arg.size() != 2 || arg[0] != L'%' || allow_unsafe_insert_sequences); |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 139 | |
| S. Ganesh | f2c7cb73 | 2022-12-16 22:54:42 | [diff] [blame] | 140 | constexpr wchar_t kQuotableCharacters[] = L" \t\\\""; |
| 141 | if (arg.find_first_of(kQuotableCharacters) == std::wstring::npos) { |
| [email protected] | 0fd23af | 2011-02-20 06:33:04 | [diff] [blame] | 142 | return arg; |
| 143 | } |
| 144 | |
| S. Ganesh | f2c7cb73 | 2022-12-16 22:54:42 | [diff] [blame] | 145 | std::wstring out(1, L'"'); |
| [email protected] | 0fd23af | 2011-02-20 06:33:04 | [diff] [blame] | 146 | for (size_t i = 0; i < arg.size(); ++i) { |
| S. Ganesh | f2c7cb73 | 2022-12-16 22:54:42 | [diff] [blame] | 147 | if (arg[i] == L'\\') { |
| 148 | // Finds the extent of this run of backslashes. |
| 149 | size_t end = i + 1; |
| 150 | while (end < arg.size() && arg[end] == L'\\') { |
| 151 | ++end; |
| 152 | } |
| [email protected] | 0fd23af | 2011-02-20 06:33:04 | [diff] [blame] | 153 | |
| S. Ganesh | f2c7cb73 | 2022-12-16 22:54:42 | [diff] [blame] | 154 | const size_t backslash_count = end - i; |
| 155 | |
| 156 | // Backslashes are escaped only if the run is followed by a double quote. |
| [email protected] | 0fd23af | 2011-02-20 06:33:04 | [diff] [blame] | 157 | // Since we also will end the string with a double quote, we escape for |
| 158 | // either a double quote or the end of the string. |
| S. Ganesh | f2c7cb73 | 2022-12-16 22:54:42 | [diff] [blame] | 159 | const size_t backslash_multiplier = |
| 160 | (end == arg.size() || arg[end] == L'"') ? 2 : 1; |
| [email protected] | 0fd23af | 2011-02-20 06:33:04 | [diff] [blame] | 161 | |
| S. Ganesh | f2c7cb73 | 2022-12-16 22:54:42 | [diff] [blame] | 162 | out.append(std::wstring(backslash_count * backslash_multiplier, L'\\')); |
| 163 | |
| 164 | // Advances `i` to one before `end` to balance `++i` in loop. |
| [email protected] | 0fd23af | 2011-02-20 06:33:04 | [diff] [blame] | 165 | i = end - 1; |
| S. Ganesh | f2c7cb73 | 2022-12-16 22:54:42 | [diff] [blame] | 166 | } else if (arg[i] == L'"') { |
| 167 | out.append(LR"(\")"); |
| [email protected] | 0fd23af | 2011-02-20 06:33:04 | [diff] [blame] | 168 | } else { |
| 169 | out.push_back(arg[i]); |
| 170 | } |
| 171 | } |
| S. Ganesh | f2c7cb73 | 2022-12-16 22:54:42 | [diff] [blame] | 172 | |
| 173 | out.push_back(L'"'); |
| [email protected] | 0fd23af | 2011-02-20 06:33:04 | [diff] [blame] | 174 | |
| 175 | return out; |
| 176 | } |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 177 | #endif // BUILDFLAG(IS_WIN) |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 178 | |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 179 | } // namespace |
| 180 | |
| Greg Guterman | e605119 | 2021-10-15 21:14:36 | [diff] [blame] | 181 | // static |
| 182 | void CommandLine::SetDuplicateSwitchHandler( |
| 183 | std::unique_ptr<DuplicateSwitchHandler> new_duplicate_switch_handler) { |
| 184 | delete g_duplicate_switch_handler; |
| 185 | g_duplicate_switch_handler = new_duplicate_switch_handler.release(); |
| 186 | } |
| 187 | |
| Greg Guterman | fe16560d | 2021-10-07 19:58:15 | [diff] [blame] | 188 | CommandLine::CommandLine(NoProgram no_program) : argv_(1), begin_args_(1) {} |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 189 | |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 190 | CommandLine::CommandLine(const FilePath& program) : argv_(1), begin_args_(1) { |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 191 | SetProgram(program); |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 192 | } |
| 193 | |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 194 | CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv) |
| Greg Guterman | fe16560d | 2021-10-07 19:58:15 | [diff] [blame] | 195 | : argv_(1), begin_args_(1) { |
| Tom Sepez | 8dce0f0f | 2025-02-14 20:37:03 | [diff] [blame] | 196 | // SAFETY: required from caller. |
| 197 | UNSAFE_BUFFERS(InitFromArgv(argc, argv)); |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 198 | } |
| 199 | |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 200 | CommandLine::CommandLine(const StringVector& argv) : argv_(1), begin_args_(1) { |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 201 | InitFromArgv(argv); |
| 202 | } |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 203 | |
| Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 204 | CommandLine::CommandLine(const CommandLine& other) = default; |
| Greg Thompson | 809a1c0 | 2024-02-22 16:28:55 | [diff] [blame] | 205 | CommandLine::CommandLine(CommandLine&& other) noexcept |
| 206 | : |
| 207 | #if BUILDFLAG(IS_WIN) |
| 208 | raw_command_line_string_( |
| Lei Zhang | d638812 | 2024-07-30 20:39:19 | [diff] [blame] | 209 | std::exchange(other.raw_command_line_string_, StringViewType())), |
| Greg Thompson | 809a1c0 | 2024-02-22 16:28:55 | [diff] [blame] | 210 | has_single_argument_switch_( |
| 211 | std::exchange(other.has_single_argument_switch_, false)), |
| 212 | #endif // BUILDFLAG(IS_WIN) |
| 213 | argv_(std::exchange(other.argv_, StringVector(1))), |
| 214 | switches_(std::move(other.switches_)), |
| 215 | begin_args_(std::exchange(other.begin_args_, 1)) { |
| 216 | #if BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS) |
| 217 | other.sequence_checker_.Detach(); |
| 218 | #endif |
| 219 | } |
| Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 220 | CommandLine& CommandLine::operator=(const CommandLine& other) = default; |
| Greg Thompson | 809a1c0 | 2024-02-22 16:28:55 | [diff] [blame] | 221 | CommandLine& CommandLine::operator=(CommandLine&& other) noexcept { |
| 222 | #if BUILDFLAG(IS_WIN) |
| 223 | raw_command_line_string_ = |
| Lei Zhang | d638812 | 2024-07-30 20:39:19 | [diff] [blame] | 224 | std::exchange(other.raw_command_line_string_, StringViewType()); |
| Greg Thompson | 809a1c0 | 2024-02-22 16:28:55 | [diff] [blame] | 225 | has_single_argument_switch_ = |
| 226 | std::exchange(other.has_single_argument_switch_, false); |
| 227 | #endif // BUILDFLAG(IS_WIN) |
| 228 | argv_ = std::exchange(other.argv_, StringVector(1)); |
| 229 | switches_ = std::move(other.switches_); |
| 230 | begin_args_ = std::exchange(other.begin_args_, 1); |
| 231 | #if BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS) |
| 232 | other.sequence_checker_.Detach(); |
| 233 | #endif |
| 234 | return *this; |
| 235 | } |
| Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 236 | CommandLine::~CommandLine() = default; |
| [email protected] | acbeb3d | 2011-03-01 20:47:58 | [diff] [blame] | 237 | |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 238 | #if BUILDFLAG(IS_WIN) |
| [email protected] | bf98a0e1 | 2013-09-25 23:36:00 | [diff] [blame] | 239 | // static |
| 240 | void CommandLine::set_slash_is_not_a_switch() { |
| 241 | // The last switch prefix should be slash, so adjust the size to skip it. |
| Peter Kasting | 0e04276 | 2024-11-13 22:54:55 | [diff] [blame] | 242 | static_assert(base::span(kSwitchPrefixes).back() == L"/", |
| Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 243 | "Error: Last switch prefix is not a slash."); |
| Daniel Cheng | f45f4760 | 2022-02-28 22:38:32 | [diff] [blame] | 244 | switch_prefix_count = std::size(kSwitchPrefixes) - 1; |
| [email protected] | bf98a0e1 | 2013-09-25 23:36:00 | [diff] [blame] | 245 | } |
| ananta | d936bc1 | 2016-06-22 21:40:31 | [diff] [blame] | 246 | |
| 247 | // static |
| 248 | void CommandLine::InitUsingArgvForTesting(int argc, const char* const* argv) { |
| 249 | DCHECK(!current_process_commandline_); |
| 250 | current_process_commandline_ = new CommandLine(NO_PROGRAM); |
| Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 251 | // On Windows we need to convert the command line arguments to std::wstring. |
| jdoerrie | 5c4dc4e | 2019-02-01 18:02:33 | [diff] [blame] | 252 | CommandLine::StringVector argv_vector; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 253 | for (int i = 0; i < argc; ++i) { |
| Tom Sepez | 8dce0f0f | 2025-02-14 20:37:03 | [diff] [blame] | 254 | // SAFETY: required from caller. |
| 255 | argv_vector.push_back(UTF8ToWide(UNSAFE_BUFFERS(argv[i]))); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 256 | } |
| ananta | d936bc1 | 2016-06-22 21:40:31 | [diff] [blame] | 257 | current_process_commandline_->InitFromArgv(argv_vector); |
| 258 | } |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 259 | #endif // BUILDFLAG(IS_WIN) |
| [email protected] | bf98a0e1 | 2013-09-25 23:36:00 | [diff] [blame] | 260 | |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 261 | // static |
| [email protected] | 72e2e242 | 2012-02-27 18:38:12 | [diff] [blame] | 262 | bool CommandLine::Init(int argc, const char* const* argv) { |
| [email protected] | f96fe2c4 | 2011-07-13 18:03:34 | [diff] [blame] | 263 | if (current_process_commandline_) { |
| 264 | // If this is intentional, Reset() must be called first. If we are using |
| 265 | // the shared build mode, we have to share a single object across multiple |
| 266 | // shared libraries. |
| [email protected] | 72e2e242 | 2012-02-27 18:38:12 | [diff] [blame] | 267 | return false; |
| [email protected] | f96fe2c4 | 2011-07-13 18:03:34 | [diff] [blame] | 268 | } |
| 269 | |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 270 | current_process_commandline_ = new CommandLine(NO_PROGRAM); |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 271 | #if BUILDFLAG(IS_WIN) |
| Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 272 | current_process_commandline_->ParseFromString(::GetCommandLineW()); |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 273 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
| Tom Sepez | 8dce0f0f | 2025-02-14 20:37:03 | [diff] [blame] | 274 | // SAFETY: required from caller. |
| 275 | UNSAFE_BUFFERS(current_process_commandline_->InitFromArgv(argc, argv)); |
| Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 276 | #else |
| 277 | #error Unsupported platform |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 278 | #endif |
| [email protected] | 72e2e242 | 2012-02-27 18:38:12 | [diff] [blame] | 279 | |
| 280 | return true; |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | // static |
| 284 | void CommandLine::Reset() { |
| 285 | DCHECK(current_process_commandline_); |
| 286 | delete current_process_commandline_; |
| Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 287 | current_process_commandline_ = nullptr; |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | // static |
| 291 | CommandLine* CommandLine::ForCurrentProcess() { |
| 292 | DCHECK(current_process_commandline_); |
| 293 | return current_process_commandline_; |
| [email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 294 | } |
| 295 | |
| [email protected] | 2bf64a9 | 2013-07-11 23:10:40 | [diff] [blame] | 296 | // static |
| 297 | bool CommandLine::InitializedForCurrentProcess() { |
| 298 | return !!current_process_commandline_; |
| 299 | } |
| 300 | |
| mayuko aiba | fa08692f | 2024-01-12 03:58:38 | [diff] [blame] | 301 | // static |
| 302 | CommandLine CommandLine::FromArgvWithoutProgram(const StringVector& argv) { |
| 303 | CommandLine cmd(NO_PROGRAM); |
| 304 | cmd.AppendSwitchesAndArguments(argv); |
| 305 | return cmd; |
| 306 | } |
| 307 | |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 308 | #if BUILDFLAG(IS_WIN) |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 309 | // static |
| Lei Zhang | d638812 | 2024-07-30 20:39:19 | [diff] [blame] | 310 | CommandLine CommandLine::FromString(StringViewType command_line) { |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 311 | CommandLine cmd(NO_PROGRAM); |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 312 | cmd.ParseFromString(command_line); |
| 313 | return cmd; |
| 314 | } |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 315 | #endif // BUILDFLAG(IS_WIN) |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 316 | |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 317 | void CommandLine::InitFromArgv(int argc, |
| 318 | const CommandLine::CharType* const* argv) { |
| 319 | StringVector new_argv; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 320 | for (int i = 0; i < argc; ++i) { |
| Tom Sepez | 8dce0f0f | 2025-02-14 20:37:03 | [diff] [blame] | 321 | // SAFETY: required from caller. |
| 322 | new_argv.push_back(UNSAFE_BUFFERS(argv[i])); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 323 | } |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 324 | InitFromArgv(new_argv); |
| [email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 325 | } |
| 326 | |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 327 | void CommandLine::InitFromArgv(const StringVector& argv) { |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 328 | argv_ = StringVector(1); |
| [email protected] | 93660ab3 | 2013-06-18 08:19:18 | [diff] [blame] | 329 | switches_.clear(); |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 330 | begin_args_ = 1; |
| 331 | SetProgram(argv.empty() ? FilePath() : FilePath(argv[0])); |
| mayuko aiba | fa08692f | 2024-01-12 03:58:38 | [diff] [blame] | 332 | if (!argv.empty()) { |
| Peter Kasting | 654bb625 | 2024-11-16 02:29:08 | [diff] [blame] | 333 | AppendSwitchesAndArguments(span(argv).subspan<1>()); |
| mayuko aiba | fa08692f | 2024-01-12 03:58:38 | [diff] [blame] | 334 | } |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 335 | } |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 336 | |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 337 | FilePath CommandLine::GetProgram() const { |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 338 | return FilePath(argv_[0]); |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void CommandLine::SetProgram(const FilePath& program) { |
| Will Harris | ee9f0a2 | 2023-03-02 21:38:58 | [diff] [blame] | 342 | #if BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS) |
| 343 | sequence_checker_.Check(); |
| 344 | #endif |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 345 | #if BUILDFLAG(IS_WIN) |
| Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 346 | argv_[0] = StringType(TrimWhitespace(program.value(), TRIM_ALL)); |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 347 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
| tfarina | 023b1dcc | 2015-12-06 13:25:41 | [diff] [blame] | 348 | TrimWhitespaceASCII(program.value(), TRIM_ALL, &argv_[0]); |
| Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 349 | #else |
| 350 | #error Unsupported platform |
| tfarina | 023b1dcc | 2015-12-06 13:25:41 | [diff] [blame] | 351 | #endif |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 352 | } |
| 353 | |
| Helmut Januschka | 6e9d8fc | 2024-04-04 16:09:55 | [diff] [blame] | 354 | bool CommandLine::HasSwitch(std::string_view switch_string) const { |
| Daniel Cheng | e383581 | 2025-05-26 23:01:39 | [diff] [blame] | 355 | CHECK(IsSwitchNameValid(switch_string)); |
| Jan Wilken Dörrie | f61e74c | 2019-06-07 08:20:02 | [diff] [blame] | 356 | return Contains(switches_, switch_string); |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 357 | } |
| 358 | |
| jackhou | 1bd9da9 | 2015-05-21 04:48:00 | [diff] [blame] | 359 | bool CommandLine::HasSwitch(const char switch_constant[]) const { |
| Helmut Januschka | 6e9d8fc | 2024-04-04 16:09:55 | [diff] [blame] | 360 | return HasSwitch(std::string_view(switch_constant)); |
| tapted | 009a1dc8 | 2015-03-30 03:57:10 | [diff] [blame] | 361 | } |
| 362 | |
| Helmut Januschka | 6e9d8fc | 2024-04-04 16:09:55 | [diff] [blame] | 363 | std::string CommandLine::GetSwitchValueASCII( |
| 364 | std::string_view switch_string) const { |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 365 | StringType value = GetSwitchValueNative(switch_string); |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 366 | #if BUILDFLAG(IS_WIN) |
| Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 367 | if (!IsStringASCII(base::AsStringPiece16(value))) { |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 368 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
| [email protected] | bd6fc2fb | 2014-03-17 23:55:43 | [diff] [blame] | 369 | if (!IsStringASCII(value)) { |
| Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 370 | #endif |
| [email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 371 | DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII."; |
| 372 | return std::string(); |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 373 | } |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 374 | #if BUILDFLAG(IS_WIN) |
| Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 375 | return WideToUTF8(value); |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 376 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 377 | return value; |
| 378 | #endif |
| 379 | } |
| 380 | |
| S. Ganesh | e484e29 | 2025-02-13 20:43:14 | [diff] [blame] | 381 | std::string CommandLine::GetSwitchValueUTF8( |
| 382 | std::string_view switch_string) const { |
| 383 | StringType value = GetSwitchValueNative(switch_string); |
| 384 | |
| 385 | #if BUILDFLAG(IS_WIN) |
| 386 | const std::string maybe_utf8_value = WideToUTF8(value); |
| 387 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
| 388 | const std::string maybe_utf8_value = value; |
| 389 | #endif |
| 390 | |
| 391 | if (!IsStringUTF8(maybe_utf8_value)) { |
| 392 | DLOG(WARNING) << "Value of switch (" << switch_string << ") is not UTF8."; |
| 393 | return {}; |
| 394 | } |
| 395 | return maybe_utf8_value; |
| 396 | } |
| 397 | |
| Helmut Januschka | 6e9d8fc | 2024-04-04 16:09:55 | [diff] [blame] | 398 | FilePath CommandLine::GetSwitchValuePath(std::string_view switch_string) const { |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 399 | return FilePath(GetSwitchValueNative(switch_string)); |
| 400 | } |
| 401 | |
| 402 | CommandLine::StringType CommandLine::GetSwitchValueNative( |
| Helmut Januschka | 6e9d8fc | 2024-04-04 16:09:55 | [diff] [blame] | 403 | std::string_view switch_string) const { |
| Daniel Cheng | e383581 | 2025-05-26 23:01:39 | [diff] [blame] | 404 | CHECK(IsSwitchNameValid(switch_string)); |
| Lei Zhang | a562bca | 2024-12-09 23:42:41 | [diff] [blame] | 405 | |
| Jeremy Roman | 863386d | 2017-10-31 19:25:38 | [diff] [blame] | 406 | auto result = switches_.find(switch_string); |
| 407 | return result == switches_.end() ? StringType() : result->second; |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 408 | } |
| 409 | |
| Helmut Januschka | 6e9d8fc | 2024-04-04 16:09:55 | [diff] [blame] | 410 | void CommandLine::AppendSwitch(std::string_view switch_string) { |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 411 | AppendSwitchNative(switch_string, StringType()); |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 412 | } |
| 413 | |
| Helmut Januschka | 6e9d8fc | 2024-04-04 16:09:55 | [diff] [blame] | 414 | void CommandLine::AppendSwitchPath(std::string_view switch_string, |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 415 | const FilePath& path) { |
| 416 | AppendSwitchNative(switch_string, path.value()); |
| 417 | } |
| 418 | |
| Helmut Januschka | 6e9d8fc | 2024-04-04 16:09:55 | [diff] [blame] | 419 | void CommandLine::AppendSwitchNative(std::string_view switch_string, |
| Lei Zhang | d638812 | 2024-07-30 20:39:19 | [diff] [blame] | 420 | CommandLine::StringViewType value) { |
| Will Harris | ee9f0a2 | 2023-03-02 21:38:58 | [diff] [blame] | 421 | #if BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS) |
| 422 | sequence_checker_.Check(); |
| 423 | #endif |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 424 | #if BUILDFLAG(IS_WIN) |
| brettw | fce8d19 | 2015-08-10 19:07:51 | [diff] [blame] | 425 | const std::string switch_key = ToLowerASCII(switch_string); |
| Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 426 | StringType combined_switch_string(UTF8ToWide(switch_key)); |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 427 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
| Helmut Januschka | 6e9d8fc | 2024-04-04 16:09:55 | [diff] [blame] | 428 | std::string_view switch_key = switch_string; |
| jackhou | 1bd9da9 | 2015-05-21 04:48:00 | [diff] [blame] | 429 | StringType combined_switch_string(switch_key); |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 430 | #endif |
| 431 | size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); |
| Greg Guterman | fe16560d | 2021-10-07 19:58:15 | [diff] [blame] | 432 | auto key = switch_key.substr(prefix_length); |
| Greg Guterman | e605119 | 2021-10-15 21:14:36 | [diff] [blame] | 433 | if (g_duplicate_switch_handler) { |
| 434 | g_duplicate_switch_handler->ResolveDuplicate(key, value, |
| 435 | switches_[std::string(key)]); |
| Greg Guterman | fe16560d | 2021-10-07 19:58:15 | [diff] [blame] | 436 | } else { |
| Daniel Cheng | a371b4c | 2022-01-14 18:24:27 | [diff] [blame] | 437 | switches_[std::string(key)] = StringType(value); |
| Greg Guterman | fe16560d | 2021-10-07 19:58:15 | [diff] [blame] | 438 | } |
| 439 | |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 440 | // Preserve existing switch prefixes in |argv_|; only append one if necessary. |
| Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 441 | if (prefix_length == 0) { |
| 442 | combined_switch_string.insert(0, kSwitchPrefixes[0].data(), |
| 443 | kSwitchPrefixes[0].size()); |
| 444 | } |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 445 | if (!value.empty()) { |
| Jan Wilken Dörrie | bd953ac | 2020-07-10 23:28:54 | [diff] [blame] | 446 | base::StrAppend(&combined_switch_string, {kSwitchValueSeparator, value}); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 447 | } |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 448 | // Append the switch and update the switches/arguments divider |begin_args_|. |
| Peter Kasting | de85e74 | 2022-06-01 17:41:54 | [diff] [blame] | 449 | argv_.insert(argv_.begin() + begin_args_, combined_switch_string); |
| 450 | begin_args_ = (CheckedNumeric(begin_args_) + 1).ValueOrDie(); |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 451 | } |
| 452 | |
| Helmut Januschka | 6e9d8fc | 2024-04-04 16:09:55 | [diff] [blame] | 453 | void CommandLine::AppendSwitchASCII(std::string_view switch_string, |
| 454 | std::string_view value_string) { |
| S. Ganesh | e484e29 | 2025-02-13 20:43:14 | [diff] [blame] | 455 | AppendSwitchUTF8(switch_string, value_string); |
| 456 | } |
| 457 | |
| 458 | void CommandLine::AppendSwitchUTF8(std::string_view switch_string, |
| 459 | std::string_view value_string) { |
| S. Ganesh | 36c9c55f | 2025-02-14 05:20:51 | [diff] [blame] | 460 | DCHECK(IsStringUTF8(value_string)) |
| 461 | << "Switch (" << switch_string << ") value (" << value_string |
| 462 | << ") is not UTF8."; |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 463 | #if BUILDFLAG(IS_WIN) |
| Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 464 | AppendSwitchNative(switch_string, UTF8ToWide(value_string)); |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 465 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 466 | AppendSwitchNative(switch_string, value_string); |
| Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 467 | #else |
| 468 | #error Unsupported platform |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 469 | #endif |
| 470 | } |
| 471 | |
| Helmut Januschka | 6e9d8fc | 2024-04-04 16:09:55 | [diff] [blame] | 472 | void CommandLine::RemoveSwitch(std::string_view switch_key_without_prefix) { |
| Will Harris | ee9f0a2 | 2023-03-02 21:38:58 | [diff] [blame] | 473 | #if BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS) |
| 474 | sequence_checker_.Check(); |
| 475 | #endif |
| Daniel Cheng | e383581 | 2025-05-26 23:01:39 | [diff] [blame] | 476 | CHECK(IsSwitchNameValid(switch_key_without_prefix)); |
| Lei Zhang | a562bca | 2024-12-09 23:42:41 | [diff] [blame] | 477 | |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 478 | #if BUILDFLAG(IS_WIN) |
| Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 479 | StringType switch_key_native = UTF8ToWide(switch_key_without_prefix); |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 480 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
| Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 481 | StringType switch_key_native(switch_key_without_prefix); |
| Pavol Marko | bf16b81 | 2019-06-14 00:53:12 | [diff] [blame] | 482 | #endif |
| 483 | |
| Pavol Marko | bf16b81 | 2019-06-14 00:53:12 | [diff] [blame] | 484 | DCHECK_EQ(0u, GetSwitchPrefixLength(switch_key_native)); |
| Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 485 | auto it = switches_.find(switch_key_without_prefix); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 486 | if (it == switches_.end()) { |
| Pavol Marko | bf16b81 | 2019-06-14 00:53:12 | [diff] [blame] | 487 | return; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 488 | } |
| Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 489 | switches_.erase(it); |
| Pavol Marko | bf16b81 | 2019-06-14 00:53:12 | [diff] [blame] | 490 | // Also erase from the switches section of |argv_| and update |begin_args_| |
| 491 | // accordingly. |
| 492 | // Switches in |argv_| have indices [1, begin_args_). |
| 493 | auto argv_switches_begin = argv_.begin() + 1; |
| 494 | auto argv_switches_end = argv_.begin() + begin_args_; |
| 495 | DCHECK(argv_switches_begin <= argv_switches_end); |
| 496 | DCHECK(argv_switches_end <= argv_.end()); |
| Andrei Polushin | 2ec89bc | 2019-07-30 20:47:17 | [diff] [blame] | 497 | auto expell = std::remove_if(argv_switches_begin, argv_switches_end, |
| Pavol Marko | bf16b81 | 2019-06-14 00:53:12 | [diff] [blame] | 498 | [&switch_key_native](const StringType& arg) { |
| 499 | return IsSwitchWithKey(arg, switch_key_native); |
| 500 | }); |
| Andrei Polushin | 2ec89bc | 2019-07-30 20:47:17 | [diff] [blame] | 501 | if (expell == argv_switches_end) { |
| Peter Boström | de57333 | 2024-08-26 20:42:45 | [diff] [blame] | 502 | NOTREACHED(); |
| Pavol Marko | bf16b81 | 2019-06-14 00:53:12 | [diff] [blame] | 503 | } |
| Andrei Polushin | 2ec89bc | 2019-07-30 20:47:17 | [diff] [blame] | 504 | begin_args_ -= argv_switches_end - expell; |
| 505 | argv_.erase(expell, argv_switches_end); |
| Avi Drissman | 1aa6cb9 | 2019-01-23 15:58:38 | [diff] [blame] | 506 | } |
| 507 | |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 508 | void CommandLine::CopySwitchesFrom(const CommandLine& source, |
| Lei Zhang | 24dcd3e | 2023-07-07 20:08:36 | [diff] [blame] | 509 | span<const char* const> switches) { |
| 510 | for (const char* entry : switches) { |
| 511 | if (source.HasSwitch(entry)) { |
| 512 | AppendSwitchNative(entry, source.GetSwitchValueNative(entry)); |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| [email protected] | 75f1c78 | 2011-07-13 23:41:22 | [diff] [blame] | 517 | CommandLine::StringVector CommandLine::GetArgs() const { |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 518 | // Gather all arguments after the last switch (may include kSwitchTerminator). |
| 519 | StringVector args(argv_.begin() + begin_args_, argv_.end()); |
| 520 | // Erase only the first kSwitchTerminator (maybe "--" is a legitimate page?) |
| Peter Kasting | 025a9425 | 2025-01-29 21:28:37 | [diff] [blame] | 521 | auto switch_terminator = std::ranges::find(args, kSwitchTerminator); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 522 | if (switch_terminator != args.end()) { |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 523 | args.erase(switch_terminator); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 524 | } |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 525 | return args; |
| 526 | } |
| 527 | |
| Helmut Januschka | 6e9d8fc | 2024-04-04 16:09:55 | [diff] [blame] | 528 | void CommandLine::AppendArg(std::string_view value) { |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 529 | #if BUILDFLAG(IS_WIN) |
| [email protected] | bd6fc2fb | 2014-03-17 23:55:43 | [diff] [blame] | 530 | DCHECK(IsStringUTF8(value)); |
| Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 531 | AppendArgNative(UTF8ToWide(value)); |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 532 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 533 | AppendArgNative(value); |
| Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 534 | #else |
| 535 | #error Unsupported platform |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 536 | #endif |
| 537 | } |
| 538 | |
| 539 | void CommandLine::AppendArgPath(const FilePath& path) { |
| 540 | AppendArgNative(path.value()); |
| 541 | } |
| 542 | |
| Lei Zhang | d638812 | 2024-07-30 20:39:19 | [diff] [blame] | 543 | void CommandLine::AppendArgNative(StringViewType value) { |
| Will Harris | ee9f0a2 | 2023-03-02 21:38:58 | [diff] [blame] | 544 | #if BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS) |
| 545 | sequence_checker_.Check(); |
| 546 | #endif |
| Peter Kasting | 811504a7 | 2025-01-09 03:18:50 | [diff] [blame] | 547 | argv_.emplace_back(value); |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | void CommandLine::AppendArguments(const CommandLine& other, |
| 551 | bool include_program) { |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 552 | if (include_program) { |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 553 | SetProgram(other.GetProgram()); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 554 | } |
| mayuko aiba | fa08692f | 2024-01-12 03:58:38 | [diff] [blame] | 555 | if (!other.argv().empty()) { |
| Peter Kasting | 654bb625 | 2024-11-16 02:29:08 | [diff] [blame] | 556 | AppendSwitchesAndArguments(span(other.argv()).subspan<1>()); |
| mayuko aiba | fa08692f | 2024-01-12 03:58:38 | [diff] [blame] | 557 | } |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 558 | } |
| 559 | |
| Lei Zhang | d638812 | 2024-07-30 20:39:19 | [diff] [blame] | 560 | void CommandLine::PrependWrapper(StringViewType wrapper) { |
| Will Harris | ee9f0a2 | 2023-03-02 21:38:58 | [diff] [blame] | 561 | #if BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS) |
| 562 | sequence_checker_.Check(); |
| 563 | #endif |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 564 | if (wrapper.empty()) { |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 565 | return; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 566 | } |
| skyostil | d851aa1 | 2017-03-29 17:38:35 | [diff] [blame] | 567 | // Split the wrapper command based on whitespace (with quoting). |
| Lei Zhang | d638812 | 2024-07-30 20:39:19 | [diff] [blame] | 568 | // StringViewType does not currently work directly with StringTokenizerT. |
| skyostil | d851aa1 | 2017-03-29 17:38:35 | [diff] [blame] | 569 | using CommandLineTokenizer = |
| 570 | StringTokenizerT<StringType, StringType::const_iterator>; |
| Wez | 65018d0 | 2021-05-20 15:47:45 | [diff] [blame] | 571 | StringType wrapper_string(wrapper); |
| 572 | CommandLineTokenizer tokenizer(wrapper_string, FILE_PATH_LITERAL(" ")); |
| skyostil | d851aa1 | 2017-03-29 17:38:35 | [diff] [blame] | 573 | tokenizer.set_quote_chars(FILE_PATH_LITERAL("'\"")); |
| 574 | std::vector<StringType> wrapper_argv; |
| Lei Zhang | 09b218e5 | 2024-09-12 19:21:55 | [diff] [blame] | 575 | while (std::optional<StringViewType> token = tokenizer.GetNextTokenView()) { |
| 576 | wrapper_argv.emplace_back(token.value()); |
| 577 | } |
| skyostil | d851aa1 | 2017-03-29 17:38:35 | [diff] [blame] | 578 | |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 579 | // Prepend the wrapper and update the switches/arguments |begin_args_|. |
| 580 | argv_.insert(argv_.begin(), wrapper_argv.begin(), wrapper_argv.end()); |
| 581 | begin_args_ += wrapper_argv.size(); |
| [email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 582 | } |
| 583 | |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 584 | #if BUILDFLAG(IS_WIN) |
| Lei Zhang | d638812 | 2024-07-30 20:39:19 | [diff] [blame] | 585 | void CommandLine::ParseFromString(StringViewType command_line) { |
| jdoerrie | 9078701 | 2019-02-07 16:22:02 | [diff] [blame] | 586 | command_line = TrimWhitespace(command_line, TRIM_ALL); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 587 | if (command_line.empty()) { |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 588 | return; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 589 | } |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 590 | raw_command_line_string_ = command_line; |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 591 | |
| 592 | int num_args = 0; |
| 593 | wchar_t** args = NULL; |
| Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 594 | // When calling CommandLineToArgvW, use the apiset if available. |
| David Bienvenu | 2c02069 | 2023-02-27 19:14:51 | [diff] [blame] | 595 | // Doing so will bypass loading shell32.dll on Windows. |
| Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 596 | HMODULE downlevel_shell32_dll = |
| 597 | ::LoadLibraryEx(L"api-ms-win-downlevel-shell32-l1-1-0.dll", nullptr, |
| 598 | LOAD_LIBRARY_SEARCH_SYSTEM32); |
| 599 | if (downlevel_shell32_dll) { |
| 600 | auto command_line_to_argv_w_proc = |
| 601 | reinterpret_cast<decltype(::CommandLineToArgvW)*>( |
| 602 | ::GetProcAddress(downlevel_shell32_dll, "CommandLineToArgvW")); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 603 | if (command_line_to_argv_w_proc) { |
| Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 604 | args = command_line_to_argv_w_proc(command_line.data(), &num_args); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 605 | } |
| Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 606 | } else { |
| 607 | // Since the apiset is not available, allow the delayload of shell32.dll |
| 608 | // to take place. |
| Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 609 | args = ::CommandLineToArgvW(command_line.data(), &num_args); |
| Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 610 | } |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 611 | |
| [email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 612 | DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: " |
| Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 613 | << command_line; |
| Tom Sepez | 8dce0f0f | 2025-02-14 20:37:03 | [diff] [blame] | 614 | StringVector argv(args, UNSAFE_TODO(args + num_args)); |
| jdoerrie | 5c4dc4e | 2019-02-01 18:02:33 | [diff] [blame] | 615 | InitFromArgv(argv); |
| Lei Zhang | d638812 | 2024-07-30 20:39:19 | [diff] [blame] | 616 | raw_command_line_string_ = StringViewType(); |
| [email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 617 | LocalFree(args); |
| Chris Davis | a36cb12 | 2020-01-21 05:34:00 | [diff] [blame] | 618 | |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 619 | if (downlevel_shell32_dll) { |
| Chris Davis | a36cb12 | 2020-01-21 05:34:00 | [diff] [blame] | 620 | ::FreeLibrary(downlevel_shell32_dll); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 621 | } |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 622 | } |
| David Bienvenu | edbb4ce | 2022-09-28 16:02:16 | [diff] [blame] | 623 | |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 624 | #endif // BUILDFLAG(IS_WIN) |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 625 | |
| mayuko aiba | fa08692f | 2024-01-12 03:58:38 | [diff] [blame] | 626 | void CommandLine::AppendSwitchesAndArguments(span<const StringType> argv) { |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 627 | bool parse_switches = true; |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 628 | #if BUILDFLAG(IS_WIN) |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 629 | const bool is_parsed_from_string = !raw_command_line_string_.empty(); |
| 630 | #endif |
| mayuko aiba | fa08692f | 2024-01-12 03:58:38 | [diff] [blame] | 631 | for (StringType arg : argv) { |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 632 | #if BUILDFLAG(IS_WIN) |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 633 | arg = CommandLine::StringType(TrimWhitespace(arg, TRIM_ALL)); |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 634 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 635 | TrimWhitespaceASCII(arg, TRIM_ALL, &arg); |
| [email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 636 | #endif |
| [email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 637 | |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 638 | CommandLine::StringType switch_string; |
| 639 | CommandLine::StringType switch_value; |
| 640 | parse_switches &= (arg != kSwitchTerminator); |
| 641 | if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) { |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 642 | #if BUILDFLAG(IS_WIN) |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 643 | if (is_parsed_from_string && |
| 644 | IsSwitchWithKey(switch_string, kSingleArgument)) { |
| 645 | ParseAsSingleArgument(switch_string); |
| 646 | return; |
| 647 | } |
| 648 | AppendSwitchNative(WideToUTF8(switch_string), switch_value); |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 649 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 650 | AppendSwitchNative(switch_string, switch_value); |
| 651 | #else |
| 652 | #error Unsupported platform |
| 653 | #endif |
| 654 | } else { |
| 655 | AppendArgNative(arg); |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
| Jesse McKenna | 185ceda2 | 2021-03-10 06:47:55 | [diff] [blame] | 660 | CommandLine::StringType CommandLine::GetArgumentsStringInternal( |
| 661 | bool allow_unsafe_insert_sequences) const { |
| 662 | StringType params; |
| 663 | // Append switches and arguments. |
| 664 | bool parse_switches = true; |
| David Bienvenu | 6d3c35fe2 | 2022-11-22 00:25:48 | [diff] [blame] | 665 | #if BUILDFLAG(IS_WIN) |
| 666 | bool appended_single_argument_switch = false; |
| 667 | #endif |
| 668 | |
| Jesse McKenna | 185ceda2 | 2021-03-10 06:47:55 | [diff] [blame] | 669 | for (size_t i = 1; i < argv_.size(); ++i) { |
| 670 | StringType arg = argv_[i]; |
| 671 | StringType switch_string; |
| 672 | StringType switch_value; |
| 673 | parse_switches &= arg != kSwitchTerminator; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 674 | if (i > 1) { |
| Jesse McKenna | 185ceda2 | 2021-03-10 06:47:55 | [diff] [blame] | 675 | params.append(FILE_PATH_LITERAL(" ")); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 676 | } |
| Jesse McKenna | 185ceda2 | 2021-03-10 06:47:55 | [diff] [blame] | 677 | if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) { |
| 678 | params.append(switch_string); |
| 679 | if (!switch_value.empty()) { |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 680 | #if BUILDFLAG(IS_WIN) |
| S. Ganesh | f2c7cb73 | 2022-12-16 22:54:42 | [diff] [blame] | 681 | switch_value = QuoteForCommandLineToArgvWInternal( |
| Jesse McKenna | 185ceda2 | 2021-03-10 06:47:55 | [diff] [blame] | 682 | switch_value, allow_unsafe_insert_sequences); |
| 683 | #endif |
| 684 | params.append(kSwitchValueSeparator + switch_value); |
| 685 | } |
| 686 | } else { |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 687 | #if BUILDFLAG(IS_WIN) |
| David Bienvenu | 6d3c35fe2 | 2022-11-22 00:25:48 | [diff] [blame] | 688 | if (has_single_argument_switch_) { |
| 689 | // Check that we don't have multiple arguments when |
| 690 | // `has_single_argument_switch_` is true. |
| 691 | DCHECK(!appended_single_argument_switch); |
| 692 | appended_single_argument_switch = true; |
| 693 | params.append(base::StrCat( |
| 694 | {kSwitchPrefixes[0], kSingleArgument, FILE_PATH_LITERAL(" ")})); |
| 695 | } else { |
| S. Ganesh | f2c7cb73 | 2022-12-16 22:54:42 | [diff] [blame] | 696 | arg = QuoteForCommandLineToArgvWInternal(arg, |
| 697 | allow_unsafe_insert_sequences); |
| David Bienvenu | 6d3c35fe2 | 2022-11-22 00:25:48 | [diff] [blame] | 698 | } |
| Jesse McKenna | 185ceda2 | 2021-03-10 06:47:55 | [diff] [blame] | 699 | #endif |
| 700 | params.append(arg); |
| 701 | } |
| 702 | } |
| 703 | return params; |
| 704 | } |
| 705 | |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 706 | CommandLine::StringType CommandLine::GetCommandLineString() const { |
| mgiuca | c974d510 | 2014-10-01 09:24:51 | [diff] [blame] | 707 | StringType string(argv_[0]); |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 708 | #if BUILDFLAG(IS_WIN) |
| S. Ganesh | f2c7cb73 | 2022-12-16 22:54:42 | [diff] [blame] | 709 | string = QuoteForCommandLineToArgvWInternal( |
| 710 | string, |
| 711 | /*allow_unsafe_insert_sequences=*/false); |
| mgiuca | c974d510 | 2014-10-01 09:24:51 | [diff] [blame] | 712 | #endif |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 713 | StringType params(GetArgumentsString()); |
| mgiuca | c974d510 | 2014-10-01 09:24:51 | [diff] [blame] | 714 | if (!params.empty()) { |
| Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 715 | string.append(FILE_PATH_LITERAL(" ")); |
| mgiuca | c974d510 | 2014-10-01 09:24:51 | [diff] [blame] | 716 | string.append(params); |
| 717 | } |
| 718 | return string; |
| 719 | } |
| 720 | |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 721 | #if BUILDFLAG(IS_WIN) |
| S. Ganesh | f2c7cb73 | 2022-12-16 22:54:42 | [diff] [blame] | 722 | // static |
| 723 | std::wstring CommandLine::QuoteForCommandLineToArgvW(const std::wstring& arg) { |
| 724 | return QuoteForCommandLineToArgvWInternal( |
| 725 | arg, /*allow_unsafe_insert_sequences=*/false); |
| 726 | } |
| 727 | |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 728 | // NOTE: this function is used to set Chrome's open command in the registry |
| 729 | // during update. Any change to the syntax must be compatible with the prior |
| 730 | // version (i.e., any new syntax must be understood by older browsers expecting |
| 731 | // the old syntax, and the new browser must still handle the old syntax), as |
| 732 | // old versions are likely to persist, e.g., immediately after background |
| 733 | // update, when parsing command lines for other channels, when uninstalling web |
| 734 | // applications installed using the old syntax, etc. |
| 735 | CommandLine::StringType CommandLine::GetCommandLineStringForShell() const { |
| 736 | DCHECK(GetArgs().empty()); |
| 737 | StringType command_line_string = GetCommandLineString(); |
| 738 | return command_line_string + FILE_PATH_LITERAL(" ") + |
| Jan Wilken Dörrie | 4aa966b | 2020-08-24 18:20:29 | [diff] [blame] | 739 | StringType(kSwitchPrefixes[0]) + kSingleArgument + |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 740 | FILE_PATH_LITERAL(" %1"); |
| 741 | } |
| Jesse McKenna | 185ceda2 | 2021-03-10 06:47:55 | [diff] [blame] | 742 | |
| 743 | CommandLine::StringType |
| 744 | CommandLine::GetCommandLineStringWithUnsafeInsertSequences() const { |
| 745 | StringType string(argv_[0]); |
| S. Ganesh | f2c7cb73 | 2022-12-16 22:54:42 | [diff] [blame] | 746 | string = QuoteForCommandLineToArgvWInternal( |
| 747 | string, |
| 748 | /*allow_unsafe_insert_sequences=*/true); |
| Jesse McKenna | 185ceda2 | 2021-03-10 06:47:55 | [diff] [blame] | 749 | StringType params( |
| 750 | GetArgumentsStringInternal(/*allow_unsafe_insert_sequences=*/true)); |
| 751 | if (!params.empty()) { |
| 752 | string.append(FILE_PATH_LITERAL(" ")); |
| 753 | string.append(params); |
| 754 | } |
| 755 | return string; |
| 756 | } |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 757 | #endif // BUILDFLAG(IS_WIN) |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 758 | |
| 759 | CommandLine::StringType CommandLine::GetArgumentsString() const { |
| Jesse McKenna | 185ceda2 | 2021-03-10 06:47:55 | [diff] [blame] | 760 | return GetArgumentsStringInternal(/*allow_unsafe_insert_sequences=*/false); |
| mgiuca | c974d510 | 2014-10-01 09:24:51 | [diff] [blame] | 761 | } |
| 762 | |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 763 | #if BUILDFLAG(IS_WIN) |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 764 | void CommandLine::ParseAsSingleArgument( |
| 765 | const CommandLine::StringType& single_arg_switch) { |
| 766 | DCHECK(!raw_command_line_string_.empty()); |
| 767 | |
| 768 | // Remove any previously parsed arguments. |
| Peter Kasting | de85e74 | 2022-06-01 17:41:54 | [diff] [blame] | 769 | argv_.resize(static_cast<size_t>(begin_args_)); |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 770 | |
| 771 | // Locate "--single-argument" in the process's raw command line. Results are |
| 772 | // unpredictable if "--single-argument" appears as part of a previous |
| 773 | // argument or switch. |
| 774 | const size_t single_arg_switch_position = |
| 775 | raw_command_line_string_.find(single_arg_switch); |
| 776 | DCHECK_NE(single_arg_switch_position, StringType::npos); |
| 777 | |
| 778 | // Append the portion of the raw command line that starts one character past |
| 779 | // "--single-argument" as the one and only argument, or return if no |
| 780 | // argument is present. |
| 781 | const size_t arg_position = |
| 782 | single_arg_switch_position + single_arg_switch.length() + 1; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 783 | if (arg_position >= raw_command_line_string_.length()) { |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 784 | return; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 785 | } |
| David Bienvenu | edbb4ce | 2022-09-28 16:02:16 | [diff] [blame] | 786 | has_single_argument_switch_ = true; |
| Lei Zhang | d638812 | 2024-07-30 20:39:19 | [diff] [blame] | 787 | const StringViewType arg = raw_command_line_string_.substr(arg_position); |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 788 | if (!arg.empty()) { |
| Wez | 65018d0 | 2021-05-20 15:47:45 | [diff] [blame] | 789 | AppendArgNative(arg); |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 790 | } |
| 791 | } |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 792 | #endif // BUILDFLAG(IS_WIN) |
| Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 793 | |
| Will Harris | ee9f0a2 | 2023-03-02 21:38:58 | [diff] [blame] | 794 | void CommandLine::DetachFromCurrentSequence() { |
| 795 | #if BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS) |
| 796 | sequence_checker_.Detach(); |
| 797 | #endif // BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS) |
| 798 | } |
| 799 | |
| [email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 800 | } // namespace base |