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