diff --git a/doc/21-library-reference.md b/doc/21-library-reference.md index d8caed59b..92cdbde8e 100644 --- a/doc/21-library-reference.md +++ b/doc/21-library-reference.md @@ -23,6 +23,7 @@ dirname(path) | Returns the directory portion of the specified basename(path) | Returns the filename portion of the specified path. escape\_shell\_arg(text) | Escapes a string for use as a single shell argument. escape\_shell\_cmd(text) | Escapes shell meta characters in a string. +escape\_create\_process\_arg(text)| (Windows only) Escapes a string for use as an argument for CreateProcess(). exit(integer) | Terminates the application. ## Object Accessor Functions diff --git a/lib/base/process.cpp b/lib/base/process.cpp index d192044a8..4f34f9359 100644 --- a/lib/base/process.cpp +++ b/lib/base/process.cpp @@ -133,7 +133,7 @@ Process::Arguments Process::PrepareCommand(const Value& command) if (args != "") args += " "; - args += Utility::EscapeShellArg(argument); + args += Utility::EscapeCreateProcessArg(argument); #else /* _WIN32 */ args.push_back(argument); #endif /* _WIN32 */ diff --git a/lib/base/scriptutils.cpp b/lib/base/scriptutils.cpp index 5651b29d1..a916e5a0b 100644 --- a/lib/base/scriptutils.cpp +++ b/lib/base/scriptutils.cpp @@ -61,6 +61,7 @@ REGISTER_SAFE_SCRIPTFUNCTION(msi_get_component_path, &ScriptUtils::MsiGetCompone REGISTER_SAFE_SCRIPTFUNCTION(track_parents, &ScriptUtils::TrackParents); REGISTER_SAFE_SCRIPTFUNCTION(escape_shell_cmd, &Utility::EscapeShellCmd); REGISTER_SAFE_SCRIPTFUNCTION(escape_shell_arg, &Utility::EscapeShellArg); +REGISTER_SAFE_SCRIPTFUNCTION(escape_create_process_arg, &Utility::EscapeCreateProcessArg); String ScriptUtils::CastString(const Value& value) { diff --git a/lib/base/string.hpp b/lib/base/string.hpp index 2de4b5a44..82c9f1218 100644 --- a/lib/base/string.hpp +++ b/lib/base/string.hpp @@ -241,6 +241,11 @@ public: return t; } + inline void Append(int count, char ch) + { + m_Data.append(count, ch); + } + inline bool Contains(const String& str) const { return (m_Data.find(str) != std::string::npos); diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index 3bf58bf33..c63998d51 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -978,6 +978,40 @@ String Utility::EscapeShellArg(const String& s) return result; } +#ifdef _WIN32 +String Utility::EscapeCreateProcessArg(const String& arg) +{ + if (arg.FindFirstOf(" \t\n\v\"") == String::NPos) + return arg; + + String result = "\""; + + for (String::ConstIterator it = arg.Begin(); ; it++) { + int numBackslashes = 0; + + while (it != arg.End() && *it == '\\') { + it++; + numBackslashes++; + } + + if (it == arg.End()) { + result.Append(numBackslashes * 2, '\\'); + break; + } else if (*it == '"') { + result.Append(numBackslashes * 2, '\\'); + result.Append(1, *it); + } else { + result.Append(numBackslashes, '\\'); + result.Append(1, *it); + } + } + + result += "\""; + + return result; +} +#endif /* _WIN32 */ + #ifdef _WIN32 static void WindowsSetThreadName(const char *name) { diff --git a/lib/base/utility.hpp b/lib/base/utility.hpp index 3ca6a4ad5..839a5aca8 100644 --- a/lib/base/utility.hpp +++ b/lib/base/utility.hpp @@ -103,6 +103,7 @@ public: static String EscapeShellCmd(const String& s); static String EscapeShellArg(const String& s); + static String EscapeCreateProcessArg(const String& arg); static String EscapeString(const String& s, const String& chars, const bool illegal); static String UnescapeString(const String& s);