From 0ebcfc62e435d352b4a2035ac1791b867228194d Mon Sep 17 00:00:00 2001 From: dec05eba <0xdec05eba@gmail.com> Date: Mon, 24 Sep 2018 15:19:56 +0200 Subject: Escape input arg, which usually is a filename --- src/Ninja.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) (limited to 'src/Ninja.cpp') diff --git a/src/Ninja.cpp b/src/Ninja.cpp index 9b9e177..778e701 100644 --- a/src/Ninja.cpp +++ b/src/Ninja.cpp @@ -72,6 +72,32 @@ namespace ninja return true; } + static std::string escape(const std::string &str) + { + std::string result; + result.reserve(str.size()); + for(char c : str) + { + if(c == '"') + result += "\\\""; + else + result += c; + } + return '"' + result + '"'; + } + + static std::string combine_escape(const std::vector &strs) + { + std::string result; + for(const std::string &str : strs) + { + if(!result.empty()) + result += ' '; + result += escape(str); + } + return result; + } + NinjaVariable::NinjaVariable(const std::string &_name) : name(_name) { @@ -93,6 +119,11 @@ namespace ninja { delete rule.second; } + + for(NinjaBuild *build : builds) + { + delete build; + } } NinjaBuild* NinjaRule::build(const std::string &in, const std::string &out, const std::vector &additionalArgs, const std::vector &dependsOnBuilds) @@ -100,6 +131,11 @@ namespace ninja return buildFile->build(this, in, out, additionalArgs, dependsOnBuilds); } + NinjaBuild* NinjaRule::build(const std::vector &in, const std::string &out, const std::vector &additionalArgs, const std::vector &dependsOnBuilds) + { + return buildFile->build(this, in, out, additionalArgs, dependsOnBuilds); + } + void NinjaBuildFile::defineGlobalVariable(const std::string &name, const std::string &value) { if(!isValidName(name)) @@ -127,7 +163,14 @@ namespace ninja NinjaBuild* NinjaBuildFile::build(const NinjaRule *rule, const std::string &in, const std::string &out, const std::vector &additionalArgs, const std::vector &dependsOnBuilds) { - NinjaBuild *build = new NinjaBuild { rule, in, out, additionalArgs, dependsOnBuilds }; + NinjaBuild *build = new NinjaBuild { rule, { in }, escape(out), additionalArgs, dependsOnBuilds }; + builds.push_back(build); + return build; + } + + NinjaBuild* NinjaBuildFile::build(const NinjaRule *rule, const std::vector &in, const std::string &out, const std::vector &additionalArgs, const std::vector &dependsOnBuilds) + { + NinjaBuild *build = new NinjaBuild { rule, in, escape(out), additionalArgs, dependsOnBuilds }; builds.push_back(build); return build; } @@ -157,10 +200,14 @@ namespace ninja result += "\n depfile = "; result += rule.second->depFile; } + if(!rule.second->deps.empty()) + { + result += "\n deps = "; + result += rule.second->deps; + } result += "\n\n"; } - // TODO: enclose all names with quotes to escape whitespace? for(const NinjaBuild *build : builds) { result += "build "; @@ -168,7 +215,7 @@ namespace ninja result += ": "; result += build->rule->name; result += ' '; - result += build->in; + result += combine_escape(build->in); if(!build->dependsOnBuilds.empty()) { -- cgit v1.2.3