subprocess
0.4.0
Modern subprocess library for c++
|
Namespaces | |
details | |
Classes | |
struct | CalledProcessError |
struct | CommandNotFoundError |
struct | CompletedProcess |
class | CwdGuard |
class | EnvGuard |
class | Environ |
class | EnvironSetter |
struct | OSError |
struct | PipePair |
struct | Popen |
class | ProcessBuilder |
struct | RunBuilder |
struct | RunOptions |
struct | SpawnError |
class | StopWatch |
struct | SubprocessError |
struct | TimeoutExpired |
Typedefs | |
typedef intptr_t | ssize_t |
typedef int | PipeHandle |
typedef ::pid_t | pid_t |
typedef std::vector< std::string > | CommandLine |
typedef std::map< std::string, std::string > | EnvMap |
typedef std::variant< PipeOption, std::string, PipeHandle, std::istream *, std::ostream *, FILE * > | PipeVar |
Enumerations | |
enum | SigNum { PSIGHUP = 1, PSIGINT = SIGINT, PSIGQUIT = 3, PSIGILL = SIGILL, PSIGTRAP = 5, PSIGABRT = SIGABRT, PSIGIOT = 6, PSIGBUS = 7, PSIGFPE = SIGFPE, PSIGKILL = 9, PSIGUSR1 = 10, PSIGSEGV = SIGSEGV, PSIGUSR2 = 12, PSIGPIPE = 13, PSIGALRM = 14, PSIGTERM = SIGTERM, PSIGSTKFLT = 16, PSIGCHLD = 17, PSIGCONT = 18, PSIGSTOP = 19, PSIGTSTP = 20, PSIGTTIN = 21, PSIGTTOU = 22, PSIGURG = 23, PSIGXCPU = 24, PSIGXFSZ = 25, PSIGVTALRM = 26, PSIGPROF = 27, PSIGWINCH = 28, PSIGIO = 29 } |
enum | PipeOption : int { PipeOption::inherit, PipeOption::cout, PipeOption::cerr, PipeOption::specific, PipeOption::pipe, PipeOption::close } |
enum | PipeVarIndex { PipeVarIndex::option, PipeVarIndex::string, PipeVarIndex::handle, PipeVarIndex::istream, PipeVarIndex::ostream, PipeVarIndex::file } |
Functions | |
EnvMap | current_env_copy () |
std::u16string | create_env_block (const EnvMap &map) |
bool | pipe_close (PipeHandle handle) |
PipePair | pipe_create (bool inheritable=true) |
void | pipe_set_inheritable (PipeHandle handle, bool inheritable) |
ssize_t | pipe_read (PipeHandle, void *buffer, size_t size) |
ssize_t | pipe_write (PipeHandle, const void *buffer, size_t size) |
void | pipe_ignore_and_close (PipeHandle handle) |
std::string | pipe_read_all (PipeHandle handle) |
PipeOption | get_pipe_option (const PipeVar &option) |
CompletedProcess | run (Popen &popen, bool check=false) |
CompletedProcess | run (CommandLine command, RunOptions options={}) |
double | monotonic_seconds () |
double | sleep_seconds (double seconds) |
std::string | getenv (const std::string &var) |
std::string | find_program (const std::string &name) |
void | find_program_clear_cache () |
std::string | escape_shell_arg (std::string arg) |
std::string | getcwd () |
void | setcwd (const std::string &path) |
std::string | abspath (std::string dir, std::string relativeTo="") |
Variables | |
constexpr bool | kIsWin32 = false |
constexpr char | kPathDelimiter = ':' |
const PipeHandle | kBadPipeValue = (PipeHandle)-1 |
constexpr int | kStdInValue = 0 |
constexpr int | kStdOutValue = 1 |
constexpr int | kStdErrValue = 2 |
constexpr int | kBadReturnCode = -1000 |
Environ | cenv |
typedef std::vector<std::string> subprocess::CommandLine |
typedef std::map<std::string, std::string> subprocess::EnvMap |
typedef ::pid_t subprocess::pid_t |
typedef int subprocess::PipeHandle |
typedef std::variant<PipeOption, std::string, PipeHandle, std::istream*, std::ostream*, FILE*> subprocess::PipeVar |
typedef intptr_t subprocess::ssize_t |
|
strong |
Redirect destination
|
strong |
enum subprocess::SigNum |
Signals to send. they start with P because SIGX are macros, P stands for Posix as these values are as defined by Posix.
std::string subprocess::abspath | ( | std::string | dir, |
std::string | relativeTo = "" |
||
) |
Converts dir to be absolute path.
dir | The path you want to be absolute. |
relativeTo | If specified use this instead of the current working directory. |
std::u16string subprocess::create_env_block | ( | const EnvMap & | map | ) |
Gives an environment block used in Windows APIs. Each item is null terminated, end of list is double null-terminated and conforms to expectations of various windows API.
EnvMap subprocess::current_env_copy | ( | ) |
Creates a copy of current environment variables and returns the map
std::string subprocess::escape_shell_arg | ( | std::string | arg | ) |
Escapes the argument suitable for use on command line.
std::string subprocess::find_program | ( | const std::string & | name | ) |
Searches for program in PATH environment variable.
on windows tries adding suffixes specified in PATHEXT environment variable.
on windows an input of "python3" will also search "python" executables and inspect their version to find an executable that offers python 3.x.
void subprocess::find_program_clear_cache | ( | ) |
Clears cache used by find_program.
find_program uses internal cache to find executables. If you modify PATH using subprocess::cenv this will be called automatically for you. If a new program is added to a folder with the same name as an existing program you may want to clear the cache so that the new program is found as expected instead of the old program being returned.
|
inline |
std::string subprocess::getcwd | ( | ) |
Gets the current working directory of the process
std::string subprocess::getenv | ( | const std::string & | var | ) |
Get the value of environment variable
double subprocess::monotonic_seconds | ( | ) |
bool subprocess::pipe_close | ( | PipeHandle | handle | ) |
Closes a pipe handle.
handle | The handle to close. |
PipePair subprocess::pipe_create | ( | bool | inheritable = true | ) |
Creates a pair of pipes for input/output
inheritable | if true subprocesses will inherit the pipe. |
OSError | if system call fails. |
void subprocess::pipe_ignore_and_close | ( | PipeHandle | handle | ) |
Spawns a thread to read from the pipe. When no more data available pipe will be closed.
ssize_t subprocess::pipe_read | ( | PipeHandle | , |
void * | buffer, | ||
size_t | size | ||
) |
std::string subprocess::pipe_read_all | ( | PipeHandle | handle | ) |
Read contents of handle until no more data is available.
If the pipe is non-blocking this will end prematurely.
void subprocess::pipe_set_inheritable | ( | PipeHandle | handle, |
bool | inheritable | ||
) |
Set the pipe to be inheritable or not for subprocess.
OSError | if system call fails. |
inheritable | if true handle will be inherited in subprocess. |
ssize_t subprocess::pipe_write | ( | PipeHandle | , |
const void * | buffer, | ||
size_t | size | ||
) |
CompletedProcess subprocess::run | ( | CommandLine | command, |
RunOptions | options = {} |
||
) |
Run a command blocking until completion.
see subprocess::run(Popen&,bool) for more details about exceptions.
command | The command to run. First element must be executable. |
options | Options specifying how to run the command. |
CompletedProcess subprocess::run | ( | Popen & | popen, |
bool | check = false |
||
) |
If you have stuff to pipe this will run the process to completion.
This will read stdout/stderr if they exist and store in cout, cerr.
popen | An already running process. |
check | if true will throw CalledProcessException if process returns non-zero exit code. |
CalledProcessError | if check=true and process returned non-zero error code |
TimeoutExpired | if subprocess does not finish by timeout seconds. |
OSError | for os level exceptions such as failing OS commands. |
std::runtime_error | for various errors. |
std::invalid_argument | if argument is invalid for current usage. |
std::domain_error | when arguments don't make sense working together |
void subprocess::setcwd | ( | const std::string & | path | ) |
Sets the current working directory of process.
double subprocess::sleep_seconds | ( | double | seconds | ) |
Sleep for a number of seconds.
seconds | The number of seconds to sleep for. |
Environ subprocess::cenv |
Makes it easy to get/set environment variables. e.g. like so subprocess::cenv["VAR"] = "Value";
const PipeHandle subprocess::kBadPipeValue = (PipeHandle)-1 |
The value representing an invalid pipe
|
constexpr |
The value representing an invalid exit code possible for a process.
|
constexpr |
|
constexpr |
The path seperator for PATH environment variable.
|
constexpr |
|
constexpr |
|
constexpr |