Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(199)

Side by Side Diff: base/command_line.cc

Issue 2778173003: base: Support command line wrappers with quoted arguments (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/command_line_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include <algorithm> 7 #include <algorithm>
8 #include <ostream> 8 #include <ostream>
9 9
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/strings/string_split.h" 13 #include "base/strings/string_split.h"
14 #include "base/strings/string_tokenizer.h"
14 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
16 #include "build/build_config.h" 17 #include "build/build_config.h"
17 18
18 #if defined(OS_WIN) 19 #if defined(OS_WIN)
19 #include <windows.h> 20 #include <windows.h>
20 #include <shellapi.h> 21 #include <shellapi.h>
21 #endif 22 #endif
22 23
23 namespace base { 24 namespace base {
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 void CommandLine::AppendArguments(const CommandLine& other, 405 void CommandLine::AppendArguments(const CommandLine& other,
405 bool include_program) { 406 bool include_program) {
406 if (include_program) 407 if (include_program)
407 SetProgram(other.GetProgram()); 408 SetProgram(other.GetProgram());
408 AppendSwitchesAndArguments(this, other.argv()); 409 AppendSwitchesAndArguments(this, other.argv());
409 } 410 }
410 411
411 void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) { 412 void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) {
412 if (wrapper.empty()) 413 if (wrapper.empty())
413 return; 414 return;
414 // The wrapper may have embedded arguments (like "gdb --args"). In this case, 415 // Split the wrapper command based on whitespace (with quoting).
415 // we don't pretend to do anything fancy, we just split on spaces. 416 StringTokenizer tokenizer(wrapper, " ");
gab 2017/03/28 17:03:59 This won't compile on Windows I think (CommandLine
Sami 2017/03/28 17:54:09 Ah, thanks for the tip! Done.
416 StringVector wrapper_argv = SplitString( 417 tokenizer.set_quote_chars("'\"");
417 wrapper, FilePath::StringType(1, ' '), base::TRIM_WHITESPACE, 418 // Prepend the wrapper and update the switches/arguments |begin_args_|
418 base::SPLIT_WANT_ALL); 419 auto it = argv_.begin();
419 // Prepend the wrapper and update the switches/arguments |begin_args_|. 420 while (tokenizer.GetNext()) {
420 argv_.insert(argv_.begin(), wrapper_argv.begin(), wrapper_argv.end()); 421 it = argv_.emplace(it, tokenizer.token()) + 1;
gab 2017/03/28 17:03:59 Pushing in front of vector repeatedly is inefficie
Sami 2017/03/28 17:54:09 Right you are -- done.
421 begin_args_ += wrapper_argv.size(); 422 begin_args_++;
gab 2017/03/28 17:04:00 Irrelevant after above comment but FWIW: prefer pr
Sami 2017/03/28 17:54:09 Acknowledged.
423 }
422 } 424 }
423 425
424 #if defined(OS_WIN) 426 #if defined(OS_WIN)
425 void CommandLine::ParseFromString(const string16& command_line) { 427 void CommandLine::ParseFromString(const string16& command_line) {
426 string16 command_line_string; 428 string16 command_line_string;
427 TrimWhitespace(command_line, TRIM_ALL, &command_line_string); 429 TrimWhitespace(command_line, TRIM_ALL, &command_line_string);
428 if (command_line_string.empty()) 430 if (command_line_string.empty())
429 return; 431 return;
430 432
431 int num_args = 0; 433 int num_args = 0;
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 return params; 486 return params;
485 } 487 }
486 488
487 void CommandLine::ResetStringPieces() { 489 void CommandLine::ResetStringPieces() {
488 switches_by_stringpiece_.clear(); 490 switches_by_stringpiece_.clear();
489 for (const auto& entry : switches_) 491 for (const auto& entry : switches_)
490 switches_by_stringpiece_[entry.first] = &(entry.second); 492 switches_by_stringpiece_[entry.first] = &(entry.second);
491 } 493 }
492 494
493 } // namespace base 495 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/command_line_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698