Platform-independent interface to create and control a subprocess. More...
#include <subprocess.h>
Public Types | |
typedef std::vector< std::string > | CommandLine |
typedef std::vector< std::string > | Environment |
enum | RedirectMode { RM_NONE, RM_PIPE, RM_STDOUT } |
Modes of redirection for standard input/output buffers. More... | |
Public Member Functions | |
bool | communicate (std::istream &in, std::ostream &out, std::ostream &err) |
Communicate with subprocess. More... | |
bool | communicate (std::ostream &out, std::ostream &err) |
Communicate with subprocess. More... | |
bool | communicate (std::ostream &out) |
Communicate with subprocess. More... | |
bool | kill () |
Kill subprocess. More... | |
int | pid () const |
bool | poll () const |
Check if subprocess terminated and update return code. More... | |
bool | popen (const CommandLine &args, const RedirectMode rm_in=RM_NONE, const RedirectMode rm_out=RM_NONE, const RedirectMode rm_err=RM_NONE, const Environment *env=NULL) |
Open new subprocess. More... | |
bool | popen (const std::string &cmd, const RedirectMode rm_in=RM_NONE, const RedirectMode rm_out=RM_NONE, const RedirectMode rm_err=RM_NONE, const Environment *env=NULL) |
Open new subprocess. More... | |
int | read (void *buf, size_t nbuf, bool err=false) |
Read data from stdout or stderr of subprocess. More... | |
int | returncode () const |
bool | send_signal (int signal) |
Send signal to subprocess. More... | |
bool | signaled () const |
Subprocess () | |
Default constructor. More... | |
bool | terminate () |
Terminate subprocess. More... | |
bool | wait () |
Wait for subprocess to terminate. More... | |
int | write (const void *buf, size_t nbuf) |
Write data to stdin of subprocess. More... | |
~Subprocess () | |
Terminate running subprocess and close all related handles. More... | |
Static Public Member Functions | |
static int | call (const CommandLine &cmd) |
Execute command as subprocess. More... | |
static int | call (const std::string &cmd) |
Execute command as subprocess. More... | |
static CommandLine | split (const std::string &cmd) |
Split double quoted string into arguments. More... | |
static std::string | tostring (const CommandLine &args) |
Convert argument vector to double quoted string. More... | |
Platform-independent interface to create and control a subprocess.
Definition at line 40 of file subprocess.h.
typedef std::vector<std::string> basis::Subprocess::CommandLine |
Definition at line 46 of file subprocess.h.
typedef std::vector<std::string> basis::Subprocess::Environment |
Definition at line 47 of file subprocess.h.
Modes of redirection for standard input/output buffers.
Enumerator | |
---|---|
RM_NONE | Do not redirect the input/output. |
RM_PIPE | Use a pipe to redirect the input/output from/to the parent. |
RM_STDOUT | Redirect stderr to stdout. |
Definition at line 75 of file subprocess.h.
basis::Subprocess::Subprocess | ( | ) |
Default constructor.
Definition at line 177 of file subprocess.cxx.
basis::Subprocess::~Subprocess | ( | ) |
Terminate running subprocess and close all related handles.
Definition at line 194 of file subprocess.cxx.
|
static |
Execute command as subprocess.
This function is implemented in the same manner as system() on Unix. It simply creates a Subprocess instance, executes the subprocess and waits for its termination.
Example:
Definition at line 771 of file subprocess.cxx.
|
static |
Execute command as subprocess.
This function is implemented in the same manner as system() on Unix. It simply creates a Subprocess instance, executes the subprocess and waits for its termination.
Example:
[in] | cmd | Command-line given as single string. Quote arguments containing whitespace characters using ". |
Definition at line 779 of file subprocess.cxx.
bool basis::Subprocess::communicate | ( | std::istream & | in, |
std::ostream & | out, | ||
std::ostream & | err | ||
) |
Communicate with subprocess.
[in] | in | Data send to subprocess via pipe to stdin. If no pipe was setup during subprocess creation, this function does nothing and returns false. |
[in] | out | Data read from stdout of subprocess. Can be an empty string if no pipe was created for stdout. |
[in] | err | Data read from stderr of subprocess. Can be an empty string if no pipe was created for stderr. |
Definition at line 634 of file subprocess.cxx.
bool basis::Subprocess::communicate | ( | std::ostream & | out, |
std::ostream & | err | ||
) |
Communicate with subprocess.
[in] | out | Data read from stdout of subprocess. Can be an empty string if no pipe was created for stdout. |
[in] | err | Data read from stderr of subprocess. Can be an empty string if no pipe was created for stderr. |
Definition at line 705 of file subprocess.cxx.
bool basis::Subprocess::communicate | ( | std::ostream & | out | ) |
Communicate with subprocess.
[in] | out | Data read from stdout of subprocess. Can be an empty string if no pipe was created for stdout. |
Definition at line 719 of file subprocess.cxx.
bool basis::Subprocess::kill | ( | ) |
Kill subprocess.
Definition at line 565 of file subprocess.cxx.
int basis::Subprocess::pid | ( | ) | const |
Definition at line 610 of file subprocess.cxx.
bool basis::Subprocess::poll | ( | ) | const |
Check if subprocess terminated and update return code.
This method returns immediately and does not wait for the subprocess to actually being terminated. For that purpuse, use wait() instead.
Definition at line 466 of file subprocess.cxx.
bool basis::Subprocess::popen | ( | const CommandLine & | args, |
const RedirectMode | rm_in = RM_NONE , |
||
const RedirectMode | rm_out = RM_NONE , |
||
const RedirectMode | rm_err = RM_NONE , |
||
const Environment * | env = NULL |
||
) |
Open new subprocess.
This method creates the subprocess and returns immediately. In order to wait for the suprocess to finish, the wait() method has to be called explicitly.
[in] | args | Command-line of subprocess. The first argument has to be the name/path of the command to be executed. |
[in] | rm_in | Mode used for redirection of stdin of subprocess. Can be either RM_NONE or RM_PIPE. |
[in] | rm_out | Mode used for redirection of stdout of subprocess. Can be either RM_NONE or RM_PIPE. |
[in] | rm_err | Mode used for redirection of stderr of subprocess. Can be either RM_NONE, RM_PIPE, or RM_STDOUT. |
[in] | env | Environment for the subprocess. If NULL is given, the environment of the parent process is used. |
Definition at line 218 of file subprocess.cxx.
|
inline |
Open new subprocess.
This method creates the subprocess and returns immediately. In order to wait for the suprocess to finish, the wait() method has to be called explicitly.
[in] | cmd | Command-line given as double quoted string. Arguments containing whitespaces have to be quoted using double quotes. Use a backslash () to escape double quotes inside an argument as well as to escape a backslash itself (required if backslash at end of double quoted argument, e.g., "this argument \\"). |
[in] | rm_in | Mode used for redirection of stdin of subprocess. Can be either RM_NONE or RM_PIPE. |
[in] | rm_out | Mode used for redirection of stdout of subprocess. Can be either RM_NONE or RM_PIPE. |
[in] | rm_err | Mode used for redirection of stderr of subprocess. Can be either RM_NONE, RM_PIPE, or RM_STDOUT. |
[in] | env | Environment for the subprocess. If NULL is given, the environment of the parent process is used. |
Definition at line 172 of file subprocess.h.
int basis::Subprocess::read | ( | void * | buf, |
size_t | nbuf, | ||
bool | err = false |
||
) |
Read data from stdout or stderr of subprocess.
[out] | buf | Allocated buffer to store read data to. |
[in] | nbuf | Number of bytes to read from subprocess. |
[in] | err | If true and the redirection mode of stderr is RM_PIPE, the data is read from stderr of the subprocess. Otherwise, the data is read from stdout. |
Definition at line 751 of file subprocess.cxx.
int basis::Subprocess::returncode | ( | ) | const |
Definition at line 620 of file subprocess.cxx.
bool basis::Subprocess::send_signal | ( | int | signal | ) |
Send signal to subprocess.
On Windows, SIGTERM is an alias for terminate() and SIGKILL an alias for kill() which in turn is nothing else but a termination of the subprocess. All other signals are only sent to POSIX processes.
Definition at line 529 of file subprocess.cxx.
bool basis::Subprocess::signaled | ( | ) | const |
Definition at line 575 of file subprocess.cxx.
|
static |
Split double quoted string into arguments.
[in] | cmd | Double quoted string. Use '\' to escape double quotes within arguments. |
Definition at line 71 of file subprocess.cxx.
bool basis::Subprocess::terminate | ( | ) |
Terminate subprocess.
Definition at line 541 of file subprocess.cxx.
|
static |
Convert argument vector to double quoted string.
[in] | args | Argument vector. |
Definition at line 141 of file subprocess.cxx.
bool basis::Subprocess::wait | ( | ) |
Wait for subprocess to terminate.
This method also sets the exit code as returned by GetExitCode().
Definition at line 504 of file subprocess.cxx.
int basis::Subprocess::write | ( | const void * | buf, |
size_t | nbuf | ||
) |
Write data to stdin of subprocess.
[in] | buf | Bytes to write. |
[in] | nbuf | Number of bytes from buf to write. |
Definition at line 738 of file subprocess.cxx.