#pragma once #include #include #include #include #include /// Running other programs. Arguments are passed as a vector, never through a shell, so nothing /// needs quoting by the caller. namespace breeze::proc { struct Options { /// Working directory; empty keeps the current one. std::filesystem::path cwd; /// Variables set (or replaced) for the child only. std::vector> env; /// Collect the child's standard output into Result::output instead of showing it. bool capture = false; /// With capture, standard error is collected too (merged into the same string). bool mergeStderr = false; /// Throw the child's output away. bool silent = false; }; struct Result { int exitCode = 0; std::string output; }; /// Runs argv[0] with the rest as arguments (looked up on PATH when it has no directory part) /// and waits. A program that can't be started is exit code 127. Throws Error only for failures /// of this function itself (nothing to run, the system refusing to create a process). Result run(const std::vector& argv, const Options& options = {}); /// Runs a .bat/.cmd script on Windows through cmd.exe, or the script itself elsewhere. Result runScript(const std::filesystem::path& script, const std::vector& args, const Options& options = {}); /// Replaces this process with argv (POSIX), or runs it and exits with its status (Windows). /// Does not return. Throws Error when it can't be started. [[noreturn]] void execReplace(const std::vector& argv); /// The first match for `program` on PATH, if any. std::optional findInPath(const std::string& program); /// Sets an environment variable for this process and the programs it starts from now on. void setEnvironment(const std::string& name, const std::string& value); bool stdinIsTerminal(); bool stdoutIsTerminal(); }