26 enum class RedirectMode {
37 enum class StreamIndex {
47 static constexpr size_t Index(StreamIndex stream) {
48 return static_cast<size_t>(stream);
54 static constexpr size_t kStreamCount =
55 static_cast<size_t>(StreamIndex::count);
58 typedef std::function<void(
void *buf,
size_t size)> RedirectDelegate;
72 std::string _executablePath;
73 StringCollection _arguments;
80 std::string _rawTrailingArgument;
81 EnvironmentBlock _environment;
91 std::string _workingDirectory;
92 std::array<RedirectDescriptor, kStreamCount> _descriptors;
97 std::array<std::thread, kStreamCount> _redirectThreads;
98 std::mutex _outputBufferMutex;
99 std::string _outputBuffer;
105 HANDLE _threadHandle;
116 bool setExecutable(
const std::string &path);
117 bool setShellCommand(
const std::string &command);
118 bool setWorkingDirectory(
const std::string &path);
121 bool setArguments(
const StringCollection &args);
123 template <
typename... Args>
inline bool setArguments(
const Args &...args) {
124 std::string args_[] = {args...};
125 return setArguments(StringCollection(&args_[0], &args_[
sizeof...(Args)]));
129 bool setEnvironment(
const EnvironmentBlock &args);
130 bool addEnvironment(
const std::string &key,
const std::string &val);
141 void setDebugOnCreate(
bool debug) { _debugOnCreate = debug; }
144 bool redirectInputToConsole();
145 bool redirectOutputToConsole();
146 bool redirectErrorToConsole();
149 bool redirectInputToNull();
150 bool redirectOutputToNull();
151 bool redirectErrorToNull();
154 bool redirectInputToFile(
const std::string &path);
155 bool redirectOutputToFile(
const std::string &path);
156 bool redirectErrorToFile(
const std::string &path);
159 bool redirectOutputToBuffer();
160 bool redirectErrorToBuffer();
163 bool redirectInputToTerminal() {
return false; }
166 bool redirectOutputToDelegate(RedirectDelegate delegate) {
return false; }
167 bool redirectErrorToDelegate(RedirectDelegate delegate) {
return false; }
170 ErrorCode run(std::function<
bool()> preExecAction = []() {
return true; });
172 bool isRunning()
const;
176 inline ProcessId pid()
const {
return _pid; }
177 inline HANDLE handle()
const {
return _handle; }
178 inline ThreadId tid()
const {
return _tid; }
179 inline HANDLE threadHandle()
const {
return _threadHandle; }
180 inline int exitStatus()
const {
return _exitStatus; }
181 inline int signalCode()
const {
return 0; }
182 inline const std::string &executable()
const {
return _executablePath; }
183 inline const StringCollection &arguments()
const {
return _arguments; }
184 inline const EnvironmentBlock &environment()
const {
return _environment; }
187 ErrorCode input(
const ByteVector &buf) {
return kErrorUnsupported; }
189 inline const std::string &output()
const {
return _outputBuffer; }
192 void redirectionThread(
size_t index);