OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/win/command_line.h" |
| 16 |
| 17 namespace crashpad { |
| 18 |
| 19 // Ref: |
| 20 // http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/every
one-quotes-arguments-the-wrong-way.aspx |
| 21 void AppendCommandLineArgument(const std::wstring& argument, |
| 22 std::wstring* command_line) { |
| 23 if (!command_line->empty()) { |
| 24 command_line->push_back(L' '); |
| 25 } |
| 26 |
| 27 // Don’t bother quoting if unnecessary. |
| 28 if (!argument.empty() && |
| 29 argument.find_first_of(L" \t\n\v\"") == std::wstring::npos) { |
| 30 command_line->append(argument); |
| 31 } else { |
| 32 command_line->push_back(L'"'); |
| 33 for (std::wstring::const_iterator i = argument.begin();; ++i) { |
| 34 size_t backslash_count = 0; |
| 35 while (i != argument.end() && *i == L'\\') { |
| 36 ++i; |
| 37 ++backslash_count; |
| 38 } |
| 39 if (i == argument.end()) { |
| 40 // Escape all backslashes, but let the terminating double quotation mark |
| 41 // we add below be interpreted as a metacharacter. |
| 42 command_line->append(backslash_count * 2, L'\\'); |
| 43 break; |
| 44 } else if (*i == L'"') { |
| 45 // Escape all backslashes and the following double quotation mark. |
| 46 command_line->append(backslash_count * 2 + 1, L'\\'); |
| 47 command_line->push_back(*i); |
| 48 } else { |
| 49 // Backslashes aren’t special here. |
| 50 command_line->append(backslash_count, L'\\'); |
| 51 command_line->push_back(*i); |
| 52 } |
| 53 } |
| 54 command_line->push_back(L'"'); |
| 55 } |
| 56 } |
| 57 |
| 58 } // namespace crashpad |
OLD | NEW |