Files
Breeze/source/BreezeCore/include/breeze/Process.hpp
T
2026-10-24 09:53:48 -03:00

57 lines
1.9 KiB
C++

#pragma once
#include <filesystem>
#include <optional>
#include <string>
#include <utility>
#include <vector>
/// 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<std::pair<std::string, std::string>> 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<std::string>& 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<std::string>& 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<std::string>& argv);
/// The first match for `program` on PATH, if any.
std::optional<std::filesystem::path> 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();
}