8 #include <sys/select.h>
20 #ifndef WIBBLE_SYS_PIPE_H
21 #define WIBBLE_SYS_PIPE_H
38 Writer() : fd( -1 ), close( false ), running( false ) {}
46 wrote =
::write( fd, data.c_str(), data.length() );
48 data.erase( data.begin(), data.begin() + wrote );
78 void run(
int _fd, std::string what ) {
100 Pipe(
int p ) : fd( p ), _eof( false )
105 if ( fcntl( fd, F_SETFL, O_NONBLOCK ) == -1 )
109 Pipe() : fd( -1 ), _eof( false ) {}
113 writer.
run( fd, what );
137 return err == EAGAIN || err == EWOULDBLOCK;
139 return err == EAGAIN;
146 int r = ::read( fd, _buffer, 1023 );
147 if ( r == -1 && !
blocking( errno ) )
154 std::copy( _buffer, _buffer + r, std::back_inserter( buffer ) );
159 std::string line( buffer.begin(), buffer.end() );
167 std::find( buffer.begin(), buffer.end(),
'\n' );
168 while ( nl == buffer.end() ) {
171 nl = std::find( buffer.begin(), buffer.end(),
'\n' );
173 std::string line( buffer.begin(), nl );
175 if ( nl != buffer.end() )
177 buffer.erase( buffer.begin(), nl );
189 while ( buffer.empty() && !
eof() ) {
195 #pragma GCC diagnostic push
196 #pragma GCC diagnostic ignored "-Wold-style-cast"
198 select( fd + 1, &fds, 0, 0, 0 );
199 #pragma GCC diagnostic pop
213 return std::string( buffer.begin(), buffer.end() );
227 std::string
run( std::string data ) {
239 Pipe in( _in ), out( _out );
244 while ( !out.
eof() ) {
std::string run(std::string data)
Definition: pipe.h:227
Iterator< typename I::value_type > iterator(I i)
Definition: iterator.h:123
std::deque< char > Buffer
Definition: pipe.h:94
void start()
Start the thread.
Definition: thread.cpp:70
std::string data
Definition: pipe.h:33
bool _eof
Definition: pipe.h:97
Acquire a mutex lock, RAII-style.
Definition: mutex.h:200
void sleep(int secs)
Portable version of sleep.
Definition: thread.cpp:31
void run(int _fd, std::string what)
Definition: pipe.h:78
#define assert_eq(x, y)
Definition: test.h:33
#define assert(x)
Definition: test.h:30
bool active()
Definition: pipe.h:127
Encapsulates a thread.
Definition: thread.h:83
bool closed
Definition: pipe.h:35
pthread mutex wrapper; WARNING: the class allows copying and assignment, but this is not always safe...
Definition: mutex.h:47
Writer()
Definition: pipe.h:38
bool done()
Definition: pipe.h:71
std::string nextLineBlocking()
Definition: pipe.h:205
void write(std::string what)
Definition: pipe.h:112
int readMore()
Definition: pipe.h:143
void wait()
Definition: pipe.h:183
std::string nextLine()
Definition: pipe.h:164
wibble::sys::Mutex mutex
Definition: pipe.h:36
Pipe(int p)
Definition: pipe.h:100
void * main()
Main thread function, executed in the new thread after creation.
Definition: pipe.h:40
pid_t fork()
For a subprocess to run proc.
PipeThrough(const std::string &_cmd)
Definition: pipe.h:225
void setupRedirects(int *stdinfd=0, int *stdoutfd=0, int *stderrfd=0)
Definition: childprocess.cpp:145
int fd
Definition: pipe.h:31
void close()
Definition: pipe.h:116
int fd
Definition: pipe.h:96
#define assert_neq(x, y)
Definition: test.h:36
bool close
Definition: pipe.h:32
std::string nextChunk()
Definition: pipe.h:158
bool running
Definition: pipe.h:34
Pipe()
Definition: pipe.h:109
bool eof()
Definition: pipe.h:131
bool valid()
Definition: pipe.h:123
static bool blocking(int err)
Definition: pipe.h:135
Buffer buffer
Definition: pipe.h:95
Base class for system exceptions.
Definition: exception.h:396
std::string cmd
Definition: pipe.h:223
Writer writer
Definition: pipe.h:98
Execute a shell command using /bin/sh -c.
Definition: exec.h:97
Execute external commands, either forked as a ChildProcess or directly using exec().
Definition: exec.h:33