| 33 | |
| 34 | |
| 35 | static void escape_cmdline_argument (std::string& cmdline, const std::string& arg) |
| 36 | { |
| 37 | // For an explanation of Win32's arcane argument quoting rules, see: |
| 38 | // http://msdn.microsoft.com/en-us/library/17w5ykft%28v=vs.85%29.aspx |
| 39 | // http://msdn.microsoft.com/en-us/library/bb776391%28v=vs.85%29.aspx |
| 40 | // http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/everyone-quotes-arguments-the-wrong-way.aspx |
| 41 | // http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx |
| 42 | cmdline.push_back('"'); |
| 43 | |
| 44 | std::string::const_iterator p(arg.begin()); |
| 45 | while (p != arg.end()) { |
| 46 | if (*p == '"') { |
| 47 | cmdline.push_back('\\'); |
| 48 | cmdline.push_back('"'); |
| 49 | ++p; |
| 50 | } else if (*p == '\\') { |
| 51 | unsigned int num_backslashes = 0; |
| 52 | while (p != arg.end() && *p == '\\') { |
| 53 | ++num_backslashes; |
| 54 | ++p; |
| 55 | } |
| 56 | if (p == arg.end() || *p == '"') { |
| 57 | // Backslashes need to be escaped |
| 58 | num_backslashes *= 2; |
| 59 | } |
| 60 | while (num_backslashes--) { |
| 61 | cmdline.push_back('\\'); |
| 62 | } |
| 63 | } else { |
| 64 | cmdline.push_back(*p++); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | cmdline.push_back('"'); |
| 69 | } |
| 70 | |
| 71 | static std::string format_cmdline (const std::vector<std::string>& command) |
| 72 | { |