4#ifndef _Stroika_Foundation_Execution_ProcessRunner_h_
5#define _Stroika_Foundation_Execution_ProcessRunner_h_ 1
7#include "Stroika/Foundation/StroikaPreComp.h"
14#include "Stroika/Foundation/Common/Common.h"
15#include "Stroika/Foundation/Containers/Mapping.h"
16#include "Stroika/Foundation/Containers/Sequence.h"
18#include "Stroika/Foundation/Execution/CommandLine.h"
19#include "Stroika/Foundation/Execution/Signals.h"
25#include "ProgressMonitor.h"
95 using Characters::String;
96 using Containers::Mapping;
97 using Containers::Sequence;
150#if qStroika_Foundation_Common_Platform_Windows
151 CommandLine::WrapInShell::eWindowsCMD
153 CommandLine::WrapInShell::eBash
166 optional<filesystem::path> fWorkingDirectory;
190 optional<bool> fDetached;
192#if qStroika_Foundation_Common_Platform_POSIX
198 optional<mode_t> fChildUMask{027};
201#if qStroika_Foundation_Common_Platform_Windows
206 bool fCreateNoWindow : 1 {
true};
225#if qCompilerAndStdLib_DefaultMemberInitializerNeededEnclosingForDefaultFunArg_Buggy
235 ProcessRunner (
const String& commandLine,
const Options& o = {});
242#if qStroika_Foundation_Common_Platform_POSIX
243 using ExitStatusType = uint8_t;
244#elif qStroika_Foundation_Common_Platform_Windows
245 using ExitStatusType = DWORD;
247 using ExitStatusType = int;
256 nonvirtual CommandLine GetCommandLine ()
const;
257 nonvirtual
void SetCommandLine (
const CommandLine& args);
262 nonvirtual Options GetOptions ()
const;
263 nonvirtual
void SetOptions (
const Options& o);
272 optional<ExitStatusType> fExitStatus;
273 optional<SignalID> fTerminatedByUncaughtSignalNumber;
275 void ThrowIfFailed ();
350 nonvirtual tuple<Characters::String, Characters::String>
Run (
const Characters::String& cmdStdInValue =
""sv,
355 class BackgroundProcess;
382 nonvirtual function<void ()> CreateRunnable_ (
Synchronized<optional<ProcessResultType>>* processResult,
386 optional<filesystem::path> fExecutable_;
395 [[deprecated (
"Since Stroika v3.0d12 - pass stdin/stdout/stderr to ProcessRunner Run() method (if needed)")]]
ProcessRunner (
398 [[deprecated (
"Since Stroika v3.0d12 - pass stdin/stdout/stderr to ProcessRunner Run() method (if needed)")]]
ProcessRunner (
401 [[deprecated (
"Since Stroika v3.0d12 - pass stdin/stdout/stderr to ProcessRunner Run() method (if needed)")]]
ProcessRunner (
407 this->fStdOut_ = out;
408 this->fStdErr_ = error;
411 [[deprecated (
"Since Stroika v3.0d12 - use other overloads for ProcessRunner")]]
ProcessRunner (
417 this->fStdOut_ = out;
418 this->fStdErr_ = error;
421 [[deprecated (
"Since Stroika v3.0d12 - pass in/out/error streams(can be nullptr) to Run method instead of CTOR")]]
void
422 Run (optional<ProcessResultType>* processResult, ProgressMonitor::Updater progress =
nullptr,
Time::DurationSeconds timeout = Time::kInfinity);
424 [[deprecated (
"Since Stroika v3.0d12 pass in/out/error(can be nullptr) in RunInbackground() method")]] BackgroundProcess
430 [[deprecated (
"Since Stroika v3.0d12 - use GetOptions().fWorkingDirectory")]] optional<filesystem::path> GetWorkingDirectory ()
const;
431 [[deprecated (
"Since Stroika v3.0d12 - use SetOptions({.fWorkingDirectory})")]]
void SetWorkingDirectory (
const optional<filesystem::path>& d);
442 [[deprecated (
"Since Stroika v3.0d12 - pass in/out/error streams(can be nullptr) to Run method instead of CTOR")]]
void
451 [[deprecated (
"Since Stroika v3.0d12 - pass in/out/error streams(can be nullptr) to Run method instead of CTOR")]]
void
460 [[deprecated (
"Since Stroika v3.0d12 - pass in/out/error streams(can be nullptr) to Run method instead of CTOR")]]
void
474 Exception (
const String& failureMessage,
const optional<String>& stderrFragment = nullopt,
475 const optional<ExitStatusType>& wExitStatus = nullopt,
const optional<SignalID>& wTermSig = nullopt);
484 const optional<String> fStderrFragment;
487 const optional<ExitStatusType> fExitStatus;
490 const optional<SignalID> fTermSignal;
493 static String mkMsg_ (
const String& errorMessage,
const optional<String>& stderrSubset,
const optional<ExitStatusType>& wExitStatus,
494 const optional<SignalID>& wTermSig);
575 virtual ~Rep_ () =
default;
580 shared_ptr<Rep_> fRep_;
594#include "ProcessRunner.inl"
time_point< RealtimeClock, DurationSeconds > TimePointSeconds
TimePointSeconds is a simpler approach to chrono::time_point, which doesn't require using templates e...
chrono::duration< double > DurationSeconds
chrono::duration<double> - a time span (length of time) measured in seconds, but high precision.
String is like std::u32string, except it is much easier to use, often much more space efficient,...
A generalization of a vector: a container whose elements are keyed by the natural numbers.
NOT a real mutex - just a debugging infrastructure support tool so in debug builds can be assured thr...
nonvirtual void WaitForStarted(Time::DurationSeconds timeout=Time::kInfinity) const
wait until GetChildProcessID () returns a valid answer, or until the process failed to start (in whic...
nonvirtual optional< ProcessResultType > GetProcessResult() const
nonvirtual void PropagateIfException() const
nonvirtual void WaitForDone(Time::DurationSeconds timeout=Time::kInfinity) const
nonvirtual void Join(Time::DurationSeconds timeout=Time::kInfinity) const
Join () does WaitForDone () and throw exception if there was any error (see PropagateIfException).
nonvirtual void JoinUntil(Time::TimePointSeconds timeoutAt) const
WaitForDoneUntil () and throw exception if there was any error (see PropagateIfException).
nonvirtual void Terminate()
optional< pid_t > GetChildProcessID() const
maybe missing if process not yet (or ever successfully) launched. Child process may have already exit...
const String fFailureMessage
Run the given command, and optionally support stdin/stdout/stderr as streams (either sync with Run,...
nonvirtual void Run(const Streams::InputStream::Ptr< byte > &in, const Streams::OutputStream::Ptr< byte > &out=nullptr, const Streams::OutputStream::Ptr< byte > &error=nullptr, ProgressMonitor::Updater progress=nullptr, Time::DurationSeconds timeout=Time::kInfinity)
Run the given external command/process (set by constructor) - with the given arguments,...
Streams::InputStream::Ptr< byte > GetStdIn() const
Streams::OutputStream::Ptr< byte > GetStdOut() const
nonvirtual BackgroundProcess RunInBackground(const Streams::InputStream::Ptr< byte > &in=nullptr, const Streams::OutputStream::Ptr< byte > &out=nullptr, const Streams::OutputStream::Ptr< byte > &error=nullptr, ProgressMonitor::Updater progress=nullptr)
Run the given external command/process (set by constructor) - with the given arguments in the backgro...
ProcessRunner()=delete
Construct ProcessRunner with a CommandLine to run (doesn't actually RUN til you call Run or RunInBack...
Streams::OutputStream::Ptr< byte > GetStdErr() const
Wrap any object with Synchronized<> and it can be used similarly to the base type,...
OutputStream<>::Ptr is Smart pointer to a stream-based sink of data.
Run () options for mapping Strings - what code page converters to use.
optional< Characters::CodeCvt<> > fInputCodeCvt
optional< Characters::CodeCvt<> > fOutputCodeCvt