From 691adcce8c2b23af4d0ed855ac7003bda0c8a171 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 26 Jun 2021 17:30:27 +0200 Subject: Cross platform escape of args --- src/Ninja.cpp | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/Ninja.cpp b/src/Ninja.cpp index 09df2c1..3b3356b 100644 --- a/src/Ninja.cpp +++ b/src/Ninja.cpp @@ -3,6 +3,36 @@ namespace ninja { +#if defined(_WIN32) || defined(_WIN64) + static std::string escape_arg(const std::string &arg) { + std::string escaped = "\""; + for(char c : arg) { + if(c == '"') { + escaped += "\"\""; + } else { + escaped += c; + } + } + escaped += "\""; + return escaped; + } +#else + static std::string escape_arg(const std::string &arg) { + std::string escaped = "\""; + for(char c : arg) { + if(c == '"') { + escaped += "\\\""; + } else if(c == '\\') { + escaped += "\\\\"; + } else { + escaped += c; + } + } + escaped += "\""; + return escaped; + } +#endif + static std::string joinArgs(const std::vector &args) { std::string result; @@ -19,7 +49,7 @@ namespace ninja { case NinjaArg::Type::VALUE: { - result += '\"' + arg.arg + '\"'; + result += escape_arg(arg.arg); break; } case NinjaArg::Type::VARIABLE: -- cgit v1.2.3