1 #include "hmbdc/Copyright.hpp" 4 #include "hmbdc/Exception.hpp" 15 namespace hmbdc {
namespace os {
29 ,
bool captureStderr =
false);
57 bool const writeStatus =
true;
58 bool const readStatus =
true;
59 bool const errStatus =
true;
66 auto s = write(
exeStdinFd, input, strlen(input));
67 if ((
size_t)s != strlen(input)) {
68 const_cast<bool&
>(writeStatus) =
false;
73 ExecutionIo& operator << (std::string
const& input) {
74 auto s = write(
exeStdinFd, input.c_str(), input.size());
75 if ((
size_t)s != input.size()) {
76 const_cast<bool&
>(writeStatus) =
false;
82 return read(
exeStdoutFd, output, buf_,
sizeof(buf_), bufLen_, readStatus);
86 return read(
exeStderrFd, output, errbuf_,
sizeof(errbuf_), errbufLen_, errStatus);
95 ExecutionIo& read(
int fd, std::string& output,
char* buf,
size_t bufCap,
size_t& bufLen,
const bool& status) {
97 auto l = std::find(buf, buf + bufLen,
'\n');
99 if (l == buf + bufCap) l--;
100 if (l != buf + bufLen) {
101 output = std::string(buf, l + 1);
102 bufLen -= output.size();
103 memmove(buf, l + 1, bufLen);
106 auto s = ::read(fd, buf + bufLen, bufCap - bufLen);
110 output = std::string(buf, bufLen);
112 const_cast<bool&
>(status) =
false;
121 size_t errbufLen_ = 0;
ExecutionIo(const char *const argv[], bool captureStderr=false)
execute a program as a child and capture its stdin stdout and/or stderr
pid_t exePid
process id of the started child process
Definition: ExecutionIo.hpp:54
int exeStderrFd
if captured, child stderr fd, use read on it - not write
Definition: ExecutionIo.hpp:49
int exeStdoutFd
child stdout fd, use read on it - not write
Definition: ExecutionIo.hpp:43
execute a program as a child and capture its stdin stdout and/or stderr
Definition: ExecutionIo.hpp:20
int exeStdinFd
child stdin fd, use write on it - not read
Definition: ExecutionIo.hpp:37