license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 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 | |
| 7 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 8 | #include <windows.h> |
| 9 | #include <shellapi.h> |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 10 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | |
| 12 | #include <algorithm> |
| 13 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | #include "base/logging.h" |
| 15 | #include "base/singleton.h" |
[email protected] | 4bdaceb4 | 2008-08-19 13:19:24 | [diff] [blame] | 16 | #include "base/string_piece.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 17 | #include "base/string_util.h" |
[email protected] | 05b5d79 | 2008-08-07 22:28:24 | [diff] [blame] | 18 | #include "base/sys_string_conversions.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 19 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 20 | CommandLine* CommandLine::current_process_commandline_ = NULL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 21 | |
| 22 | // Since we use a lazy match, make sure that longer versions (like L"--") |
| 23 | // are listed before shorter versions (like L"-") of similar prefixes. |
[email protected] | 5d42633 | 2008-08-08 20:46:21 | [diff] [blame] | 24 | #if defined(OS_WIN) |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 25 | const wchar_t* const kSwitchPrefixes[] = {L"--", L"-", L"/"}; |
| 26 | const wchar_t kSwitchTerminator[] = L"--"; |
| 27 | const wchar_t kSwitchValueSeparator[] = L"="; |
[email protected] | 5d42633 | 2008-08-08 20:46:21 | [diff] [blame] | 28 | #elif defined(OS_POSIX) |
[email protected] | 1a48f31 | 2008-08-12 01:14:37 | [diff] [blame] | 29 | // Unixes don't use slash as a switch. |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 30 | const char* const kSwitchPrefixes[] = {"--", "-"}; |
| 31 | const char kSwitchTerminator[] = "--"; |
| 32 | const char kSwitchValueSeparator[] = "="; |
[email protected] | 5d42633 | 2008-08-08 20:46:21 | [diff] [blame] | 33 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 34 | |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 35 | #if defined(OS_WIN) |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 36 | // Lowercase a string. This is used to lowercase switch names. |
| 37 | // Is this what we really want? It seems crazy to me. I've left it in |
| 38 | // for backwards compatibility on Windows. |
| 39 | static void Lowercase(std::wstring* parameter) { |
| 40 | transform(parameter->begin(), parameter->end(), parameter->begin(), |
| 41 | tolower); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 42 | } |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 43 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 44 | |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 45 | #if defined(OS_WIN) |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 46 | void CommandLine::ParseFromString(const std::wstring& command_line) { |
| 47 | TrimWhitespace(command_line, TRIM_ALL, &command_line_string_); |
| 48 | |
| 49 | if (command_line_string_.empty()) |
| 50 | return; |
| 51 | |
| 52 | int num_args = 0; |
| 53 | wchar_t** args = NULL; |
| 54 | |
| 55 | args = CommandLineToArgvW(command_line_string_.c_str(), &num_args); |
| 56 | |
| 57 | // Populate program_ with the trimmed version of the first arg. |
| 58 | TrimWhitespace(args[0], TRIM_ALL, &program_); |
| 59 | |
| 60 | bool parse_switches = true; |
| 61 | for (int i = 1; i < num_args; ++i) { |
| 62 | std::wstring arg; |
| 63 | TrimWhitespace(args[i], TRIM_ALL, &arg); |
| 64 | |
| 65 | if (!parse_switches) { |
| 66 | loose_values_.push_back(arg); |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | if (arg == kSwitchTerminator) { |
| 71 | parse_switches = false; |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | std::string switch_string; |
| 76 | std::wstring switch_value; |
| 77 | if (IsSwitch(arg, &switch_string, &switch_value)) { |
| 78 | switches_[switch_string] = switch_value; |
| 79 | } else { |
| 80 | loose_values_.push_back(arg); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if (args) |
| 85 | LocalFree(args); |
| 86 | } |
| 87 | CommandLine::CommandLine(const std::wstring& program) { |
| 88 | if (!program.empty()) { |
| 89 | program_ = program; |
| 90 | command_line_string_ = L'"' + program + L'"'; |
| 91 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 92 | } |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 93 | #elif defined(OS_POSIX) |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 94 | CommandLine::CommandLine(int argc, const char* const* argv) { |
| 95 | for (int i = 0; i < argc; ++i) |
| 96 | argv_.push_back(argv[i]); |
| 97 | InitFromArgv(); |
| 98 | } |
| 99 | CommandLine::CommandLine(const std::vector<std::string>& argv) { |
| 100 | argv_ = argv; |
| 101 | InitFromArgv(); |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 102 | } |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 103 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 104 | void CommandLine::InitFromArgv() { |
| 105 | bool parse_switches = true; |
| 106 | for (size_t i = 1; i < argv_.size(); ++i) { |
| 107 | const std::string& arg = argv_[i]; |
| 108 | |
| 109 | if (!parse_switches) { |
| 110 | loose_values_.push_back(arg); |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | if (arg == kSwitchTerminator) { |
| 115 | parse_switches = false; |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | std::string switch_string; |
| 120 | std::string switch_value; |
| 121 | if (IsSwitch(arg, &switch_string, &switch_value)) { |
| 122 | switches_[switch_string] = switch_value; |
| 123 | } else { |
| 124 | loose_values_.push_back(arg); |
| 125 | } |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 126 | } |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | CommandLine::CommandLine(const std::wstring& program) { |
| 130 | argv_.push_back(WideToASCII(program)); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 131 | } |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 132 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 133 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 134 | // static |
| 135 | bool CommandLine::IsSwitch(const StringType& parameter_string, |
| 136 | std::string* switch_string, |
| 137 | StringType* switch_value) { |
| 138 | switch_string->clear(); |
| 139 | switch_value->clear(); |
| 140 | |
| 141 | for (size_t i = 0; i < arraysize(kSwitchPrefixes); ++i) { |
| 142 | StringType prefix(kSwitchPrefixes[i]); |
| 143 | if (parameter_string.find(prefix) != 0) |
| 144 | continue; |
| 145 | |
| 146 | const size_t switch_start = prefix.length(); |
| 147 | const size_t equals_position = parameter_string.find( |
| 148 | kSwitchValueSeparator, switch_start); |
| 149 | StringType switch_native; |
| 150 | if (equals_position == StringType::npos) { |
| 151 | switch_native = parameter_string.substr(switch_start); |
| 152 | } else { |
| 153 | switch_native = parameter_string.substr( |
| 154 | switch_start, equals_position - switch_start); |
| 155 | *switch_value = parameter_string.substr(equals_position + 1); |
| 156 | } |
| 157 | #if defined(OS_WIN) |
| 158 | Lowercase(&switch_native); |
| 159 | *switch_string = WideToASCII(switch_native); |
| 160 | #else |
| 161 | *switch_string = switch_native; |
| 162 | #endif |
| 163 | |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | return false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 168 | } |
| 169 | |
[email protected] | 1a48f31 | 2008-08-12 01:14:37 | [diff] [blame] | 170 | // static |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 171 | void CommandLine::Init(int argc, const char* const* argv) { |
| 172 | DCHECK(current_process_commandline_ == NULL); |
| 173 | #if defined(OS_WIN) |
| 174 | current_process_commandline_ = new CommandLine; |
| 175 | current_process_commandline_->ParseFromString(::GetCommandLineW()); |
| 176 | #elif defined(OS_POSIX) |
| 177 | current_process_commandline_ = new CommandLine(argc, argv); |
[email protected] | 54d5b04 | 2008-08-12 01:27:35 | [diff] [blame] | 178 | #endif |
[email protected] | 1a48f31 | 2008-08-12 01:14:37 | [diff] [blame] | 179 | } |
| 180 | |
[email protected] | a2318cda | 2009-02-25 21:13:53 | [diff] [blame^] | 181 | void CommandLine::Terminate() { |
| 182 | DCHECK(current_process_commandline_ != NULL); |
| 183 | delete current_process_commandline_; |
| 184 | current_process_commandline_ = NULL; |
| 185 | } |
| 186 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 187 | bool CommandLine::HasSwitch(const std::wstring& switch_string) const { |
| 188 | std::wstring lowercased_switch(switch_string); |
| 189 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 190 | Lowercase(&lowercased_switch); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 191 | #endif |
| 192 | return switches_.find(WideToASCII(lowercased_switch)) != switches_.end(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 195 | std::wstring CommandLine::GetSwitchValue( |
| 196 | const std::wstring& switch_string) const { |
| 197 | std::wstring lowercased_switch(switch_string); |
| 198 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 199 | Lowercase(&lowercased_switch); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 200 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 201 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 202 | std::map<std::string, StringType>::const_iterator result = |
| 203 | switches_.find(WideToASCII(lowercased_switch)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 204 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 205 | if (result == switches_.end()) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 206 | return L""; |
| 207 | } else { |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 208 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 209 | return result->second; |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 210 | #else |
| 211 | return ASCIIToWide(result->second); |
| 212 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 216 | #if defined(OS_WIN) |
| 217 | std::vector<std::wstring> CommandLine::GetLooseValues() const { |
| 218 | return loose_values_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 219 | } |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 220 | std::wstring CommandLine::program() const { |
| 221 | return program_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 222 | } |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 223 | #else |
| 224 | std::vector<std::wstring> CommandLine::GetLooseValues() const { |
| 225 | std::vector<std::wstring> values; |
| 226 | for (size_t i = 0; i < loose_values_.size(); ++i) |
| 227 | values.push_back(ASCIIToWide(loose_values_[i])); |
| 228 | return values; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 229 | } |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 230 | std::wstring CommandLine::program() const { |
| 231 | DCHECK(argv_.size() > 0); |
| 232 | return ASCIIToWide(argv_[0]); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 233 | } |
| 234 | #endif |
| 235 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 236 | |
| 237 | // static |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 238 | std::wstring CommandLine::PrefixedSwitchString( |
| 239 | const std::wstring& switch_string) { |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 240 | return StringPrintf(L"%ls%ls", |
| 241 | kSwitchPrefixes[0], |
| 242 | switch_string.c_str()); |
| 243 | } |
| 244 | |
| 245 | // static |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 246 | std::wstring CommandLine::PrefixedSwitchStringWithValue( |
| 247 | const std::wstring& switch_string, const std::wstring& value_string) { |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 248 | if (value_string.empty()) { |
| 249 | return PrefixedSwitchString(switch_string); |
| 250 | } |
| 251 | |
| 252 | return StringPrintf(L"%ls%ls%ls%ls", |
| 253 | kSwitchPrefixes[0], |
| 254 | switch_string.c_str(), |
| 255 | kSwitchValueSeparator, |
| 256 | value_string.c_str()); |
| 257 | } |
| 258 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 259 | #if defined(OS_WIN) |
| 260 | void CommandLine::AppendSwitch(const std::wstring& switch_string) { |
| 261 | std::wstring prefixed_switch_string = PrefixedSwitchString(switch_string); |
| 262 | command_line_string_.append(L" "); |
| 263 | command_line_string_.append(prefixed_switch_string); |
| 264 | switches_[WideToASCII(switch_string)] = L""; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 267 | void CommandLine::AppendSwitchWithValue(const std::wstring& switch_string, |
| 268 | const std::wstring& value_string) { |
| 269 | std::wstring value_string_edit; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 270 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 271 | // NOTE(jhughes): If the value contains a quotation mark at one |
| 272 | // end but not both, you may get unusable output. |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 273 | if (!value_string.empty() && |
| 274 | (value_string.find(L" ") != std::wstring::npos) && |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 275 | (value_string[0] != L'"') && |
| 276 | (value_string[value_string.length() - 1] != L'"')) { |
| 277 | // need to provide quotes |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 278 | value_string_edit = StringPrintf(L"\"%ls\"", value_string.c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 279 | } else { |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 280 | value_string_edit = value_string; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 281 | } |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 282 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 283 | std::wstring combined_switch_string = |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 284 | PrefixedSwitchStringWithValue(switch_string, value_string_edit); |
| 285 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 286 | command_line_string_.append(L" "); |
| 287 | command_line_string_.append(combined_switch_string); |
| 288 | |
| 289 | switches_[WideToASCII(switch_string)] = value_string; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 290 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 291 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 292 | void CommandLine::AppendLooseValue(const std::wstring& value) { |
| 293 | // TODO(evan): quoting? |
| 294 | command_line_string_.append(L" "); |
| 295 | command_line_string_.append(value); |
| 296 | } |
| 297 | |
| 298 | void CommandLine::AppendArguments(const CommandLine& other, |
| 299 | bool include_program) { |
| 300 | // Verify include_program is used correctly. |
| 301 | // Logic could be shorter but this is clearer. |
| 302 | DCHECK(include_program ? !other.program().empty() : other.program().empty()); |
| 303 | command_line_string_ += L" " + other.command_line_string_; |
| 304 | std::map<std::string, StringType>::const_iterator i; |
| 305 | for (i = other.switches_.begin(); i != other.switches_.end(); ++i) |
| 306 | switches_[i->first] = i->second; |
| 307 | } |
| 308 | |
| 309 | #elif defined(OS_POSIX) |
| 310 | void CommandLine::AppendSwitch(const std::wstring& switch_string) { |
| 311 | std::string ascii_switch = WideToASCII(switch_string); |
| 312 | argv_.push_back(kSwitchPrefixes[0] + ascii_switch); |
| 313 | switches_[ascii_switch] = ""; |
| 314 | } |
| 315 | |
| 316 | void CommandLine::AppendSwitchWithValue(const std::wstring& switch_string, |
| 317 | const std::wstring& value_string) { |
| 318 | std::string ascii_switch = WideToASCII(switch_string); |
| 319 | std::string ascii_value = WideToASCII(value_string); |
| 320 | |
| 321 | argv_.push_back(kSwitchPrefixes[0] + ascii_switch + |
| 322 | kSwitchValueSeparator + ascii_value); |
| 323 | switches_[ascii_switch] = ascii_value; |
| 324 | } |
| 325 | |
| 326 | void CommandLine::AppendLooseValue(const std::wstring& value) { |
| 327 | argv_.push_back(WideToASCII(value)); |
| 328 | } |
| 329 | |
| 330 | void CommandLine::AppendArguments(const CommandLine& other, |
| 331 | bool include_program) { |
| 332 | // Verify include_program is used correctly. |
| 333 | // Logic could be shorter but this is clearer. |
| 334 | DCHECK(include_program ? !other.program().empty() : other.program().empty()); |
| 335 | |
| 336 | size_t first_arg = include_program ? 0 : 1; |
| 337 | for (size_t i = first_arg; i < other.argv_.size(); ++i) |
| 338 | argv_.push_back(other.argv_[i]); |
| 339 | std::map<std::string, StringType>::const_iterator i; |
| 340 | for (i = other.switches_.begin(); i != other.switches_.end(); ++i) |
| 341 | switches_[i->first] = i->second; |
| 342 | } |
[email protected] | 052f1d48 | 2009-02-10 00:52:14 | [diff] [blame] | 343 | |
| 344 | void CommandLine::PrependWrapper(const std::wstring& wrapper_wide) { |
| 345 | // The wrapper may have embedded arguments (like "gdb --args"). In this case, |
| 346 | // we don't pretend to do anything fancy, we just split on spaces. |
| 347 | const std::string wrapper = WideToASCII(wrapper_wide); |
| 348 | std::vector<std::string> wrapper_and_args; |
| 349 | SplitString(wrapper, ' ', &wrapper_and_args); |
| 350 | argv_.insert(argv_.begin(), wrapper_and_args.begin(), wrapper_and_args.end()); |
| 351 | } |
| 352 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 353 | #endif |