diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-06-12 16:18:07 -0400 |
|---|---|---|
| committer | Tim Foley <tfoleyNV@users.noreply.github.com> | 2019-06-12 13:18:07 -0700 |
| commit | 503721047731e8f6566bc51d6eadc7d24161c129 (patch) | |
| tree | fcb017be8e6fa8312721b09b39465959c3d14709 /source/core/slang-process-util.h | |
| parent | 7931adac99a78fd5488f665578fba858b34bc8a6 (diff) | |
CommandLine arg escaping (#980)
* Work in progress to be able to invoke VS from within code.
* First pass at windows version of refactor of OSProcessSpawner
* Closer to getting VS path lookup working.
* Make OSString assignable/ctor able
* Work out program files directory directly, so don't have to expand %%.
* WIP: Improve handling of process spawning.
* Add support for splitting input by line.
* * Correctly locates visual studio install
* Added functionality to invoke vs via cmd
* Add option to execute the command line.
* Handle in ProcessUtil for windows -> WinHandle.
* Rename files slang-win-visual-studio-util.cpp/.h and slang-process-util.h
* First pass at unix/linux version of ProcessUtil.
* Fix reading Visual Studio path from the registry.
* Get compiling on linux with.
* Fix vcvarsall.bat name
* Use ProcessUtil to execute external code.
* Remove OSProcessSpawner.
* Remove includes for "os.h" where no longer needed.
* Fix tabbing issue in premake5.lua
Remove test code from slang-test-main.cpp
* Fix premake4.lua tabbing issue.
* Small fixes to slang-process-util.h
Init ExecuteResult on Win execute.
* Improve comments.
* Fix bug in StringUtil::calcLines - with oddly terminated source input being able to read past end.
Make slang-generate use StringUtil over it's own impl.
* Fix off by one bug in working out Visual Studio version.
* Fix bug in calculating Visual Studio Version
* Fix compilation on linux with string parameter being passed to messageFormat.
* Remove erroneous use of kOSError codes - use Result.
* First effort to generate standard compiler options.
* Initial efforts in compiling source code in test framework for VisualStudio.
* Testing compiling c code on VisualStudio on Windows.
* Fix warning on linux.
* Fix clang on linux warning (and therefore failing) returning a StringBuilder as String.
* Disable return-std-move on clang.
* CommandLine arguments are now tagged if they are escaped or not. That it is the clients responsibility to escape command lines that cannot be automatically escaped.
* Add checks on unix/linux that command line args are all unescaped.
Diffstat (limited to 'source/core/slang-process-util.h')
| -rw-r--r-- | source/core/slang-process-util.h | 76 |
1 files changed, 72 insertions, 4 deletions
diff --git a/source/core/slang-process-util.h b/source/core/slang-process-util.h index 75ea945fe..03265c1cb 100644 --- a/source/core/slang-process-util.h +++ b/source/core/slang-process-util.h @@ -16,17 +16,48 @@ struct CommandLine Filename, ///< The executable is set as a filename }; - void addArg(const String& in) { m_args.add(in); } - void addArgs(const String* args, Int argsCount) { m_args.addRange(args, argsCount); } + enum class ArgType + { + Escaped, + Unescaped, + }; + + struct Arg + { + ArgType type; ///< How to interpret the argument value + String value; ///< The argument value + }; + + /// Add args - assumed unescaped + void addArg(const String& in) { m_args.add(Arg{ArgType::Unescaped, in}); } + void addArgs(const String* args, Int argsCount) { for (Int i = 0; i < argsCount; ++i) addArg(args[i]); } + + /// Add args - all assumed unescaped + void addArgs(const Arg* args, Int argCount) { m_args.addRange(args, argCount); } + + /// Add an escaped arg + void addEscapedArg(const String& in) { m_args.add(Arg{ArgType::Escaped, in}); } + void addEscapedArgs(const String* args, Int argsCount) { for (Int i = 0; i < argsCount; ++i) addEscapedArg(args[i]); } + + /// Find the index of an arg which is exact match for slice + SLANG_INLINE Index findArgIndex(const UnownedStringSlice& slice) const; + + /// Set the executable path void setExecutablePath(const String& path) { m_executableType = ExecutableType::Path; m_executable = path; } void setExecutableFilename(const String& filename) { m_executableType = ExecutableType::Filename; m_executable = filename; } + /// For handling args where the switch is placed directly in front of the path + SLANG_INLINE void addPrefixPathArg(const char* prefix, const String& path, const char* pathPostfix = nullptr); + + /// Get the total number of args + SLANG_FORCE_INLINE Index getArgCount() const { return m_args.getCount(); } + /// Ctor CommandLine():m_executableType(ExecutableType::Unknown) {} ExecutableType m_executableType; ///< How the executable is specified - String m_executable; ///< Executable to run - List<String> m_args; ///< The parameters to pass + String m_executable; ///< Executable to run. Note that the executable is never escaped. + List<Arg> m_args; ///< The arguments }; struct ExecuteResult @@ -59,6 +90,43 @@ struct ProcessUtil static void appendCommandLineEscaped(const UnownedStringSlice& slice, StringBuilder& out); }; +// ----------------------------------------------------------------------- +SLANG_INLINE Index CommandLine::findArgIndex(const UnownedStringSlice& slice) const +{ + const Index count = m_args.getCount(); + + for (Index i = 0; i < count; ++i) + { + const auto& arg = m_args[i]; + if (arg.value == slice) + { + return i; + } + } + return -1; +} + +// ----------------------------------------------------------------------- +SLANG_INLINE void CommandLine::addPrefixPathArg(const char* prefix, const String& path, const char* pathPostfix) +{ + StringBuilder builder; + builder << prefix; + if (pathPostfix) + { + // Work out the path with the postfix + StringBuilder fullPath; + fullPath << path << pathPostfix; + ProcessUtil::appendCommandLineEscaped(fullPath.getUnownedSlice(), builder); + } + else + { + ProcessUtil::appendCommandLineEscaped(path.getUnownedSlice(), builder); + } + + // This arg doesn't need subsequent escaping + addEscapedArg(builder); +} + } #endif // SLANG_PROCESS_UTIL_H |
