7 #include <sys/select.h> 19 #ifndef WIBBLE_SYS_PIPE_H 20 #define WIBBLE_SYS_PIPE_H 37 Writer() : fd( -1 ), close( false ), running( false ) {}
45 wrote =
::write( fd, data.c_str(), data.length() );
47 data.erase( data.begin(), data.begin() + wrote );
77 void run(
int _fd, std::string what ) {
99 Pipe(
int p ) : fd( p ), _eof( false )
104 if ( fcntl( fd, F_SETFL, O_NONBLOCK ) == -1 )
108 Pipe() : fd( -1 ), _eof( false ) {}
112 writer.
run( fd, what );
136 return err == EAGAIN || err == EWOULDBLOCK;
138 return err == EAGAIN;
145 int r = ::read( fd, _buffer, 1023 );
146 if ( r == -1 && !
blocking( errno ) )
153 std::copy( _buffer, _buffer + r, std::back_inserter( buffer ) );
158 std::string line( buffer.begin(), buffer.end() );
166 std::find( buffer.begin(), buffer.end(),
'\n' );
167 while ( nl == buffer.end() ) {
170 nl = std::find( buffer.begin(), buffer.end(),
'\n' );
172 std::string line( buffer.begin(), nl );
174 if ( nl != buffer.end() )
176 buffer.erase( buffer.begin(), nl );
188 while ( buffer.empty() && !
eof() ) {
194 #pragma GCC diagnostic push 195 #pragma GCC diagnostic ignored "-Wold-style-cast" 197 select( fd + 1, &fds, 0, 0, 0 );
198 #pragma GCC diagnostic pop 212 return std::string( buffer.begin(), buffer.end() );
238 Pipe in( _in ), out( _out );
243 while ( !out.
eof() ) {
std::string run(std::string data)
Definition: pipe.h:226
Iterator< typename I::value_type > iterator(I i)
Definition: iterator.h:123
std::deque< char > Buffer
Definition: pipe.h:93
void start()
Start the thread.
Definition: thread.cpp:70
std::string data
Definition: pipe.h:32
bool _eof
Definition: pipe.h:96
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:77
#define assert_eq(x, y)
Definition: test.h:33
#define assert(x)
Definition: test.h:30
bool active()
Definition: pipe.h:126
Encapsulates a thread.
Definition: thread.h:83
bool closed
Definition: pipe.h:34
pthread mutex wrapper; WARNING: the class allows copying and assignment, but this is not always safe...
Definition: mutex.h:47
Writer()
Definition: pipe.h:37
bool done()
Definition: pipe.h:70
std::string nextLineBlocking()
Definition: pipe.h:204
void write(std::string what)
Definition: pipe.h:111
int readMore()
Definition: pipe.h:142
void wait()
Definition: pipe.h:182
std::string nextLine()
Definition: pipe.h:163
wibble::sys::Mutex mutex
Definition: pipe.h:35
Pipe(int p)
Definition: pipe.h:99
void * main()
Main thread function, executed in the new thread after creation.
Definition: pipe.h:39
pid_t fork()
For a subprocess to run proc.
PipeThrough(const std::string &_cmd)
Definition: pipe.h:224
void setupRedirects(int *stdinfd=0, int *stdoutfd=0, int *stderrfd=0)
Definition: childprocess.cpp:145
int fd
Definition: pipe.h:30
void close()
Definition: pipe.h:115
int fd
Definition: pipe.h:95
#define assert_neq(x, y)
Definition: test.h:36
bool close
Definition: pipe.h:31
std::string nextChunk()
Definition: pipe.h:157
bool running
Definition: pipe.h:33
Pipe()
Definition: pipe.h:108
bool eof()
Definition: pipe.h:130
bool valid()
Definition: pipe.h:122
static bool blocking(int err)
Definition: pipe.h:134
Buffer buffer
Definition: pipe.h:94
Base class for system exceptions.
Definition: exception.h:396
std::string cmd
Definition: pipe.h:222
Writer writer
Definition: pipe.h:97
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