5#include "../Containers/IntrusiveDoubleLinkedList.h"
6#include "../File/FileDescriptor.h"
7#include "../Strings/String.h"
8#include "ProcessDescriptor.h"
16struct ProcessEnvironment;
76 StdStream(
String& externalString) { operation = Operation::String;
string = &externalString;}
79 StdStream(
Buffer& externalBuffer) { operation = Operation::Vector; buffer = &externalBuffer; }
85 operation = Operation::FileDescriptor;
86 (void)file.get(fileDescriptor,
Result::Error(
"Invalid redirection file descriptor"));
92 operation = Operation::ExternalPipe;
93 pipeDescriptor = &pipe;
117 Operation operation = Operation::Inherit;
125 FileDescriptor::Handle fileDescriptor;
145 using StdStream::StdStream;
150 using StdErr = StdOut;
169 using StdStream::StdStream;
190 const StdIn& stdIn = StdIn::Inherit{},
191 const StdErr& stdErr = StdErr::Inherit{})
193 SC_TRY(formatArguments(cmd));
194 return launch(stdOut, stdIn, stdErr);
206 const StdIn& stdIn = StdIn::Inherit{},
207 const StdErr& stdErr = StdErr::Inherit{})
252#if !SC_PLATFORM_WINDOWS
253 static constexpr size_t MAX_NUM_ARGUMENTS = 64;
254 size_t commandArgumentsByteOffset[MAX_NUM_ARGUMENTS];
255 size_t commandArgumentsNumber = 0;
260 static constexpr size_t MAX_NUM_ENVIRONMENT = 256;
262 size_t environmentByteOffset[MAX_NUM_ENVIRONMENT];
263 size_t environmentNumber = 0;
265 bool inheritEnv =
true;
275 Result launchImplementation();
308 const Process::StdErr& stdErr = Process::StdErr::Inherit{});
318 const Process::StdErr& stdErr = Process::StdErr::Inherit{})
325 IntrusiveDoubleLinkedList<Process> processes;
343 [[nodiscard]]
size_t size()
const {
return numberOfEnvironment; }
358 size_t numberOfEnvironment = 0;
359#if SC_PLATFORM_WINDOWS
360 static constexpr size_t MAX_ENVIRONMENTS = 256;
363 wchar_t* environment =
nullptr;
365 char** environment =
nullptr;
436#if SC_PLATFORM_WINDOWS
437 detail::ProcessDescriptorDefinition::Handle processHandle = detail::ProcessDescriptorDefinition::Invalid;
438 detail::ProcessDescriptorDefinition::Handle threadHandle = detail::ProcessDescriptorDefinition::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
@ Ascii
Encoding is ASCII.
@ Native
Encoding is UTF8.
An heap allocated byte buffer that can optionally use an inline buffer.
Definition: Buffer.h:28
File Descriptor (use SC::File to open and use it with strings and buffers).
Definition: FileDescriptor.h:52
An Intrusive Double Linked List.
Definition: IntrusiveDoubleLinkedList.h:24
Read / Write pipe (Process stdin/stdout and IPC communication)
Definition: FileDescriptor.h:153
bool windowsHide
[Windows] Hides child process window (default == Process::isWindowsConsoleSubsystem)
Definition: Process.h:68
Definition: Process.h:155
Definition: Process.h:153
StdIn(StringView string)
Fills standard input with content of a StringView.
Definition: Process.h:164
StdIn(const char(&item)[N])
Fills standard input with content of a C-String.
Definition: Process.h:161
StdIn(Span< const char > span)
Fills standard input with content of a Span.
Definition: Process.h:167
StdIn(Inherit)
Inherits child process Input from parent process.
Definition: Process.h:158
Definition: Process.h:133
Definition: Process.h:134
Definition: Process.h:131
StdOut(Span< char > span)
Read the process standard output/error into the given Span.
Definition: Process.h:143
StdOut(Ignore)
Ignores child process standard output/error (child process output will be silenced)
Definition: Process.h:137
StdOut(Inherit)
Inherits child process standard output/error (child process will print into parent process console)
Definition: Process.h:140
StdStream(Buffer &externalBuffer)
Read the process standard output/error into the given Vector.
Definition: Process.h:79
StdStream(String &externalString)
Read the process standard output/error into the given String.
Definition: Process.h:76
StdStream(FileDescriptor &&file)
Redirects child process standard output/error to a given file descriptor.
Definition: Process.h:83
Execute multiple child processes chaining input / output between them.
Definition: Process.h:295
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:316
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.
Definition: ProcessDescriptor.h:44
Wraps an OS Process descriptor.
Definition: ProcessDescriptor.h:42
Reads current process environment variables.
Definition: Process.h:333
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:343
Forks current process exiting child at end of process A fork duplicates a parent process execution st...
Definition: Process.h:395
Result waitForChild()
Waits for child fork to finish execution.
State
Definition: Process.h:411
@ Suspended
Start the forked process suspended (resume it with ProcessFork::resumeChildFork)
Definition: Process.h:412
@ Immediate
Start the forked process immediately.
Definition: Process.h:413
Side getSide() const
Obtain process parent / fork side.
Definition: Process.h:408
FileDescriptor & getWritePipe()
Gets the descriptor to "write" something to the other side.
Side
Definition: Process.h:402
@ ForkParent
Parent side of the fork.
Definition: Process.h:403
@ ForkChild
Child side of the fork.
Definition: Process.h:404
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:426
Execute a child process with standard file descriptors redirection.
Definition: Process.h:65
int32_t getExitStatus() const
gets the return code from the exited child process (valid only after exec or waitForExitSync)
Definition: Process.h:214
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:204
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:188
Options options
Options for the child process (hide console window etc.)
Definition: Process.h:176
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:220
ProcessID processID
ID of the process (can be the same as handle on some OS)
Definition: Process.h:175
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:174
Result waitForExitSync()
Waits (blocking) for process to exit after launch. It can only be called if Process::launch succeeded...
Native os handle to a process identifier.
Definition: Process.h:27
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition: Result.h:12
static constexpr Result Error(const char(&msg)[numChars])
Constructs an Error from a pointer to an ASCII string literal.
Definition: Result.h:24
String with compile time configurable inline storage (small string optimization)
Definition: String.h:168
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:32
A non-modifiable owning string with associated encoding.
Definition: String.h:29
Non-owning view over a range of characters with UTF Encoding.
Definition: StringView.h:47
A contiguous sequence of heap allocated elements.
Definition: Vector.h:189