erikwright@chromium.org | 506c72b | 2012-02-28 03:38:12 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [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 | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
evanm@google.com | 91cdff8 | 2008-08-08 05:07:32 +0900 | [diff] [blame] | 5 | #include "base/command_line.h" |
| 6 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 7 | #include <algorithm> |
jhawkins@chromium.org | 8e73d06 | 2011-04-05 03:04:37 +0900 | [diff] [blame] | 8 | #include <ostream> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 9 | |
brettw@chromium.org | 59eef1f | 2013-02-24 14:40:52 +0900 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 11 | #include "base/logging.h" |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame^] | 12 | #include "base/macros.h" |
tfarina@chromium.org | 63aaf3f | 2013-02-08 08:01:39 +0900 | [diff] [blame] | 13 | #include "base/strings/string_split.h" |
avi@chromium.org | 94bd573 | 2013-06-11 22:36:37 +0900 | [diff] [blame] | 14 | #include "base/strings/string_util.h" |
avi@chromium.org | 17f6062 | 2013-06-08 03:37:07 +0900 | [diff] [blame] | 15 | #include "base/strings/utf_string_conversions.h" |
brettw@chromium.org | 6a3802c | 2010-12-30 06:06:43 +0900 | [diff] [blame] | 16 | #include "build/build_config.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 17 | |
brettw@chromium.org | 6a3802c | 2010-12-30 06:06:43 +0900 | [diff] [blame] | 18 | #if defined(OS_WIN) |
| 19 | #include <windows.h> |
| 20 | #include <shellapi.h> |
mdm@chromium.org | 18175a0 | 2009-09-11 03:02:17 +0900 | [diff] [blame] | 21 | #endif |
| 22 | |
brettw@chromium.org | 3727389 | 2014-03-18 08:07:15 +0900 | [diff] [blame] | 23 | namespace base { |
brettw@chromium.org | c02b603 | 2013-02-16 13:12:26 +0900 | [diff] [blame] | 24 | |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 25 | CommandLine* CommandLine::current_process_commandline_ = NULL; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 26 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 27 | namespace { |
brettw@chromium.org | 3727389 | 2014-03-18 08:07:15 +0900 | [diff] [blame] | 28 | |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 29 | const CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--"); |
| 30 | const CommandLine::CharType kSwitchValueSeparator[] = FILE_PATH_LITERAL("="); |
brettw@chromium.org | 40b2d58 | 2013-09-26 08:36:00 +0900 | [diff] [blame] | 31 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [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. |
pinkerton@google.com | 0c0e01c | 2008-08-09 05:46:21 +0900 | [diff] [blame] | 34 | #if defined(OS_WIN) |
brettw@chromium.org | 40b2d58 | 2013-09-26 08:36:00 +0900 | [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. |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 38 | const CommandLine::CharType* const kSwitchPrefixes[] = {L"--", L"-", L"/"}; |
pinkerton@google.com | 0c0e01c | 2008-08-09 05:46:21 +0900 | [diff] [blame] | 39 | #elif defined(OS_POSIX) |
evanm@google.com | 638e9fb | 2008-08-12 10:14:37 +0900 | [diff] [blame] | 40 | // Unixes don't use slash as a switch. |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 41 | const CommandLine::CharType* const kSwitchPrefixes[] = {"--", "-"}; |
pinkerton@google.com | 0c0e01c | 2008-08-09 05:46:21 +0900 | [diff] [blame] | 42 | #endif |
brettw@chromium.org | 40b2d58 | 2013-09-26 08:36:00 +0900 | [diff] [blame] | 43 | size_t switch_prefix_count = arraysize(kSwitchPrefixes); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 44 | |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 45 | size_t GetSwitchPrefixLength(const CommandLine::StringType& string) { |
brettw@chromium.org | 40b2d58 | 2013-09-26 08:36:00 +0900 | [diff] [blame] | 46 | for (size_t i = 0; i < switch_prefix_count; ++i) { |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 47 | CommandLine::StringType prefix(kSwitchPrefixes[i]); |
joth@chromium.org | b23d184 | 2011-09-14 00:45:34 +0900 | [diff] [blame] | 48 | if (string.compare(0, prefix.length(), prefix) == 0) |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 49 | return prefix.length(); |
| 50 | } |
| 51 | return 0; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 52 | } |
msw@chromium.org | da7a548 | 2011-02-20 15:33:04 +0900 | [diff] [blame] | 53 | |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [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(); |
jochen@chromium.org | 1e9efb1 | 2012-10-19 15:19:59 +0900 | [diff] [blame] | 61 | size_t prefix_length = GetSwitchPrefixLength(string); |
| 62 | if (prefix_length == 0 || prefix_length == string.length()) |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [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. |
thestig | 2d5b2e3 | 2014-12-05 07:14:22 +0900 | [diff] [blame] | 73 | void AppendSwitchesAndArguments(CommandLine* command_line, |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 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]; |
tfarina | b6894c1 | 2015-12-06 22:25:41 +0900 | [diff] [blame] | 78 | #if defined(OS_WIN) |
brettw@chromium.org | 3727389 | 2014-03-18 08:07:15 +0900 | [diff] [blame] | 79 | TrimWhitespace(arg, TRIM_ALL, &arg); |
tfarina | b6894c1 | 2015-12-06 22:25:41 +0900 | [diff] [blame] | 80 | #else |
| 81 | TrimWhitespaceASCII(arg, TRIM_ALL, &arg); |
| 82 | #endif |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 83 | |
| 84 | CommandLine::StringType switch_string; |
| 85 | CommandLine::StringType switch_value; |
| 86 | parse_switches &= (arg != kSwitchTerminator); |
| 87 | if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) { |
| 88 | #if defined(OS_WIN) |
thestig | 2d5b2e3 | 2014-12-05 07:14:22 +0900 | [diff] [blame] | 89 | command_line->AppendSwitchNative(UTF16ToASCII(switch_string), |
| 90 | switch_value); |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 91 | #elif defined(OS_POSIX) |
thestig | 2d5b2e3 | 2014-12-05 07:14:22 +0900 | [diff] [blame] | 92 | command_line->AppendSwitchNative(switch_string, switch_value); |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 93 | #endif |
| 94 | } else { |
thestig | 2d5b2e3 | 2014-12-05 07:14:22 +0900 | [diff] [blame] | 95 | command_line->AppendArgNative(arg); |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 100 | #if defined(OS_WIN) |
| 101 | // Quote a string as necessary for CommandLineToArgvW compatiblity *on Windows*. |
thestig | 2d5b2e3 | 2014-12-05 07:14:22 +0900 | [diff] [blame] | 102 | string16 QuoteForCommandLineToArgvW(const string16& arg, |
| 103 | bool quote_placeholders) { |
msw@chromium.org | da7a548 | 2011-02-20 15:33:04 +0900 | [diff] [blame] | 104 | // We follow the quoting rules of CommandLineToArgvW. |
| 105 | // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx |
thestig | 2d5b2e3 | 2014-12-05 07:14:22 +0900 | [diff] [blame] | 106 | string16 quotable_chars(L" \\\""); |
mgiuca | 90d7449 | 2014-10-01 18:24:51 +0900 | [diff] [blame] | 107 | // We may also be required to quote '%', which is commonly used in a command |
| 108 | // line as a placeholder. (It may be substituted for a string with spaces.) |
| 109 | if (quote_placeholders) |
| 110 | quotable_chars.push_back(L'%'); |
thestig | 2d5b2e3 | 2014-12-05 07:14:22 +0900 | [diff] [blame] | 111 | if (arg.find_first_of(quotable_chars) == string16::npos) { |
msw@chromium.org | da7a548 | 2011-02-20 15:33:04 +0900 | [diff] [blame] | 112 | // No quoting necessary. |
| 113 | return arg; |
| 114 | } |
| 115 | |
thestig | 2d5b2e3 | 2014-12-05 07:14:22 +0900 | [diff] [blame] | 116 | string16 out; |
msw@chromium.org | da7a548 | 2011-02-20 15:33:04 +0900 | [diff] [blame] | 117 | out.push_back(L'"'); |
| 118 | for (size_t i = 0; i < arg.size(); ++i) { |
| 119 | if (arg[i] == '\\') { |
| 120 | // Find the extent of this run of backslashes. |
| 121 | size_t start = i, end = start + 1; |
thestig | 2d5b2e3 | 2014-12-05 07:14:22 +0900 | [diff] [blame] | 122 | for (; end < arg.size() && arg[end] == '\\'; ++end) {} |
msw@chromium.org | da7a548 | 2011-02-20 15:33:04 +0900 | [diff] [blame] | 123 | size_t backslash_count = end - start; |
| 124 | |
| 125 | // Backslashes are escapes only if the run is followed by a double quote. |
| 126 | // Since we also will end the string with a double quote, we escape for |
| 127 | // either a double quote or the end of the string. |
| 128 | if (end == arg.size() || arg[end] == '"') { |
| 129 | // To quote, we need to output 2x as many backslashes. |
| 130 | backslash_count *= 2; |
| 131 | } |
| 132 | for (size_t j = 0; j < backslash_count; ++j) |
| 133 | out.push_back('\\'); |
| 134 | |
| 135 | // Advance i to one before the end to balance i++ in loop. |
| 136 | i = end - 1; |
| 137 | } else if (arg[i] == '"') { |
| 138 | out.push_back('\\'); |
| 139 | out.push_back('"'); |
| 140 | } else { |
| 141 | out.push_back(arg[i]); |
| 142 | } |
| 143 | } |
| 144 | out.push_back('"'); |
| 145 | |
| 146 | return out; |
| 147 | } |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 148 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 149 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 150 | } // namespace |
| 151 | |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 152 | CommandLine::CommandLine(NoProgram no_program) |
| 153 | : argv_(1), |
| 154 | begin_args_(1) { |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 155 | } |
| 156 | |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 157 | CommandLine::CommandLine(const FilePath& program) |
| 158 | : argv_(1), |
| 159 | begin_args_(1) { |
| 160 | SetProgram(program); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 161 | } |
| 162 | |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 163 | CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv) |
| 164 | : argv_(1), |
| 165 | begin_args_(1) { |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 166 | InitFromArgv(argc, argv); |
| 167 | } |
| 168 | |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 169 | CommandLine::CommandLine(const StringVector& argv) |
| 170 | : argv_(1), |
| 171 | begin_args_(1) { |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 172 | InitFromArgv(argv); |
| 173 | } |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 174 | |
jackhou | a5995af | 2015-05-21 13:48:00 +0900 | [diff] [blame] | 175 | CommandLine::CommandLine(const CommandLine& other) |
| 176 | : argv_(other.argv_), |
| 177 | switches_(other.switches_), |
| 178 | begin_args_(other.begin_args_) { |
| 179 | ResetStringPieces(); |
| 180 | } |
| 181 | |
| 182 | CommandLine& CommandLine::operator=(const CommandLine& other) { |
| 183 | argv_ = other.argv_; |
| 184 | switches_ = other.switches_; |
| 185 | begin_args_ = other.begin_args_; |
| 186 | ResetStringPieces(); |
| 187 | return *this; |
| 188 | } |
| 189 | |
msw@chromium.org | 96b695b | 2011-03-02 05:47:58 +0900 | [diff] [blame] | 190 | CommandLine::~CommandLine() { |
| 191 | } |
| 192 | |
brettw@chromium.org | 40b2d58 | 2013-09-26 08:36:00 +0900 | [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 | a7ea666 | 2015-03-10 07:27:25 +0900 | [diff] [blame] | 197 | DCHECK_EQ(wcscmp(kSwitchPrefixes[arraysize(kSwitchPrefixes) - 1], L"/"), 0); |
brettw@chromium.org | 40b2d58 | 2013-09-26 08:36:00 +0900 | [diff] [blame] | 198 | switch_prefix_count = arraysize(kSwitchPrefixes) - 1; |
| 199 | } |
| 200 | #endif |
| 201 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 202 | // static |
erikwright@chromium.org | 506c72b | 2012-02-28 03:38:12 +0900 | [diff] [blame] | 203 | bool CommandLine::Init(int argc, const char* const* argv) { |
rvargas@google.com | 58a1184 | 2011-07-14 03:03:34 +0900 | [diff] [blame] | 204 | if (current_process_commandline_) { |
| 205 | // If this is intentional, Reset() must be called first. If we are using |
| 206 | // the shared build mode, we have to share a single object across multiple |
| 207 | // shared libraries. |
erikwright@chromium.org | 506c72b | 2012-02-28 03:38:12 +0900 | [diff] [blame] | 208 | return false; |
rvargas@google.com | 58a1184 | 2011-07-14 03:03:34 +0900 | [diff] [blame] | 209 | } |
| 210 | |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 211 | current_process_commandline_ = new CommandLine(NO_PROGRAM); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 212 | #if defined(OS_WIN) |
| 213 | current_process_commandline_->ParseFromString(::GetCommandLineW()); |
| 214 | #elif defined(OS_POSIX) |
| 215 | current_process_commandline_->InitFromArgv(argc, argv); |
| 216 | #endif |
erikwright@chromium.org | 506c72b | 2012-02-28 03:38:12 +0900 | [diff] [blame] | 217 | |
| 218 | return true; |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | // static |
| 222 | void CommandLine::Reset() { |
| 223 | DCHECK(current_process_commandline_); |
| 224 | delete current_process_commandline_; |
| 225 | current_process_commandline_ = NULL; |
| 226 | } |
| 227 | |
| 228 | // static |
| 229 | CommandLine* CommandLine::ForCurrentProcess() { |
| 230 | DCHECK(current_process_commandline_); |
| 231 | return current_process_commandline_; |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 232 | } |
| 233 | |
vollick@chromium.org | 4ff9adc | 2013-07-12 08:10:40 +0900 | [diff] [blame] | 234 | // static |
| 235 | bool CommandLine::InitializedForCurrentProcess() { |
| 236 | return !!current_process_commandline_; |
| 237 | } |
| 238 | |
evanm@google.com | 91cdff8 | 2008-08-08 05:07:32 +0900 | [diff] [blame] | 239 | #if defined(OS_WIN) |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 240 | // static |
thestig | 2d5b2e3 | 2014-12-05 07:14:22 +0900 | [diff] [blame] | 241 | CommandLine CommandLine::FromString(const string16& command_line) { |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 242 | CommandLine cmd(NO_PROGRAM); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 243 | cmd.ParseFromString(command_line); |
| 244 | return cmd; |
| 245 | } |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 246 | #endif |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 247 | |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 248 | void CommandLine::InitFromArgv(int argc, |
| 249 | const CommandLine::CharType* const* argv) { |
| 250 | StringVector new_argv; |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 251 | for (int i = 0; i < argc; ++i) |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 252 | new_argv.push_back(argv[i]); |
| 253 | InitFromArgv(new_argv); |
evan@chromium.org | c4fee2c | 2009-10-27 07:39:33 +0900 | [diff] [blame] | 254 | } |
| 255 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 256 | void CommandLine::InitFromArgv(const StringVector& argv) { |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 257 | argv_ = StringVector(1); |
bartfab@chromium.org | ad4cf18 | 2013-06-18 17:19:18 +0900 | [diff] [blame] | 258 | switches_.clear(); |
jackhou | a5995af | 2015-05-21 13:48:00 +0900 | [diff] [blame] | 259 | switches_by_stringpiece_.clear(); |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 260 | begin_args_ = 1; |
| 261 | SetProgram(argv.empty() ? FilePath() : FilePath(argv[0])); |
thestig | 2d5b2e3 | 2014-12-05 07:14:22 +0900 | [diff] [blame] | 262 | AppendSwitchesAndArguments(this, argv); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 263 | } |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 264 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 265 | FilePath CommandLine::GetProgram() const { |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 266 | return FilePath(argv_[0]); |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void CommandLine::SetProgram(const FilePath& program) { |
tfarina | b6894c1 | 2015-12-06 22:25:41 +0900 | [diff] [blame] | 270 | #if defined(OS_WIN) |
brettw@chromium.org | 3727389 | 2014-03-18 08:07:15 +0900 | [diff] [blame] | 271 | TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]); |
tfarina | b6894c1 | 2015-12-06 22:25:41 +0900 | [diff] [blame] | 272 | #else |
| 273 | TrimWhitespaceASCII(program.value(), TRIM_ALL, &argv_[0]); |
| 274 | #endif |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 275 | } |
| 276 | |
jackhou | a5995af | 2015-05-21 13:48:00 +0900 | [diff] [blame] | 277 | bool CommandLine::HasSwitch(const base::StringPiece& switch_string) const { |
brettw | 200ab41 | 2015-08-11 04:07:51 +0900 | [diff] [blame] | 278 | DCHECK_EQ(ToLowerASCII(switch_string), switch_string); |
jackhou | a5995af | 2015-05-21 13:48:00 +0900 | [diff] [blame] | 279 | return switches_by_stringpiece_.find(switch_string) != |
| 280 | switches_by_stringpiece_.end(); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 281 | } |
| 282 | |
jackhou | a5995af | 2015-05-21 13:48:00 +0900 | [diff] [blame] | 283 | bool CommandLine::HasSwitch(const char switch_constant[]) const { |
| 284 | return HasSwitch(base::StringPiece(switch_constant)); |
tapted | 5762d50 | 2015-03-30 12:57:10 +0900 | [diff] [blame] | 285 | } |
| 286 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 287 | std::string CommandLine::GetSwitchValueASCII( |
jackhou | a5995af | 2015-05-21 13:48:00 +0900 | [diff] [blame] | 288 | const base::StringPiece& switch_string) const { |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 289 | StringType value = GetSwitchValueNative(switch_string); |
brettw@chromium.org | 9339740 | 2014-03-18 08:55:43 +0900 | [diff] [blame] | 290 | if (!IsStringASCII(value)) { |
brettw@chromium.org | 5faed3c | 2011-10-27 06:48:00 +0900 | [diff] [blame] | 291 | DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII."; |
| 292 | return std::string(); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 293 | } |
| 294 | #if defined(OS_WIN) |
brettw@chromium.org | 3727389 | 2014-03-18 08:07:15 +0900 | [diff] [blame] | 295 | return UTF16ToASCII(value); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 296 | #else |
| 297 | return value; |
| 298 | #endif |
| 299 | } |
| 300 | |
| 301 | FilePath CommandLine::GetSwitchValuePath( |
jackhou | a5995af | 2015-05-21 13:48:00 +0900 | [diff] [blame] | 302 | const base::StringPiece& switch_string) const { |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 303 | return FilePath(GetSwitchValueNative(switch_string)); |
| 304 | } |
| 305 | |
| 306 | CommandLine::StringType CommandLine::GetSwitchValueNative( |
jackhou | a5995af | 2015-05-21 13:48:00 +0900 | [diff] [blame] | 307 | const base::StringPiece& switch_string) const { |
brettw | 200ab41 | 2015-08-11 04:07:51 +0900 | [diff] [blame] | 308 | DCHECK_EQ(ToLowerASCII(switch_string), switch_string); |
jackhou | a5995af | 2015-05-21 13:48:00 +0900 | [diff] [blame] | 309 | auto result = switches_by_stringpiece_.find(switch_string); |
| 310 | return result == switches_by_stringpiece_.end() ? StringType() |
| 311 | : *(result->second); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 312 | } |
| 313 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 314 | void CommandLine::AppendSwitch(const std::string& switch_string) { |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 315 | AppendSwitchNative(switch_string, StringType()); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void CommandLine::AppendSwitchPath(const std::string& switch_string, |
| 319 | const FilePath& path) { |
| 320 | AppendSwitchNative(switch_string, path.value()); |
| 321 | } |
| 322 | |
| 323 | void CommandLine::AppendSwitchNative(const std::string& switch_string, |
| 324 | const CommandLine::StringType& value) { |
| 325 | #if defined(OS_WIN) |
brettw | 200ab41 | 2015-08-11 04:07:51 +0900 | [diff] [blame] | 326 | const std::string switch_key = ToLowerASCII(switch_string); |
thestig | 2d5b2e3 | 2014-12-05 07:14:22 +0900 | [diff] [blame] | 327 | StringType combined_switch_string(ASCIIToUTF16(switch_key)); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 328 | #elif defined(OS_POSIX) |
jackhou | a5995af | 2015-05-21 13:48:00 +0900 | [diff] [blame] | 329 | const std::string& switch_key = switch_string; |
| 330 | StringType combined_switch_string(switch_key); |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 331 | #endif |
| 332 | size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); |
jackhou | a5995af | 2015-05-21 13:48:00 +0900 | [diff] [blame] | 333 | auto insertion = |
| 334 | switches_.insert(make_pair(switch_key.substr(prefix_length), value)); |
| 335 | if (!insertion.second) |
| 336 | insertion.first->second = value; |
| 337 | switches_by_stringpiece_[insertion.first->first] = &(insertion.first->second); |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 338 | // Preserve existing switch prefixes in |argv_|; only append one if necessary. |
| 339 | if (prefix_length == 0) |
| 340 | combined_switch_string = kSwitchPrefixes[0] + combined_switch_string; |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 341 | if (!value.empty()) |
| 342 | combined_switch_string += kSwitchValueSeparator + value; |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 343 | // Append the switch and update the switches/arguments divider |begin_args_|. |
| 344 | argv_.insert(argv_.begin() + begin_args_++, combined_switch_string); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | void CommandLine::AppendSwitchASCII(const std::string& switch_string, |
| 348 | const std::string& value_string) { |
| 349 | #if defined(OS_WIN) |
thestig | 2d5b2e3 | 2014-12-05 07:14:22 +0900 | [diff] [blame] | 350 | AppendSwitchNative(switch_string, ASCIIToUTF16(value_string)); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 351 | #elif defined(OS_POSIX) |
| 352 | AppendSwitchNative(switch_string, value_string); |
| 353 | #endif |
| 354 | } |
| 355 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 356 | void CommandLine::CopySwitchesFrom(const CommandLine& source, |
| 357 | const char* const switches[], |
| 358 | size_t count) { |
| 359 | for (size_t i = 0; i < count; ++i) { |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 360 | if (source.HasSwitch(switches[i])) |
| 361 | AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i])); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
msw@chromium.org | a25adf4 | 2011-07-14 08:41:22 +0900 | [diff] [blame] | 365 | CommandLine::StringVector CommandLine::GetArgs() const { |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 366 | // Gather all arguments after the last switch (may include kSwitchTerminator). |
| 367 | StringVector args(argv_.begin() + begin_args_, argv_.end()); |
| 368 | // Erase only the first kSwitchTerminator (maybe "--" is a legitimate page?) |
| 369 | StringVector::iterator switch_terminator = |
| 370 | std::find(args.begin(), args.end(), kSwitchTerminator); |
| 371 | if (switch_terminator != args.end()) |
| 372 | args.erase(switch_terminator); |
| 373 | return args; |
| 374 | } |
| 375 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 376 | void CommandLine::AppendArg(const std::string& value) { |
| 377 | #if defined(OS_WIN) |
brettw@chromium.org | 9339740 | 2014-03-18 08:55:43 +0900 | [diff] [blame] | 378 | DCHECK(IsStringUTF8(value)); |
| 379 | AppendArgNative(UTF8ToWide(value)); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 380 | #elif defined(OS_POSIX) |
| 381 | AppendArgNative(value); |
| 382 | #endif |
| 383 | } |
| 384 | |
| 385 | void CommandLine::AppendArgPath(const FilePath& path) { |
| 386 | AppendArgNative(path.value()); |
| 387 | } |
| 388 | |
| 389 | void CommandLine::AppendArgNative(const CommandLine::StringType& value) { |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 390 | argv_.push_back(value); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | void CommandLine::AppendArguments(const CommandLine& other, |
| 394 | bool include_program) { |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 395 | if (include_program) |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 396 | SetProgram(other.GetProgram()); |
thestig | 2d5b2e3 | 2014-12-05 07:14:22 +0900 | [diff] [blame] | 397 | AppendSwitchesAndArguments(this, other.argv()); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) { |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 401 | if (wrapper.empty()) |
| 402 | return; |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 403 | // The wrapper may have embedded arguments (like "gdb --args"). In this case, |
| 404 | // we don't pretend to do anything fancy, we just split on spaces. |
brettw | ed9cfcf | 2015-08-08 09:28:47 +0900 | [diff] [blame] | 405 | StringVector wrapper_argv = SplitString( |
| 406 | wrapper, FilePath::StringType(1, ' '), base::TRIM_WHITESPACE, |
| 407 | base::SPLIT_WANT_ALL); |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 408 | // Prepend the wrapper and update the switches/arguments |begin_args_|. |
| 409 | argv_.insert(argv_.begin(), wrapper_argv.begin(), wrapper_argv.end()); |
| 410 | begin_args_ += wrapper_argv.size(); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | #if defined(OS_WIN) |
thestig | 2d5b2e3 | 2014-12-05 07:14:22 +0900 | [diff] [blame] | 414 | void CommandLine::ParseFromString(const string16& command_line) { |
| 415 | string16 command_line_string; |
brettw@chromium.org | 3727389 | 2014-03-18 08:07:15 +0900 | [diff] [blame] | 416 | TrimWhitespace(command_line, TRIM_ALL, &command_line_string); |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 417 | if (command_line_string.empty()) |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 418 | return; |
| 419 | |
| 420 | int num_args = 0; |
| 421 | wchar_t** args = NULL; |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 422 | args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args); |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 423 | |
brettw@chromium.org | 5faed3c | 2011-10-27 06:48:00 +0900 | [diff] [blame] | 424 | DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: " |
brettw@chromium.org | 3727389 | 2014-03-18 08:07:15 +0900 | [diff] [blame] | 425 | << UTF16ToUTF8(command_line); |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 426 | InitFromArgv(num_args, args); |
| 427 | LocalFree(args); |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 428 | } |
evanm@google.com | 91cdff8 | 2008-08-08 05:07:32 +0900 | [diff] [blame] | 429 | #endif |
brettw@chromium.org | 3727389 | 2014-03-18 08:07:15 +0900 | [diff] [blame] | 430 | |
mgiuca | 90d7449 | 2014-10-01 18:24:51 +0900 | [diff] [blame] | 431 | CommandLine::StringType CommandLine::GetCommandLineStringInternal( |
| 432 | bool quote_placeholders) const { |
| 433 | StringType string(argv_[0]); |
| 434 | #if defined(OS_WIN) |
| 435 | string = QuoteForCommandLineToArgvW(string, quote_placeholders); |
| 436 | #endif |
| 437 | StringType params(GetArgumentsStringInternal(quote_placeholders)); |
| 438 | if (!params.empty()) { |
| 439 | string.append(StringType(FILE_PATH_LITERAL(" "))); |
| 440 | string.append(params); |
| 441 | } |
| 442 | return string; |
| 443 | } |
| 444 | |
| 445 | CommandLine::StringType CommandLine::GetArgumentsStringInternal( |
| 446 | bool quote_placeholders) const { |
| 447 | StringType params; |
| 448 | // Append switches and arguments. |
| 449 | bool parse_switches = true; |
| 450 | for (size_t i = 1; i < argv_.size(); ++i) { |
| 451 | StringType arg = argv_[i]; |
| 452 | StringType switch_string; |
| 453 | StringType switch_value; |
| 454 | parse_switches &= arg != kSwitchTerminator; |
| 455 | if (i > 1) |
| 456 | params.append(StringType(FILE_PATH_LITERAL(" "))); |
| 457 | if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) { |
| 458 | params.append(switch_string); |
| 459 | if (!switch_value.empty()) { |
| 460 | #if defined(OS_WIN) |
| 461 | switch_value = |
| 462 | QuoteForCommandLineToArgvW(switch_value, quote_placeholders); |
| 463 | #endif |
| 464 | params.append(kSwitchValueSeparator + switch_value); |
| 465 | } |
thestig | 2d5b2e3 | 2014-12-05 07:14:22 +0900 | [diff] [blame] | 466 | } else { |
mgiuca | 90d7449 | 2014-10-01 18:24:51 +0900 | [diff] [blame] | 467 | #if defined(OS_WIN) |
| 468 | arg = QuoteForCommandLineToArgvW(arg, quote_placeholders); |
| 469 | #endif |
| 470 | params.append(arg); |
| 471 | } |
| 472 | } |
| 473 | return params; |
| 474 | } |
| 475 | |
jackhou | a5995af | 2015-05-21 13:48:00 +0900 | [diff] [blame] | 476 | void CommandLine::ResetStringPieces() { |
| 477 | switches_by_stringpiece_.clear(); |
| 478 | for (const auto& entry : switches_) |
| 479 | switches_by_stringpiece_[entry.first] = &(entry.second); |
| 480 | } |
| 481 | |
brettw@chromium.org | 3727389 | 2014-03-18 08:07:15 +0900 | [diff] [blame] | 482 | } // namespace base |