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