19#ifndef REDI_PSTREAM_H_SEEN
20#define REDI_PSTREAM_H_SEEN
36# include <sys/filio.h>
41#if REDI_EVISCERATE_PSTREAMS
47#define PSTREAMS_VERSION 0x0103
68 typedef std::ios_base::openmode
pmode;
87#if __cplusplus >= 201103L
89 using stringable =
decltype((void)std::string(std::declval<const T&>()));
94 template <
typename CharT,
typename Traits = std::
char_traits<CharT> >
96 :
public std::basic_streambuf<CharT, Traits>
101 typedef CharT char_type;
102 typedef Traits traits_type;
103 typedef typename traits_type::int_type int_type;
104 typedef typename traits_type::off_type off_type;
105 typedef typename traits_type::pos_type pos_type;
120#if __cplusplus >= 201103L
165#if REDI_EVISCERATE_PSTREAMS
168 fopen(FILE*& in, FILE*& out, FILE*& err);
198 xsputn(
const char_type* s, std::streamsize n);
202 write(
const char_type* s, std::streamsize n);
206 read(char_type* s, std::streamsize n);
237 create_buffers(
pmode mode);
240 destroy_buffers(
pmode mode);
257#if __cplusplus >= 201103L
258 using basic_streambuf = std::basic_streambuf<char_type, traits_type>;
271 char_type* rbuffer_[2];
272 char_type* rbufstate_[3];
280 template <
typename CharT,
typename Traits = std::
char_traits<CharT> >
282 :
virtual public std::basic_ios<CharT, Traits>
287 typedef std::basic_ios<CharT, Traits> ios_type;
305#if __cplusplus >= 201103L
308 ,
buf_(std::move(rhs.buf_))
317 buf_ = std::move(rhs.buf_);
336 do_open(
const std::string& file,
const argv_type& argv, pmode mode);
356#if REDI_EVISCERATE_PSTREAMS
359 fopen(FILE*& in, FILE*& out, FILE*& err);
378 template <
typename CharT,
typename Traits = std::
char_traits<CharT> >
380 :
public std::basic_istream<CharT, Traits>
384 typedef std::basic_istream<CharT, Traits> istream_type;
399 typedef typename pbase_type::pmode
pmode;
421 : istream_type(NULL),
pbase_type(cmd, readable(mode))
438 : istream_type(NULL),
pbase_type(file, argv, readable(mode))
453 : istream_type(NULL),
pbase_type(argv.at(0), argv, readable(mode))
456#if __cplusplus >= 201103L
457 template<
typename T,
typename =
stringable<T>>
464 : istream_type(std::move(rhs))
465 , pbase_type(std::move(rhs))
471 istream_type::operator=(std::move(rhs));
472 pbase_type::operator=(std::move(rhs));
479 istream_type::swap(rhs);
480 pbase_type::swap(rhs);
504 this->
do_open(cmd, readable(mode));
522 this->
do_open(file, argv, readable(mode));
558 template <
typename CharT,
typename Traits = std::
char_traits<CharT> >
560 :
public std::basic_ostream<CharT, Traits>
564 typedef std::basic_ostream<CharT, Traits> ostream_type;
571 typedef typename pbase_type::pmode
pmode;
628#if __cplusplus >= 201103L
636 template<
typename T,
typename =
stringable<T>>
643 : ostream_type(std::move(rhs))
644 , pbase_type(std::move(rhs))
650 ostream_type::operator=(std::move(rhs));
651 pbase_type::operator=(std::move(rhs));
658 ostream_type::swap(rhs);
659 pbase_type::swap(rhs);
718 template <
typename CharT,
typename Traits = std::
char_traits<CharT> >
720 :
public std::basic_iostream<CharT, Traits>
724 typedef std::basic_iostream<CharT, Traits> iostream_type;
731 typedef typename pbase_type::pmode
pmode;
770 : iostream_type(NULL),
pbase_type(file, argv, mode)
785 : iostream_type(NULL),
pbase_type(argv.at(0), argv, mode)
788#if __cplusplus >= 201103L
796 template<
typename T,
typename =
stringable<T>>
803 : iostream_type(std::move(rhs))
804 , pbase_type(std::move(rhs))
810 iostream_type::operator=(std::move(rhs));
811 pbase_type::operator=(std::move(rhs));
818 iostream_type::swap(rhs);
819 pbase_type::swap(rhs);
860 this->
do_open(file, argv, mode);
908 template <
typename CharT,
typename Traits = std::
char_traits<CharT> >
910 :
public std::basic_ostream<CharT, Traits>
911 ,
private std::basic_istream<CharT, Traits>
915 typedef std::basic_ostream<CharT, Traits> ostream_type;
916 typedef std::basic_istream<CharT, Traits> istream_type;
923 typedef typename pbase_type::pmode
pmode;
930 : ostream_type(NULL), istream_type(NULL),
pbase_type()
945 : ostream_type(NULL) , istream_type(NULL) ,
pbase_type(cmd, mode)
962 : ostream_type(NULL), istream_type(NULL),
pbase_type(file, argv, mode)
977 : ostream_type(NULL), istream_type(NULL)
981#if __cplusplus >= 201103L
989 template<
typename T,
typename =
stringable<T>>
999 : iostream_type(std::move(rhs))
1000 , pbase_type(std::move(rhs))
1006 iostream_type::operator=(std::move(rhs));
1007 pbase_type::operator=(std::move(rhs));
1014 iostream_type::swap(rhs);
1015 pbase_type::swap(rhs);
1053 this->
do_open(file, argv, mode);
1106 template <
typename C,
typename T>
1107 inline std::basic_ostream<C,T>&
1111 if (pstreambuf_type* p =
dynamic_cast<pstreambuf_type*
>(s.rdbuf()))
1128 template <
typename C,
typename T>
1140 rpipe_[rsrc_out] = rpipe_[rsrc_err] = -1;
1151 template <
typename C,
typename T>
1163 rpipe_[rsrc_out] = rpipe_[rsrc_err] = -1;
1176 template <
typename C,
typename T>
1190 rpipe_[rsrc_out] = rpipe_[rsrc_err] = -1;
1191 open(file, argv, mode);
1198 template <
typename C,
typename T>
1205#if __cplusplus >= 201103L
1209 template <
typename C,
typename T>
1212 : basic_streambuf(
static_cast<const basic_streambuf&
>(rhs))
1214 , wpipe_(rhs.wpipe_)
1215 , rpipe_{rhs.rpipe_[0], rhs.rpipe_[1]}
1216 , wbuffer_(rhs.wbuffer_)
1217 , rbuffer_{rhs.rbuffer_[0], rhs.rbuffer_[1]}
1218 , rbufstate_{rhs.rbufstate_[0], rhs.rbufstate_[1], rhs.rbufstate_[2]}
1220 , status_(rhs.status_)
1221 , error_(rhs.error_)
1225 rhs.rpipe_[0] = rhs.rpipe_[1] = -1;
1226 rhs.wbuffer_ =
nullptr;
1227 rhs.rbuffer_[0] = rhs.rbuffer_[1] =
nullptr;
1228 rhs.rbufstate_[0] = rhs.rbufstate_[1] = rhs.rbufstate_[2] =
nullptr;
1229 rhs.rsrc_ = rsrc_out;
1232 rhs.setg(
nullptr,
nullptr,
nullptr);
1233 rhs.setp(
nullptr,
nullptr);
1236 template <
typename C,
typename T>
1237 inline basic_pstreambuf<C,T>&
1238 basic_pstreambuf<C,T>::operator=( basic_pstreambuf&& rhs )
noexcept
1241 basic_streambuf::operator=(
static_cast<const basic_streambuf&
>(rhs));
1246 template <
typename C,
typename T>
1248 basic_pstreambuf<C,T>::swap( basic_pstreambuf& rhs )
noexcept
1250 basic_streambuf::swap(
static_cast<basic_streambuf&
>(rhs));
1251 std::swap(ppid_, rhs.ppid_);
1252 std::swap(wpipe_, rhs.wpipe_);
1253 std::swap(rpipe_, rhs.rpipe_);
1254 std::swap(wbuffer_, rhs.wbuffer_);
1255 std::swap(rbuffer_, rhs.rbuffer_);
1256 std::swap(rbufstate_, rhs.rbufstate_);
1257 std::swap(rsrc_, rhs.rsrc_);
1258 std::swap(status_, rhs.status_);
1259 std::swap(error_, rhs.error_);
1290 template <
typename C,
typename T>
1291 basic_pstreambuf<C,T>*
1294 const char * shell_path =
"/bin/sh";
1296 const std::string argv[] = {
"sh",
"-c", command };
1297 return this->open(shell_path,
argv_type(argv, argv+3), mode);
1307 ::execl(shell_path,
"sh",
"-c", command.c_str(), (
char*)NULL);
1322 create_buffers(mode);
1341 if (fd >= 0 && ::close(fd) == 0)
1359 for (std::size_t i = 0; i < N; ++i)
1392 template <
typename C,
typename T>
1406 fd_type ck_exec[] = { -1, -1 };
1407 if (-1 == ::pipe(ck_exec)
1408 || -1 == ::fcntl(ck_exec[RD], F_SETFD, FD_CLOEXEC)
1409 || -1 == ::fcntl(ck_exec[WR], F_SETFD, FD_CLOEXEC))
1412 close_fd_array(ck_exec);
1421 char** arg_v =
new char*[argv.size()+1];
1422 for (std::size_t i = 0; i < argv.size(); ++i)
1424 const std::string& src = argv[i];
1425 char*& dest = arg_v[i];
1426 dest =
new char[src.size()+1];
1427 dest[ src.copy(dest, src.size()) ] =
'\0';
1429 arg_v[argv.size()] = NULL;
1431 ::execvp(file.c_str(), arg_v);
1438 while (::write(ck_exec[WR], &error_,
sizeof(error_)) == -1
1442 ::close(ck_exec[WR]);
1443 ::close(ck_exec[RD]);
1451 close_fd_array(ck_exec);
1458 ::close(ck_exec[WR]);
1459 switch (::read(ck_exec[RD], &error_,
sizeof(error_)))
1463 create_buffers(mode);
1475 ::close(ck_exec[RD]);
1498 template <
typename C,
typename T>
1507 fd_type fd[] = { -1, -1, -1, -1, -1, -1 };
1519 if (!error_ && mode&pstdin && ::pipe(pin))
1522 if (!error_ && mode&pstdout && ::pipe(pout))
1525 if (!error_ && mode&pstderr && ::pipe(perr))
1543 ::dup2(pin[RD], STDIN_FILENO);
1549 ::dup2(pout[WR], STDOUT_FILENO);
1555 ::dup2(perr[WR], STDERR_FILENO);
1559#ifdef _POSIX_JOB_CONTROL
1587 rpipe_[rsrc_out] = pout[RD];
1592 rpipe_[rsrc_err] = perr[RD];
1615 template <
typename C,
typename T>
1619 const bool running = is_open();
1626 destroy_buffers(pstdin|pstdout|pstderr);
1630 close_fd_array(rpipe_);
1635 }
while (wait() == -1 && error() == EINTR);
1637 return running ? this : NULL;
1644 template <
typename C,
typename T>
1645#if __cplusplus >= 201402L && __has_cpp_attribute(deprecated)
1648 __attribute__((deprecated))
1653 rpipe_[rsrc_out] = rpipe_[rsrc_err] = -1;
1654 rbuffer_[rsrc_out] = rbuffer_[rsrc_err] = NULL;
1655 rbufstate_[0] = rbufstate_[1] = rbufstate_[2] = NULL;
1658 template <
typename C,
typename T>
1660 basic_pstreambuf<C,T>::create_buffers(pmode mode)
1665 wbuffer_ =
new char_type[bufsz];
1666 this->setp(wbuffer_, wbuffer_ + bufsz);
1670 delete[] rbuffer_[rsrc_out];
1671 rbuffer_[rsrc_out] =
new char_type[bufsz];
1673 this->setg(rbuffer_[rsrc_out] + pbsz, rbuffer_[rsrc_out] + pbsz,
1674 rbuffer_[rsrc_out] + pbsz);
1678 delete[] rbuffer_[rsrc_err];
1679 rbuffer_[rsrc_err] =
new char_type[bufsz];
1680 if (!(mode & pstdout))
1683 this->setg(rbuffer_[rsrc_err] + pbsz, rbuffer_[rsrc_err] + pbsz,
1684 rbuffer_[rsrc_err] + pbsz);
1689 template <
typename C,
typename T>
1691 basic_pstreambuf<C,T>::destroy_buffers(pmode mode)
1695 this->setp(NULL, NULL);
1701 if (rsrc_ == rsrc_out)
1702 this->setg(NULL, NULL, NULL);
1703 delete[] rbuffer_[rsrc_out];
1704 rbuffer_[rsrc_out] = NULL;
1708 if (rsrc_ == rsrc_err)
1709 this->setg(NULL, NULL, NULL);
1710 delete[] rbuffer_[rsrc_err];
1711 rbuffer_[rsrc_err] = NULL;
1715 template <
typename C,
typename T>
1717 basic_pstreambuf<C,T>::switch_read_buffer(buf_read_src src)
1721 char_type* tmpbufstate[] = {this->eback(), this->gptr(), this->egptr()};
1722 this->setg(rbufstate_[0], rbufstate_[1], rbufstate_[2]);
1723 for (std::size_t i = 0; i < 3; ++i)
1724 rbufstate_[i] = tmpbufstate[i];
1746 template <
typename C,
typename T>
1750 int child_exited = -1;
1754 switch(::waitpid(ppid_, &exit_status, nohang ? WNOHANG : 0))
1766 status_ = exit_status;
1769 destroy_buffers(pstdin);
1776 return child_exited;
1789 template <
typename C,
typename T>
1796 if (::kill(ppid_, signal))
1802 if (signal==SIGTERM || signal==SIGKILL)
1824 template <
typename C,
typename T>
1829#ifdef _POSIX_JOB_CONTROL
1832 pid_t pgid = ::getpgid(ppid_);
1835 else if (pgid == ::getpgrp())
1837 else if (::killpg(pgid, signal))
1855 template <
typename C,
typename T>
1859 return ppid_ == 0 || wait(
true)==1;
1868 template <
typename C,
typename T>
1878 template <
typename C,
typename T>
1889 template <
typename C,
typename T>
1894 destroy_buffers(pstdin);
1908 template <
typename C,
typename T>
1923 template <
typename C,
typename T>
1930 switch_read_buffer(src);
1946 template <
typename C,
typename T>
1947 typename basic_pstreambuf<C,T>::int_type
1950 if (!empty_buffer())
1951 return traits_type::eof();
1952 else if (!traits_type::eq_int_type(c, traits_type::eof()))
1953 return this->sputc(c);
1955 return traits_type::not_eof(c);
1959 template <
typename C,
typename T>
1963 return !exited() && empty_buffer() ? 0 : -1;
1971 template <
typename C,
typename T>
1975 std::streamsize done = 0;
1978 if (std::streamsize nbuf = this->epptr() - this->pptr())
1980 nbuf = std::min(nbuf, n - done);
1981 traits_type::copy(this->pptr(), s + done, nbuf);
1985 else if (!empty_buffer())
1994 template <
typename C,
typename T>
1998 const std::streamsize count = this->pptr() - this->pbase();
2001 const std::streamsize written = this->write(this->wbuffer_, count);
2004 if (
const std::streamsize unwritten = count - written)
2005 traits_type::move(this->pbase(), this->pbase()+written, unwritten);
2006 this->pbump(-written);
2020 template <
typename C,
typename T>
2021 typename basic_pstreambuf<C,T>::int_type
2024 if (this->gptr() < this->egptr() || fill_buffer())
2025 return traits_type::to_int_type(*this->gptr());
2027 return traits_type::eof();
2038 template <
typename C,
typename T>
2039 typename basic_pstreambuf<C,T>::int_type
2042 if (this->gptr() != this->eback())
2045 if (!traits_type::eq_int_type(c, traits_type::eof()))
2046 *this->gptr() = traits_type::to_char_type(c);
2047 return traits_type::not_eof(c);
2050 return traits_type::eof();
2053 template <
typename C,
typename T>
2058 if (
sizeof(char_type) == 1)
2059 avail = fill_buffer(
true) ? this->egptr() - this->gptr() : -1;
2063 if (::ioctl(rpipe(), FIONREAD, &avail) == -1)
2066 avail /=
sizeof(char_type);
2069 return std::streamsize(avail);
2075 template <
typename C,
typename T>
2079 const std::streamsize pb1 = this->gptr() - this->eback();
2080 const std::streamsize pb2 = pbsz;
2081 const std::streamsize npb = std::min(pb1, pb2);
2083 char_type*
const rbuf = rbuffer();
2086 traits_type::move(rbuf + pbsz - npb, this->gptr() - npb, npb);
2088 std::streamsize rc = -1;
2092 const int flags = ::fcntl(rpipe(), F_GETFL);
2095 const bool blocking = !(flags & O_NONBLOCK);
2097 ::fcntl(rpipe(), F_SETFL, flags | O_NONBLOCK);
2100 rc = read(rbuf + pbsz, bufsz - pbsz);
2102 if (rc == -1 && error_ == EAGAIN)
2108 ::fcntl(rpipe(), F_SETFL, flags);
2112 rc = read(rbuf + pbsz, bufsz - pbsz);
2114 if (rc > 0 || (rc == 0 && non_blocking))
2116 this->setg( rbuf + pbsz - npb,
2123 this->setg(NULL, NULL, NULL);
2135 template <
typename C,
typename T>
2136 inline std::streamsize
2139 std::streamsize nwritten = 0;
2142 nwritten = ::write(wpipe(), s, n *
sizeof(char_type));
2146 nwritten /=
sizeof(char_type);
2158 template <
typename C,
typename T>
2159 inline std::streamsize
2162 std::streamsize nread = 0;
2165 nread = ::read(rpipe(), s, n *
sizeof(char_type));
2169 nread /=
sizeof(char_type);
2175 template <
typename C,
typename T>
2183 template <
typename C,
typename T>
2187 return rpipe_[rsrc_];
2191 template <
typename C,
typename T>
2195 return rpipe_[which];
2199 template <
typename C,
typename T>
2200 inline typename basic_pstreambuf<C,T>::char_type*
2203 return rbuffer_[rsrc_];
2220 template <
typename C,
typename T>
2223 : std::basic_ios<C,T>(NULL)
2227 this->std::basic_ios<C,T>::rdbuf(&
buf_);
2238 template <
typename C,
typename T>
2241 : std::basic_ios<C,T>(NULL)
2245 this->std::basic_ios<C,T>::rdbuf(&
buf_);
2258 template <
typename C,
typename T>
2261 const argv_type& argv,
2263 : std::basic_ios<C,T>(NULL)
2267 this->std::basic_ios<C,T>::rdbuf(&
buf_);
2280 template <
typename C,
typename T>
2294 template <
typename C,
typename T>
2298 if (!buf_.open((command_=cmd), mode))
2299 this->setstate(std::ios_base::failbit);
2311 template <
typename C,
typename T>
2314 const argv_type& argv,
2317 if (!buf_.open((command_=file), argv, mode))
2318 this->setstate(std::ios_base::failbit);
2323 template <
typename C,
typename T>
2328 this->setstate(std::ios_base::failbit);
2329 return buf_.status();
2336 template <
typename C,
typename T>
2340 return buf_.is_open();
2344 template <
typename C,
typename T>
2345 inline const std::string&
2353 template <
typename C,
typename T>
2361#if REDI_EVISCERATE_PSTREAMS
2394 template <
typename C,
typename T>
2398 in = out = err = NULL;
2399 std::size_t open_files = 0;
2402 if ((in = ::fdopen(wpipe(),
"w")))
2404 open_files |= pstdin;
2407 if (rpipe(rsrc_out) > -1)
2409 if ((out = ::fdopen(rpipe(rsrc_out),
"r")))
2411 open_files |= pstdout;
2414 if (rpipe(rsrc_err) > -1)
2416 if ((err = ::fdopen(rpipe(rsrc_err),
"r")))
2418 open_files |= pstderr;
2434 template <
typename C,
typename T>
2436 pstream_common<C,T>::fopen(FILE*& fin, FILE*& fout, FILE*& ferr)
2438 return buf_.fopen(fin, fout, ferr);
Class template for Input PStreams.
Definition pstream.h:383
basic_ipstream(const std::string &file, const argv_type &argv, pmode mode=pstdout)
Constructor that initialises the stream by starting a process.
Definition pstream.h:435
basic_ipstream(const argv_type &argv, pmode mode=pstdout)
Constructor that initialises the stream by starting a process.
Definition pstream.h:452
basic_ipstream()
Default constructor, creates an uninitialised stream.
Definition pstream.h:405
~basic_ipstream()
Destructor.
Definition pstream.h:489
void open(const std::string &file, const argv_type &argv, pmode mode=pstdout)
Start a process.
Definition pstream.h:518
pbase_type::argv_type argv_type
Type used to hold the arguments for a command.
Definition pstream.h:402
basic_ipstream & err()
Set streambuf to read from process' stderr.
Definition pstream.h:541
void open(const std::string &cmd, pmode mode=pstdout)
Start a process.
Definition pstream.h:502
basic_ipstream & out()
Set streambuf to read from process' stdout.
Definition pstream.h:530
basic_ipstream(const std::string &cmd, pmode mode=pstdout)
Constructor that initialises the stream by starting a process.
Definition pstream.h:420
pbase_type::pmode pmode
Type used to specify how to connect to the process.
Definition pstream.h:399
Class template for Output PStreams.
Definition pstream.h:563
void open(const std::string &file, const argv_type &argv, pmode mode=pstdin)
Start a process.
Definition pstream.h:696
basic_opstream(const argv_type &argv, pmode mode=pstdin)
Constructor that initialises the stream by starting a process.
Definition pstream.h:624
basic_opstream(const std::string &file, const argv_type &argv, pmode mode=pstdin)
Constructor that initialises the stream by starting a process.
Definition pstream.h:607
basic_opstream()
Default constructor, creates an uninitialised stream.
Definition pstream.h:577
pbase_type::pmode pmode
Type used to specify how to connect to the process.
Definition pstream.h:571
pbase_type::argv_type argv_type
Type used to hold the arguments for a command.
Definition pstream.h:574
~basic_opstream()
Destructor.
Definition pstream.h:668
basic_opstream(const std::string &cmd, pmode mode=pstdin)
Constructor that initialises the stream by starting a process.
Definition pstream.h:592
void open(const std::string &cmd, pmode mode=pstdin)
Start a process.
Definition pstream.h:680
std::basic_ostream< C, T > & peof(std::basic_ostream< C, T > &s)
Manipulator to close the pipe connected to the process' stdin.
Definition pstream.h:1108
Class template for Bidirectional PStreams.
Definition pstream.h:723
basic_pstream(const std::string &cmd, pmode mode=pstdout|pstdin)
Constructor that initialises the stream by starting a process.
Definition pstream.h:752
void open(const std::string &cmd, pmode mode=pstdout|pstdin)
Start a process.
Definition pstream.h:840
basic_pstream()
Default constructor, creates an uninitialised stream.
Definition pstream.h:737
~basic_pstream()
Destructor.
Definition pstream.h:828
basic_pstream(const argv_type &argv, pmode mode=pstdout|pstdin)
Constructor that initialises the stream by starting a process.
Definition pstream.h:784
void open(const std::string &file, const argv_type &argv, pmode mode=pstdout|pstdin)
Start a process.
Definition pstream.h:856
basic_pstream & out()
Set streambuf to read from process' stdout.
Definition pstream.h:868
basic_pstream(const std::string &file, const argv_type &argv, pmode mode=pstdout|pstdin)
Constructor that initialises the stream by starting a process.
Definition pstream.h:767
pbase_type::argv_type argv_type
Type used to hold the arguments for a command.
Definition pstream.h:734
basic_pstream & err()
Set streambuf to read from process' stderr.
Definition pstream.h:879
pbase_type::pmode pmode
Type used to specify how to connect to the process.
Definition pstream.h:731
Class template for stream buffer.
Definition pstream.h:98
fd_type & rpipe(buf_read_src which)
Return the file descriptor for the specified input pipe.
Definition pstream.h:2193
basic_pstreambuf * killpg(int signal=SIGTERM)
Send a signal to the process' process group.
Definition pstream.h:1826
void close_fd(pstreams::fd_type &fd)
Helper function to close a file descriptor.
Definition pstream.h:1339
int status() const
Return the exit status of the process.
Definition pstream.h:1870
basic_pstreambuf * open(const std::string &file, const argv_type &argv, pmode mode)
Initialise the stream buffer with file and argv.
Definition pstream.h:1394
std::streamsize xsputn(const char_type *s, std::streamsize n)
Insert multiple characters into the pipe.
Definition pstream.h:1973
std::streamsize read(char_type *s, std::streamsize n)
Extract a sequence of characters from the pipe.
Definition pstream.h:2160
std::streamsize write(const char_type *s, std::streamsize n)
Insert a sequence of characters into the pipe.
Definition pstream.h:2137
~basic_pstreambuf()
Destructor.
Definition pstream.h:1200
int_type overflow(int_type c)
Transfer characters to the pipe when character buffer overflows.
Definition pstream.h:1948
int sync()
Write any buffered characters to the stream.
Definition pstream.h:1961
basic_pstreambuf * kill(int signal=SIGTERM)
Send a signal to the process.
Definition pstream.h:1791
void peof()
Close the pipe connected to the process' stdin.
Definition pstream.h:1891
pid_t fork(pmode mode)
Initialise pipes and fork process.
Definition pstream.h:1500
basic_pstreambuf * close()
Close the stream buffer and wait for the process to exit.
Definition pstream.h:1617
basic_pstreambuf(const std::string &file, const argv_type &argv, pmode mode)
Constructor that initialises the buffer with file and argv.
Definition pstream.h:1178
int error() const
Return the error number (errno) for the most recent failed operation.
Definition pstream.h:1880
buf_read_src
Enumerated type to indicate whether stdout or stderr is to be read.
Definition pstream.h:214
void close_fd_array(pstreams::fd_type(&fds)[N])
Helper function to close an array of file descriptors.
Definition pstream.h:1357
fd_type & wpipe()
Return the file descriptor for the output pipe.
Definition pstream.h:2177
bool exited()
Report whether the process has exited.
Definition pstream.h:1857
bool empty_buffer()
Writes buffered characters to the process' stdin pipe.
Definition pstream.h:1996
fd_type fd_t
Definition pstream.h:107
char_type * rbuffer()
Return the active input buffer.
Definition pstream.h:2201
std::streamsize showmanyc()
Report how many characters can be read from active input without blocking.
Definition pstream.h:2055
bool read_err(bool readerr=true)
Change active input source.
Definition pstream.h:1925
bool fill_buffer(bool non_blocking=false)
Definition pstream.h:2077
basic_pstreambuf * open(const std::string &cmd, pmode mode)
Initialise the stream buffer with cmd.
Definition pstream.h:1292
int_type pbackfail(int_type c=traits_type::eof())
Make a character available to be returned by the next extraction.
Definition pstream.h:2040
fd_type & rpipe()
Return the file descriptor for the active input pipe.
Definition pstream.h:2185
basic_pstreambuf()
Default constructor.
Definition pstream.h:1130
bool is_open() const
Report whether the stream buffer has been initialised.
Definition pstream.h:1910
int_type underflow()
Transfer characters from the pipe when the character buffer is empty.
Definition pstream.h:2022
int wait(bool nohang=false)
Wait for the child process to exit.
Definition pstream.h:1748
basic_pstreambuf(const std::string &cmd, pmode mode)
Constructor that initialises the buffer with cmd.
Definition pstream.h:1153
Class template for Restricted PStreams.
Definition pstream.h:914
istream_type & err()
Obtain a reference to the istream that reads the process' stderr.
Definition pstream.h:1074
~basic_rpstream()
Destructor.
Definition pstream.h:1021
basic_rpstream()
Default constructor, creates an uninitialised stream.
Definition pstream.h:929
basic_rpstream(const std::string &cmd, pmode mode=pstdout|pstdin)
Constructor that initialises the stream by starting a process.
Definition pstream.h:944
void open(const std::string &file, const argv_type &argv, pmode mode=pstdout|pstdin)
Start a process.
Definition pstream.h:1049
void open(const std::string &cmd, pmode mode=pstdout|pstdin)
Start a process.
Definition pstream.h:1033
pbase_type::argv_type argv_type
Type used to hold the arguments for a command.
Definition pstream.h:926
pbase_type::pmode pmode
Type used to specify how to connect to the process.
Definition pstream.h:923
basic_rpstream(const argv_type &argv, pmode mode=pstdout|pstdin)
Constructor that initialises the stream by starting a process.
Definition pstream.h:976
basic_rpstream(const std::string &file, const argv_type &argv, pmode mode=pstdout|pstdin)
Constructor that initialises the stream by starting a process.
Definition pstream.h:959
istream_type & out()
Obtain a reference to the istream that reads the process' stdout.
Definition pstream.h:1062
Class template for common base class.
Definition pstream.h:284
pstream_common()
Default constructor.
Definition pstream.h:2222
void do_open(const std::string &cmd, pmode mode)
Start a process.
Definition pstream.h:2296
void do_open(const std::string &file, const argv_type &argv, pmode mode)
Start a process.
Definition pstream.h:2313
pstream_common(const std::string &file, const argv_type &argv, pmode mode)
Constructor that initialises the stream by starting a process.
Definition pstream.h:2260
pstream_common(const std::string &cmd, pmode mode)
Constructor that initialises the stream by starting a process.
Definition pstream.h:2240
streambuf_type * rdbuf() const
Return a pointer to the stream buffer.
Definition pstream.h:2355
streambuf_type buf_
The stream buffer.
Definition pstream.h:364
int close()
Definition pstream.h:2325
virtual ~pstream_common()=0
Pure virtual destructor.
Definition pstream.h:2282
bool is_open() const
Report whether the stream's buffer has been initialised.
Definition pstream.h:2338
std::string command_
The command used to start the process.
Definition pstream.h:363
const std::string & command() const
Return the command used to initialise the stream.
Definition pstream.h:2346
All PStreams classes are declared in namespace redi.
basic_pstreambuf< char > pstreambuf
Type definition for common template specialisation.
Definition pstream.h:1083
basic_ipstream< char > ipstream
Type definition for common template specialisation.
Definition pstream.h:1085
basic_rpstream< char > rpstream
Type definition for common template specialisation.
Definition pstream.h:1091
basic_opstream< char > opstream
Type definition for common template specialisation.
Definition pstream.h:1087
basic_pstream< char > pstream
Type definition for common template specialisation.
Definition pstream.h:1089
Common base class providing constants and typenames.
Definition pstream.h:66
std::ios_base::openmode pmode
Type used to specify how to connect to the process.
Definition pstream.h:68
static const pmode pstderr
Read from stderr.
Definition pstream.h:78
static const pmode pstdin
Write to stdin.
Definition pstream.h:76
static const pmode newpg
Create a new process group for the child process.
Definition pstream.h:81
static const pmode pstdout
Read from stdout.
Definition pstream.h:77
int fd_type
Type used for file descriptors.
Definition pstream.h:74
std::vector< std::string > argv_type
Type used to hold the arguments for a command.
Definition pstream.h:71