5#include "../Containers/IntrusiveDoubleLinkedList.h"
6#include "../File/FileDescriptor.h"
7#include "../Strings/SmallString.h"
8#include "ProcessDescriptor.h"
15struct ProcessEnvironment;
75 StdStream(
String& externalString) { operation = Operation::String;
string = &externalString;}
84 operation = Operation::FileDescriptor;
85 (void)file.get(fileDescriptor,
Result::Error(
"Invalid redirection file descriptor"));
91 operation = Operation::ExternalPipe;
92 pipeDescriptor = &pipe;
116 Operation operation = Operation::Inherit;
124 FileDescriptor::Handle fileDescriptor;
144 using StdStream::StdStream;
149 using StdErr = StdOut;
168 using StdStream::StdStream;
189 const StdIn& stdIn = StdIn::Inherit{},
190 const StdErr& stdErr = StdErr::Inherit{})
192 SC_TRY(formatArguments(cmd));
193 return launch(stdOut, stdIn, stdErr);
205 const StdIn& stdIn = StdIn::Inherit{},
206 const StdErr& stdErr = StdErr::Inherit{})
248#if !SC_PLATFORM_WINDOWS
249 static constexpr size_t MAX_NUM_ARGUMENTS = 64;
250 size_t commandArgumentsByteOffset[MAX_NUM_ARGUMENTS];
251 size_t commandArgumentsNumber = 0;
256 static constexpr size_t MAX_NUM_ENVIRONMENT = 256;
258 size_t environmentByteOffset[MAX_NUM_ENVIRONMENT];
259 size_t environmentNumber = 0;
261 bool inheritEnv =
true;
271 Result launchImplementation();
304 const Process::StdErr& stdErr = Process::StdErr::Inherit{});
314 const Process::StdErr& stdErr = Process::StdErr::Inherit{})
321 IntrusiveDoubleLinkedList<Process> processes;
339 [[nodiscard]]
size_t size()
const {
return numberOfEnvironment; }
354 size_t numberOfEnvironment = 0;
355#if SC_PLATFORM_WINDOWS
356 static constexpr size_t MAX_ENVIRONMENTS = 256;
359 wchar_t* environment =
nullptr;
361 char** environment =
nullptr;
#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:47
@ Ascii
Encoding is ASCII.
@ Native
Encoding is UTF8.
Wraps an OS File descriptor to read and write to and from it.
Definition: FileDescriptor.h:57
An Intrusive Double Linked List.
Definition: IntrusiveDoubleLinkedList.h:22
Descriptor representing a Pipe used for InterProcess Communication (IPC)
Definition: FileDescriptor.h:202
bool windowsHide
[Windows] Hides child process window (default == Process::isWindowsConsoleSubsystem)
Definition: Process.h:67
Definition: Process.h:154
Definition: Process.h:152
StdIn(StringView string)
Fills standard input with content of a StringView.
Definition: Process.h:163
StdIn(const char(&item)[N])
Fills standard input with content of a C-String.
Definition: Process.h:160
StdIn(Span< const char > span)
Fills standard input with content of a Span.
Definition: Process.h:166
StdIn(Inherit)
Inherits child process Input from parent process.
Definition: Process.h:157
Definition: Process.h:132
Definition: Process.h:133
Definition: Process.h:130
StdOut(Span< char > span)
Read the process standard output/error into the given Span.
Definition: Process.h:142
StdOut(Ignore)
Ignores child process standard output/error (child process output will be silenced)
Definition: Process.h:136
StdOut(Inherit)
Inherits child process standard output/error (child process will print into parent process console)
Definition: Process.h:139
StdStream(Vector< char > &externalVector)
Read the process standard output/error into the given Vector.
Definition: Process.h:78
StdStream(String &externalString)
Read the process standard output/error into the given String.
Definition: Process.h:75
StdStream(FileDescriptor &&file)
Redirects child process standard output/error to a given file descriptor.
Definition: Process.h:82
Execute multiple child processes chaining input / output between them.
Definition: Process.h:291
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:312
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:329
bool get(size_t index, StringView &name, StringView &value) const
Get the environmnent 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:339
Execute a child process with standard file descriptors redirection.
Definition: Process.h:64
int32_t getExitStatus() const
gets the return code from the exited child process (valid only after exec or waitForExitSync)
Definition: Process.h:213
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:203
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:187
Options options
Options for the child process (hide console window etc.)
Definition: Process.h:175
void inheritParentEnvironmentVariables(bool inherit)
Controls if the newly spawned child process will inherit parent process environment variables.
Definition: Process.h:219
ProcessID processID
ID of the process (can be the same as handle on some OS)
Definition: Process.h:174
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:173
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:26
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition: Result.h:11
static constexpr Result Error(const char(&msg)[numChars])
Constructs an Error from a pointer to an ASCII string literal.
Definition: Result.h:23
String with compile time configurable inline storage (small string optimization)
Definition: SmallString.h:21
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:20
A non-modifiable owning string with associated encoding.
Definition: String.h:30
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:51