| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #include <shellapi.h> | 9 #include <shellapi.h> |
| 10 #elif defined(OS_FREEBSD) |
| 11 #include <stdlib.h> |
| 12 #include <unistd.h> |
| 10 #endif | 13 #endif |
| 11 | 14 |
| 12 #include <algorithm> | 15 #include <algorithm> |
| 13 | 16 |
| 14 #include "base/logging.h" | 17 #include "base/logging.h" |
| 15 #include "base/singleton.h" | 18 #include "base/singleton.h" |
| 16 #include "base/string_piece.h" | 19 #include "base/string_piece.h" |
| 17 #include "base/string_util.h" | 20 #include "base/string_util.h" |
| 18 #include "base/sys_string_conversions.h" | 21 #include "base/sys_string_conversions.h" |
| 19 | 22 |
| 23 #if defined(OS_LINUX) |
| 24 // Linux/glibc doesn't natively have setproctitle(). |
| 25 #include "base/setproctitle_linux.h" |
| 26 #endif |
| 27 |
| 20 CommandLine* CommandLine::current_process_commandline_ = NULL; | 28 CommandLine* CommandLine::current_process_commandline_ = NULL; |
| 21 | 29 |
| 22 // Since we use a lazy match, make sure that longer versions (like L"--") | 30 // Since we use a lazy match, make sure that longer versions (like L"--") |
| 23 // are listed before shorter versions (like L"-") of similar prefixes. | 31 // are listed before shorter versions (like L"-") of similar prefixes. |
| 24 #if defined(OS_WIN) | 32 #if defined(OS_WIN) |
| 25 const wchar_t* const kSwitchPrefixes[] = {L"--", L"-", L"/"}; | 33 const wchar_t* const kSwitchPrefixes[] = {L"--", L"-", L"/"}; |
| 26 const wchar_t kSwitchTerminator[] = L"--"; | 34 const wchar_t kSwitchTerminator[] = L"--"; |
| 27 const wchar_t kSwitchValueSeparator[] = L"="; | 35 const wchar_t kSwitchValueSeparator[] = L"="; |
| 28 #elif defined(OS_POSIX) | 36 #elif defined(OS_POSIX) |
| 29 // Unixes don't use slash as a switch. | 37 // Unixes don't use slash as a switch. |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 void CommandLine::Init(const std::vector<std::string>& argv) { | 195 void CommandLine::Init(const std::vector<std::string>& argv) { |
| 188 DCHECK(current_process_commandline_ == NULL); | 196 DCHECK(current_process_commandline_ == NULL); |
| 189 #if defined(OS_WIN) | 197 #if defined(OS_WIN) |
| 190 current_process_commandline_ = new CommandLine; | 198 current_process_commandline_ = new CommandLine; |
| 191 current_process_commandline_->ParseFromString(::GetCommandLineW()); | 199 current_process_commandline_->ParseFromString(::GetCommandLineW()); |
| 192 #elif defined(OS_POSIX) | 200 #elif defined(OS_POSIX) |
| 193 current_process_commandline_ = new CommandLine(argv); | 201 current_process_commandline_ = new CommandLine(argv); |
| 194 #endif | 202 #endif |
| 195 } | 203 } |
| 196 | 204 |
| 205 #if defined(OS_LINUX) || defined(OS_FREEBSD) |
| 206 // static |
| 207 void CommandLine::SetProcTitle() { |
| 208 // Build a single string which consists of all the arguments separated |
| 209 // by spaces. We can't actually keep them separate due to the way the |
| 210 // setproctitle() function works. |
| 211 std::string title; |
| 212 for (size_t i = 1; i < current_process_commandline_->argv_.size(); ++i) { |
| 213 if (!title.empty()) |
| 214 title += " "; |
| 215 title += current_process_commandline_->argv_[i]; |
| 216 } |
| 217 setproctitle("%s", title.c_str()); |
| 218 } |
| 219 |
| 220 // static |
| 221 void CommandLine::SetTrueArgv(char** argv) { |
| 222 #if defined(OS_LINUX) |
| 223 setproctitle_init(argv); |
| 224 #endif |
| 225 } |
| 226 #endif |
| 227 |
| 197 void CommandLine::Terminate() { | 228 void CommandLine::Terminate() { |
| 198 DCHECK(current_process_commandline_ != NULL); | 229 DCHECK(current_process_commandline_ != NULL); |
| 199 delete current_process_commandline_; | 230 delete current_process_commandline_; |
| 200 current_process_commandline_ = NULL; | 231 current_process_commandline_ = NULL; |
| 201 } | 232 } |
| 202 | 233 |
| 203 bool CommandLine::HasSwitch(const std::wstring& switch_string) const { | 234 bool CommandLine::HasSwitch(const std::wstring& switch_string) const { |
| 204 std::wstring lowercased_switch(switch_string); | 235 std::wstring lowercased_switch(switch_string); |
| 205 #if defined(OS_WIN) | 236 #if defined(OS_WIN) |
| 206 Lowercase(&lowercased_switch); | 237 Lowercase(&lowercased_switch); |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 void CommandLine::PrependWrapper(const std::wstring& wrapper_wide) { | 400 void CommandLine::PrependWrapper(const std::wstring& wrapper_wide) { |
| 370 // The wrapper may have embedded arguments (like "gdb --args"). In this case, | 401 // The wrapper may have embedded arguments (like "gdb --args"). In this case, |
| 371 // we don't pretend to do anything fancy, we just split on spaces. | 402 // we don't pretend to do anything fancy, we just split on spaces. |
| 372 const std::string wrapper = base::SysWideToNativeMB(wrapper_wide); | 403 const std::string wrapper = base::SysWideToNativeMB(wrapper_wide); |
| 373 std::vector<std::string> wrapper_and_args; | 404 std::vector<std::string> wrapper_and_args; |
| 374 SplitString(wrapper, ' ', &wrapper_and_args); | 405 SplitString(wrapper, ' ', &wrapper_and_args); |
| 375 argv_.insert(argv_.begin(), wrapper_and_args.begin(), wrapper_and_args.end()); | 406 argv_.insert(argv_.begin(), wrapper_and_args.begin(), wrapper_and_args.end()); |
| 376 } | 407 } |
| 377 | 408 |
| 378 #endif | 409 #endif |
| OLD | NEW |