|
|
@@ -33,15 +33,13 @@ |
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#include "child.hpp"
|
|
|
|
#include "CodeDweller/child.hpp"
|
|
|
|
|
|
|
|
namespace CodeDweller {
|
|
|
|
|
|
|
|
Child::Child(std::vector<std::string> args, size_t bufSize) :
|
|
|
|
readStreambuf(bufSize),
|
|
|
|
writeStreambuf(bufSize),
|
|
|
|
reader(&readStreambuf),
|
|
|
|
writer(&writeStreambuf),
|
|
|
|
childStreambuf(bufSize),
|
|
|
|
childStream(&childStreambuf),
|
|
|
|
cmdArgs(args) {
|
|
|
|
|
|
|
|
init();
|
|
|
@@ -49,10 +47,8 @@ namespace CodeDweller { |
|
|
|
}
|
|
|
|
|
|
|
|
Child::Child(std::string childpath, size_t bufSize) :
|
|
|
|
readStreambuf(bufSize),
|
|
|
|
writeStreambuf(bufSize),
|
|
|
|
reader(&readStreambuf),
|
|
|
|
writer(&writeStreambuf),
|
|
|
|
childStreambuf(bufSize),
|
|
|
|
childStream(&childStreambuf),
|
|
|
|
cmdline(childpath) {
|
|
|
|
cmdArgs.push_back(childpath);
|
|
|
|
init();
|
|
|
@@ -78,7 +74,7 @@ namespace CodeDweller { |
|
|
|
size_t
|
|
|
|
Child::numBytesAvailable() const {
|
|
|
|
|
|
|
|
return readStreambuf.numBytesAvailable();
|
|
|
|
return childStreambuf.numBytesAvailable();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
@@ -196,8 +192,8 @@ namespace CodeDweller { |
|
|
|
|
|
|
|
// Provide the stream buffers with the handles for communicating
|
|
|
|
// with the child process.
|
|
|
|
readStreambuf.setInputHandle(childStdOutAtParent);
|
|
|
|
writeStreambuf.setOutputHandle(childStdInAtParent);
|
|
|
|
childStreambuf.setInputHandle(childStdOutAtParent);
|
|
|
|
childStreambuf.setOutputHandle(childStdInAtParent);
|
|
|
|
|
|
|
|
// Save the handles to the child process and its primary thread.
|
|
|
|
childProcess = processInfo.hProcess;
|
|
|
@@ -303,8 +299,8 @@ namespace CodeDweller { |
|
|
|
|
|
|
|
// Provide the stream buffers with the file descriptors for
|
|
|
|
// communicating with the child process.
|
|
|
|
readStreambuf.setInputFileDescriptor(childStdOutPipe[0]);
|
|
|
|
writeStreambuf.setOutputFileDescriptor(childStdInPipe[1]);
|
|
|
|
childStreambuf.setInputFileDescriptor(childStdOutPipe[0]);
|
|
|
|
childStreambuf.setOutputFileDescriptor(childStdInPipe[1]);
|
|
|
|
|
|
|
|
// Close the child's end of the pipes.
|
|
|
|
if ( (close(childStdInPipe[0]) != 0) ||
|
|
|
@@ -377,25 +373,21 @@ namespace CodeDweller { |
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
result = waitpid(childPid, &status, WNOHANG);
|
|
|
|
// std::cout << "isDone(). waitpid(" << childPid << ",...) returned " << result << std::endl; // DEBUG
|
|
|
|
if (-1 == result) {
|
|
|
|
throw std::runtime_error("Error checking status of child process: " +
|
|
|
|
getErrorText());
|
|
|
|
} else if (0 == result) {
|
|
|
|
|
|
|
|
// Child is still running.
|
|
|
|
// std::cout << "isDone(). Child is still running..." << std::endl; // DEBUG.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// std::cout << "isDone(). Child exited." << std::endl; // DEBUG.
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
|
|
|
|
// Child exited normally.
|
|
|
|
exitCode = WEXITSTATUS(status);
|
|
|
|
exitCodeObtainedFlag = true;
|
|
|
|
//std::cout << "isDone(). Child exited normally. Exit code: " << exitCode << std::endl; // DEBUG.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
@@ -461,31 +453,41 @@ namespace CodeDweller { |
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Child::ReadStreambuf::ReadStreambuf(std::size_t bufferSize) :
|
|
|
|
Child::ChildStreambuf::ChildStreambuf(std::size_t bufferSize) :
|
|
|
|
#ifdef _WIN32
|
|
|
|
inputHandle(0),
|
|
|
|
outputHandle(0),
|
|
|
|
#else
|
|
|
|
inputFileDescriptor(-1),
|
|
|
|
outputFileDescriptor(-1),
|
|
|
|
#endif
|
|
|
|
buffer(bufferSize + 1) {
|
|
|
|
readBuffer(bufferSize + 1),
|
|
|
|
writeBuffer(bufferSize + 1) {
|
|
|
|
|
|
|
|
char *end = &(buffer.front()) + buffer.size();
|
|
|
|
// Read buffer initialization.
|
|
|
|
char *end = &(readBuffer.front()) + readBuffer.size();
|
|
|
|
|
|
|
|
// Indicate to underflow that underflow has not been called.
|
|
|
|
setg(end, end, end);
|
|
|
|
|
|
|
|
// Write buffer initialization.
|
|
|
|
char *base = &(writeBuffer.front());
|
|
|
|
|
|
|
|
// Indicate to overflow that overflow has not been called.
|
|
|
|
setp(base, base + writeBuffer.size() - 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
void
|
|
|
|
Child::ReadStreambuf::setInputHandle(HANDLE inHandle) {
|
|
|
|
Child::ChildStreambuf::setInputHandle(HANDLE inHandle) {
|
|
|
|
|
|
|
|
inputHandle = inHandle;
|
|
|
|
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void
|
|
|
|
Child::ReadStreambuf::setInputFileDescriptor(int inFd) {
|
|
|
|
Child::ChildStreambuf::setInputFileDescriptor(int inFd) {
|
|
|
|
|
|
|
|
inputFileDescriptor = inFd;
|
|
|
|
|
|
|
@@ -493,7 +495,7 @@ namespace CodeDweller { |
|
|
|
#endif
|
|
|
|
|
|
|
|
size_t
|
|
|
|
Child::ReadStreambuf::numBytesAvailable() const {
|
|
|
|
Child::ChildStreambuf::numBytesAvailable() const {
|
|
|
|
|
|
|
|
size_t nBytesAvailable = egptr() - gptr();
|
|
|
|
|
|
|
@@ -536,7 +538,7 @@ namespace CodeDweller { |
|
|
|
}
|
|
|
|
|
|
|
|
std::streambuf::int_type
|
|
|
|
Child::ReadStreambuf::underflow() {
|
|
|
|
Child::ChildStreambuf::underflow() {
|
|
|
|
|
|
|
|
// Check for empty buffer.
|
|
|
|
if (gptr() < egptr()) {
|
|
|
@@ -547,7 +549,7 @@ namespace CodeDweller { |
|
|
|
}
|
|
|
|
|
|
|
|
// Need to fill the buffer.
|
|
|
|
char *base = &(buffer.front());
|
|
|
|
char *base = &(readBuffer.front());
|
|
|
|
char *start = base;
|
|
|
|
|
|
|
|
// Check whether this is the first fill.
|
|
|
@@ -564,7 +566,7 @@ namespace CodeDweller { |
|
|
|
|
|
|
|
if (!ReadFile(inputHandle,
|
|
|
|
start,
|
|
|
|
buffer.size() - (start - base),
|
|
|
|
readBuffer.size() - (start - base),
|
|
|
|
&nBytesRead,
|
|
|
|
NULL)) {
|
|
|
|
return traits_type::eof();
|
|
|
@@ -574,7 +576,7 @@ namespace CodeDweller { |
|
|
|
|
|
|
|
nBytesRead = read(inputFileDescriptor,
|
|
|
|
start,
|
|
|
|
buffer.size() - (start - base));
|
|
|
|
readBuffer.size() - (start - base));
|
|
|
|
if (-1 == nBytesRead) {
|
|
|
|
return traits_type::eof();
|
|
|
|
}
|
|
|
@@ -592,31 +594,16 @@ namespace CodeDweller { |
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Child::WriteStreambuf::WriteStreambuf(std::size_t bufferSize) :
|
|
|
|
#ifdef _WIN32
|
|
|
|
outputHandle(0),
|
|
|
|
#else
|
|
|
|
outputFileDescriptor(-1),
|
|
|
|
#endif
|
|
|
|
buffer(bufferSize + 1) {
|
|
|
|
|
|
|
|
char *base = &(buffer.front());
|
|
|
|
|
|
|
|
// Indicate to overflow that overflow has not been called.
|
|
|
|
setp(base, base + buffer.size() - 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
void
|
|
|
|
Child::WriteStreambuf::setOutputHandle(HANDLE outHandle) {
|
|
|
|
Child::ChildStreambuf::setOutputHandle(HANDLE outHandle) {
|
|
|
|
|
|
|
|
outputHandle = outHandle;
|
|
|
|
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void
|
|
|
|
Child::WriteStreambuf::setOutputFileDescriptor(int outFd) {
|
|
|
|
Child::ChildStreambuf::setOutputFileDescriptor(int outFd) {
|
|
|
|
|
|
|
|
outputFileDescriptor = outFd;
|
|
|
|
|
|
|
@@ -624,7 +611,7 @@ namespace CodeDweller { |
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
Child::WriteStreambuf::flushBuffer() {
|
|
|
|
Child::ChildStreambuf::flushBuffer() {
|
|
|
|
|
|
|
|
// Write.
|
|
|
|
std::ptrdiff_t nBytes = pptr() - pbase();
|
|
|
@@ -662,7 +649,7 @@ namespace CodeDweller { |
|
|
|
}
|
|
|
|
|
|
|
|
std::streambuf::int_type
|
|
|
|
Child::WriteStreambuf::overflow(int_type ch) {
|
|
|
|
Child::ChildStreambuf::overflow(int_type ch) {
|
|
|
|
|
|
|
|
// Check whether we're writing EOF.
|
|
|
|
if (traits_type::eof() != ch) {
|
|
|
@@ -684,7 +671,7 @@ namespace CodeDweller { |
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Child::WriteStreambuf::sync() {
|
|
|
|
Child::ChildStreambuf::sync() {
|
|
|
|
|
|
|
|
flushBuffer(); // Throws exception on failure.
|
|
|
|
|