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