5#include "../File/File.h"
6#include "../Foundation/Internal/IntrusiveDoubleLinkedList.h"
7#include "../Strings/String.h"
13using ProcessDescriptor = FileDescriptor;
78 StdStream(
String& externalString) { operation = Operation::String;
string = &externalString;}
81 StdStream(
Buffer& externalBuffer) { operation = Operation::Vector; buffer = &externalBuffer; }
87 operation = Operation::FileDescriptor;
88 (void)file.get(fileDescriptor, Result::Error(
"Invalid redirection file descriptor"));
94 operation = Operation::ExternalPipe;
95 pipeDescriptor = &pipe;
119 Operation operation = Operation::Inherit;
127 FileDescriptor::Handle fileDescriptor;
147 using StdStream::StdStream;
152 using StdErr = StdOut;
163 template <
int N>
StdIn(
const char (&item)[N]) :
StdIn(
StringView({item, N - 1},
true, StringEncoding::Ascii)) {}
171 using StdStream::StdStream;
192 const StdIn& stdIn = StdIn::Inherit{},
193 const StdErr& stdErr = StdErr::Inherit{})
195 SC_TRY(formatArguments(cmd));
196 return launch(stdOut, stdIn, stdErr);
208 const StdIn& stdIn = StdIn::Inherit{},
209 const StdErr& stdErr = StdErr::Inherit{})
211 SC_TRY(launch(cmd, stdOut, stdIn, stdErr));
212 return waitForExitSync();
254#if !SC_PLATFORM_WINDOWS
255 static constexpr size_t MAX_NUM_ARGUMENTS = 64;
256 size_t commandArgumentsByteOffset[MAX_NUM_ARGUMENTS];
257 size_t commandArgumentsNumber = 0;
262 static constexpr size_t MAX_NUM_ENVIRONMENT = 256;
264 size_t environmentByteOffset[MAX_NUM_ENVIRONMENT];
265 size_t environmentNumber = 0;
267 bool inheritEnv =
true;
269 friend struct IntrusiveDoubleLinkedList<
Process>;
277 Result launchImplementation();
310 const Process::StdErr& stdErr = Process::StdErr::Inherit{});
320 const Process::StdErr& stdErr = Process::StdErr::Inherit{})
322 SC_TRY(launch(stdOut, stdIn, stdErr));
323 return waitForExitSync();
327 IntrusiveDoubleLinkedList<Process> processes;
345 [[nodiscard]]
size_t size()
const {
return numberOfEnvironment; }
360 size_t numberOfEnvironment = 0;
361#if SC_PLATFORM_WINDOWS
362 static constexpr size_t MAX_ENVIRONMENTS = 256;
365 wchar_t* environment =
nullptr;
367 char** environment =
nullptr;
437 Side side = ForkParent;
438#if SC_PLATFORM_WINDOWS
439 ProcessDescriptor::Handle processHandle = ProcessDescriptor::Invalid;
440 ProcessDescriptor::Handle threadHandle = ProcessDescriptor::Invalid;
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition Compiler.h:78
int int32_t
Platform independent (4) bytes signed int.
Definition PrimitiveTypes.h:46
#define SC_TRY(expression)
Checks the value of the given expression and if failed, returns this value to caller.
Definition Result.h:48
An heap allocated byte buffer that can optionally use an inline buffer.
Definition Buffer.h:29
Open, read and write to/from a file descriptor (like a file or pipe).
Definition File.h:76
Read / Write pipe (Process stdin/stdout and IPC communication)
Definition File.h:195
Execute multiple child processes chaining input / output between them.
Definition Process.h:297
Result exec(const Process::StdOut &stdOut=Process::StdOut::Inherit{}, const Process::StdIn &stdIn=Process::StdIn::Inherit{}, const Process::StdErr &stdErr=Process::StdErr::Inherit{})
Launch the entire chain of processes and waits for the results (calling ProcessChain::waitForExitSync...
Definition Process.h:318
Result launch(const Process::StdOut &stdOut=Process::StdOut::Inherit{}, const Process::StdIn &stdIn=Process::StdIn::Inherit{}, const Process::StdErr &stdErr=Process::StdErr::Inherit{})
Launch the entire chain of processes.
Result pipe(Process &process, const Span< const StringView > cmd)
Add a process to the chain, with given arguments.
Result waitForExitSync()
Waits (blocking) for entire process chain to exit.
Reads current process environment variables.
Definition Process.h:335
bool get(size_t index, StringView &name, StringView &value) const
Get the environment variable at given index, returning its name and value.
bool contains(StringView variableName, size_t *index=nullptr)
Checks if an environment variable exists in current process.
size_t size() const
Returns the total number of environment variables for current process.
Definition Process.h:345
Wraps the code returned by a process that has exited.
Definition Process.h:17
Forks current process exiting child at end of process A fork duplicates a parent process execution st...
Definition Process.h:397
Result waitForChild()
Waits for child fork to finish execution.
State
Definition Process.h:413
@ Suspended
Start the forked process suspended (resume it with ProcessFork::resumeChildFork)
Definition Process.h:414
@ Immediate
Start the forked process immediately.
Definition Process.h:415
Side getSide() const
Obtain process parent / fork side.
Definition Process.h:410
FileDescriptor & getWritePipe()
Gets the descriptor to "write" something to the other side.
Side
Definition Process.h:404
@ ForkParent
Parent side of the fork.
Definition Process.h:405
@ ForkChild
Child side of the fork.
Definition Process.h:406
Result resumeChildFork()
Sends 1 byte on parentToFork to resume State::Paused child fork.
FileDescriptor & getReadPipe()
Gets the descriptor to "read" something from the other side.
Result fork(State state)
Forks current process (use ForkProcess::getType to know the side)
int32_t getExitStatus() const
Gets the return code from the exited child fork.
Definition Process.h:428
Native os handle to a process identifier.
Definition Process.h:29
bool windowsHide
[Windows] Hides child process window (default == Process::isWindowsConsoleSubsystem)
Definition Process.h:70
StdIn(StringView string)
Fills standard input with content of a StringView.
Definition Process.h:166
StdIn(const char(&item)[N])
Fills standard input with content of a C-String.
Definition Process.h:163
StdIn(Span< const char > span)
Fills standard input with content of a Span.
Definition Process.h:169
StdIn(Inherit)
Inherits child process Input from parent process.
Definition Process.h:160
StdOut(Span< char > span)
Read the process standard output/error into the given Span.
Definition Process.h:145
StdOut(Ignore)
Ignores child process standard output/error (child process output will be silenced)
Definition Process.h:139
StdOut(Inherit)
Inherits child process standard output/error (child process will print into parent process console)
Definition Process.h:142
StdStream(Buffer &externalBuffer)
Read the process standard output/error into the given Vector.
Definition Process.h:81
StdStream(String &externalString)
Read the process standard output/error into the given String.
Definition Process.h:78
StdStream(FileDescriptor &&file)
Redirects child process standard output/error to a given file descriptor.
Definition Process.h:85
Execute a child process with standard file descriptors redirection.
Definition Process.h:67
int32_t getExitStatus() const
gets the return code from the exited child process (valid only after exec or waitForExitSync)
Definition Process.h:216
Result exec(Span< const StringView > cmd, const StdOut &stdOut=StdOut::Inherit{}, const StdIn &stdIn=StdIn::Inherit{}, const StdErr &stdErr=StdErr::Inherit{})
Executes a child process with the given arguments, waiting (blocking) until it's fully finished.
Definition Process.h:206
Result setWorkingDirectory(StringView processWorkingDirectory)
Sets the starting working directory of the process that will be launched / executed.
Result setEnvironment(StringView environmentVariable, StringView value)
Sets the environment variable for the newly spawned child process.
Result launch(Span< const StringView > cmd, const StdOut &stdOut=StdOut::Inherit{}, const StdIn &stdIn=StdIn::Inherit{}, const StdErr &stdErr=StdErr::Inherit{})
Launch child process with the given arguments.
Definition Process.h:190
Options options
Options for the child process (hide console window etc.)
Definition Process.h:178
static bool isWindowsEmulatedProcess()
Returns true if we're emulating x64 on ARM64 or the inverse on Windows.
void inheritParentEnvironmentVariables(bool inherit)
Controls if the newly spawned child process will inherit parent process environment variables.
Definition Process.h:222
ProcessID processID
ID of the process (can be the same as handle on some OS)
Definition Process.h:177
static bool isWindowsConsoleSubsystem()
Returns true only under Windows if executable is compiled with /SUBSYSTEM:Console
static size_t getNumberOfProcessors()
Returns number of (virtual) processors available.
ProcessDescriptor handle
Handle to the OS process.
Definition Process.h:176
Result waitForExitSync()
Waits (blocking) for process to exit after launch. It can only be called if Process::launch succeeded...
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:12
String with compile time configurable inline storage (small string optimization)
Definition StringFormat.h:12
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29
Non-owning view over a range of characters with UTF Encoding.
Definition StringView.h:48
A non-modifiable owning string with associated encoding.
Definition String.h:29
A contiguous sequence of heap allocated elements.
Definition Vector.h:189