![]() |
chigraph
master
Systems programming language written for beginners in LLVM
|
Provides an platform-independent abstraction for creating subprocesses. More...
#include <chi/Support/Subprocess.hpp>
Public Types | |
using | pipeHandler = std::function< void(const char *data, size_t size)> |
The function type for recieving data from pipes. | |
Public Member Functions | |
Subprocess (const boost::filesystem::path &pathToExecutable) | |
Construct a Subprocess with a path to an executable. More... | |
~Subprocess () | |
Wait for the process to exit and close all open handles. | |
Result | start () |
Start the process. More... | |
bool | started () const |
Check if the child has started (start() has been called) More... | |
Setup Functions | |
template<typename ForwardIterator > | |
void | setArguments (const ForwardIterator &begin, const ForwardIterator &end) |
Set the arguments for the program with a begin and end iterator. More... | |
template<typename Range > | |
void | setArguments (Range &&range) |
Set the arguments for the program with a range. More... | |
void | setArguments (std::initializer_list< const char *> init) |
Set the arguments for the program with an initializer_list. More... | |
const std::vector< std::string > & | arguments () const |
Get the currently set arguments for the program. More... | |
void | attachToStdOut (pipeHandler stdOutHandler) |
Attach a function handler to the child stdout. More... | |
void | attachToStdErr (pipeHandler stdErrHandler) |
Attach a function handler to the child stderr. More... | |
void | attachStringToStdOut (std::string &str) |
Attach a string to stdout. More... | |
void | attachStringToStdErr (std::string &str) |
Attach a string to stderr. More... | |
void | setWorkingDirectory (boost::filesystem::path newWd) |
Set the working directory of the process. More... | |
Runtime Functions | |
These functions are called while the process has started (ie after start() is called) | |
Result | pushToStdIn (const char *data, size_t size) |
Pushes data to the stdin stream of the child process. More... | |
Result | closeStdIn () |
Close the STDIN stream (send an EOF) More... | |
bool | isStdInClosed () const |
Checks if the stdin is closed. More... | |
void | kill () |
Kill the child process On POSIX, this sends SIGINT. More... | |
void | wait () |
Wait for the process to complete. More... | |
int | exitCode () |
Wait and gets the exit code. More... | |
bool | running () |
Check if the process is still running. More... | |
Provides an platform-independent abstraction for creating subprocesses.
On OSX and Linux, this uses the POSIX api (pipe()
, fork()
, exec()
, write()
, read()
, etc) and on windows it uses the win32 API (CreatePipe()
, CreateProcess()
, ReadFile()
, etc)
Usage is you create a Subprocess class:
Now you can use any of the Setup Functions (see above). The string we pass to attachStringToStdOut
is written from a differnt thread, so it's not safe to acces it until the program has exited.
Once you're sure all youre setup variables are correctly set, start the process. You cannot change any of the setup values once you have called start()
Now that it's started, we can write data (if we wish) to stdin:
Now, you can use running()
to see if the process has exited yet. We'll just use exitCode()
to wait for the process to complete and get its exit code:
Now that the process has exited, it's safe to use the stdOut
string:
Definition at line 52 of file Subprocess.hpp.
chi::Subprocess::Subprocess | ( | const boost::filesystem::path & | pathToExecutable | ) |
Construct a Subprocess with a path to an executable.
boost::filesystem::is_regular_file(pathToExecutable)
Definition at line 572 of file Subprocess.cpp.
|
inline |
Get the currently set arguments for the program.
Definition at line 104 of file Subprocess.hpp.
|
inline |
Attach a string to stderr.
Everything added to the string is appended. This is just a convenicence function, it just calls attachToStdout with a function appending the data to the string.
str | The string to append stderr to |
str
while the program is running, it could create a race condition. started() == false
Definition at line 146 of file Subprocess.hpp.
References attachToStdErr().
Referenced by chi::compileCToLLVM().
|
inline |
Attach a string to stdout.
Everything added to the string is appended. This is just a convenicence function, it just calls attachToStdout with a function appending the data to the string.
str | The string to append stdout to |
str
while the program is running, it could create a race condition. started() == false
Definition at line 135 of file Subprocess.hpp.
References attachToStdOut().
Referenced by chi::compileCToLLVM().
|
inline |
Attach a function handler to the child stderr.
Every time data is recieved through the stderr pipe of the child, it will be sent to this handler
stdErrHandler | The handler |
stdErrHandler
will exclusively be called from another thread. started() == false
Definition at line 122 of file Subprocess.hpp.
References started().
Referenced by attachStringToStdErr().
|
inline |
Attach a function handler to the child stdout.
Every time data is recieved through the stdout pipe of the child, it will be sent to this handler
stdOutHandler | The handler |
stdOutHandler
will exclusively be called from another thread. started() == false
Definition at line 111 of file Subprocess.hpp.
References started().
Referenced by attachStringToStdOut().
Result chi::Subprocess::closeStdIn | ( | ) |
Close the STDIN stream (send an EOF)
!isStdInClosed()
running()
isStdInClosed()
Definition at line 388 of file Subprocess.cpp.
References chi::Result::addEntry().
Referenced by chi::compileCToLLVM(), and started().
int chi::Subprocess::exitCode | ( | ) |
Wait and gets the exit code.
started()
Definition at line 534 of file Subprocess.cpp.
Referenced by chi::compileCToLLVM(), and isStdInClosed().
|
inline |
Checks if the stdin is closed.
Definition at line 188 of file Subprocess.hpp.
References exitCode(), kill(), running(), and wait().
void chi::Subprocess::kill | ( | ) |
Kill the child process On POSIX, this sends SIGINT.
started()
Definition at line 520 of file Subprocess.cpp.
Referenced by isStdInClosed().
Result chi::Subprocess::pushToStdIn | ( | const char * | data, |
size_t | size | ||
) |
Pushes data to the stdin stream of the child process.
data | The pointer to the start of the data |
size | How long the data is |
started()
!isStdInClosed()
Definition at line 374 of file Subprocess.cpp.
References chi::Result::addEntry().
Referenced by chi::compileCToLLVM(), and started().
bool chi::Subprocess::running | ( | ) |
Check if the process is still running.
started()
Definition at line 550 of file Subprocess.cpp.
Referenced by isStdInClosed().
|
inline |
Set the arguments for the program with a begin and end iterator.
begin | The start iterator of arguments. Should be an iterator of something converatable to std::string |
end | The end iterator. Should be an iterator of something converatable to std::string |
started() == false
Definition at line 75 of file Subprocess.hpp.
References started().
Referenced by chi::compileCToLLVM(), and setArguments().
|
inline |
Set the arguments for the program with a range.
range | The range. Must have begin() and end() functions, which return iterators. |
started() == false
Definition at line 91 of file Subprocess.hpp.
References setArguments().
|
inline |
Set the arguments for the program with an initializer_list.
init | the initializer list |
started() == false
Definition at line 98 of file Subprocess.hpp.
References setArguments().
|
inline |
Set the working directory of the process.
newWd | The new working directory |
started() == false
Definition at line 153 of file Subprocess.hpp.
References start().
Result chi::Subprocess::start | ( | ) |
Start the process.
After this is called, most of the Setup Functions cannot be called anymore
started() == false
started()
Definition at line 405 of file Subprocess.cpp.
References chi::Result::addEntry().
Referenced by chi::compileCToLLVM(), and setWorkingDirectory().
|
inline |
Check if the child has started (start()
has been called)
Definition at line 166 of file Subprocess.hpp.
References closeStdIn(), and pushToStdIn().
Referenced by attachToStdErr(), attachToStdOut(), and setArguments().
void chi::Subprocess::wait | ( | ) |
Wait for the process to complete.
started()
Definition at line 525 of file Subprocess.cpp.
Referenced by isStdInClosed().