aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-06-26 17:30:27 +0200
committerdec05eba <dec05eba@protonmail.com>2021-06-26 17:30:27 +0200
commit691adcce8c2b23af4d0ed855ac7003bda0c8a171 (patch)
treec0020e0d5c6b0bf97d3ff7ff33f09c9cb1663dc0
parentf3f053984698845828f169b1bcc09065bf9fe848 (diff)
Cross platform escape of args
-rw-r--r--src/Ninja.cpp32
1 files changed, 31 insertions, 1 deletions
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<NinjaArg> &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: