Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions subprocess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ Documentation for C++ subprocessing libraray.
#include <exception>
#include <locale>

#ifdef _MSC_VER
#if (defined _MSC_VER) || (defined __MINGW32__)
#define __USING_WINDOWS__
#endif

#ifdef __USING_WINDOWS__
#include <codecvt>
#endif

extern "C" {
#ifdef _MSC_VER
#ifdef __USING_WINDOWS__
#include <Windows.h>
#include <io.h>
#else
Expand Down Expand Up @@ -212,8 +216,8 @@ namespace util
}
}

#ifdef _MSC_VER
std::string get_last_error()
#ifdef __USING_WINDOWS__
inline std::string get_last_error()
{
DWORD errorMessageID = ::GetLastError();
if (errorMessageID == 0)
Expand All @@ -233,7 +237,7 @@ namespace util
return message;
}

FILE *file_from_handle(HANDLE h, const char *mode)
inline FILE *file_from_handle(HANDLE h, const char *mode)
{
int md;
if (!mode) {
Expand Down Expand Up @@ -265,7 +269,7 @@ namespace util
return fp;
}

void configure_pipe(HANDLE* read_handle, HANDLE* write_handle, HANDLE* child_handle)
inline void configure_pipe(HANDLE* read_handle, HANDLE* write_handle, HANDLE* child_handle)
{
SECURITY_ATTRIBUTES saAttr;

Expand Down Expand Up @@ -334,7 +338,7 @@ namespace util
}


#ifndef _MSC_VER
#ifndef __USING_WINDOWS__
/*!
* Function: set_clo_on_exec
* Sets/Resets the FD_CLOEXEC flag on the provided file descriptor
Expand Down Expand Up @@ -422,7 +426,7 @@ namespace util
static inline
int read_atmost_n(FILE* fp, char* buf, size_t read_upto)
{
#ifdef _MSC_VER
#ifdef __USING_WINDOWS__
return (int)fread(buf, 1, read_upto, fp);
#else
int fd = fileno(fp);
Expand Down Expand Up @@ -495,7 +499,7 @@ namespace util
return total_bytes_read;
}

#ifndef _MSC_VER
#ifndef __USING_WINDOWS__
/*!
* Function: wait_for_child_exit
* Waits for the process with pid `pid` to exit
Expand Down Expand Up @@ -674,7 +678,7 @@ struct input
}
input(IOTYPE typ) {
assert (typ == PIPE && "STDOUT/STDERR not allowed");
#ifndef _MSC_VER
#ifndef __USING_WINDOWS__
std::tie(rd_ch_, wr_ch_) = util::pipe_cloexec();
#endif
}
Expand Down Expand Up @@ -707,7 +711,7 @@ struct output
}
output(IOTYPE typ) {
assert (typ == PIPE && "STDOUT/STDERR not allowed");
#ifndef _MSC_VER
#ifndef __USING_WINDOWS__
std::tie(rd_ch_, wr_ch_) = util::pipe_cloexec();
#endif
}
Expand Down Expand Up @@ -739,7 +743,7 @@ struct error
error(IOTYPE typ) {
assert ((typ == PIPE || typ == STDOUT) && "STDERR not aloowed");
if (typ == PIPE) {
#ifndef _MSC_VER
#ifndef __USING_WINDOWS__
std::tie(rd_ch_, wr_ch_) = util::pipe_cloexec();
#endif
} else {
Expand Down Expand Up @@ -1042,7 +1046,7 @@ class Streams
std::shared_ptr<FILE> output_ = nullptr;
std::shared_ptr<FILE> error_ = nullptr;

#ifdef _MSC_VER
#ifdef __USING_WINDOWS__
HANDLE g_hChildStd_IN_Rd = nullptr;
HANDLE g_hChildStd_IN_Wr = nullptr;
HANDLE g_hChildStd_OUT_Rd = nullptr;
Expand Down Expand Up @@ -1149,7 +1153,7 @@ class Popen
/*
~Popen()
{
#ifdef _MSC_VER
#ifdef __USING_WINDOWS__
CloseHandle(this->process_handle_);
#endif
}
Expand Down Expand Up @@ -1217,7 +1221,7 @@ class Popen
private:
detail::Streams stream_;

#ifdef _MSC_VER
#ifdef __USING_WINDOWS__
HANDLE process_handle_;
#endif

Expand Down Expand Up @@ -1281,7 +1285,7 @@ inline void Popen::start_process() noexcept(false)

inline int Popen::wait() noexcept(false)
{
#ifdef _MSC_VER
#ifdef __USING_WINDOWS__
int ret = WaitForSingleObject(process_handle_, INFINITE);

return 0;
Expand All @@ -1308,7 +1312,7 @@ inline int Popen::poll() noexcept(false)
if (!child_created_) return -1; // TODO: ??
#endif

#ifdef _MSC_VER
#ifdef __USING_WINDOWS__
int ret = WaitForSingleObject(process_handle_, 0);
if (ret != WAIT_OBJECT_0) return -1;
#else
Expand All @@ -1317,7 +1321,7 @@ inline int Popen::poll() noexcept(false)
if (ret == 0) return -1;
#endif

#ifdef _MSC_VER
#ifdef __USING_WINDOWS__
DWORD dretcode_;
if (FALSE == GetExitCodeProcess(process_handle_, &dretcode_))
throw OSError("GetExitCodeProcess", 0);
Expand Down Expand Up @@ -1356,7 +1360,7 @@ inline int Popen::poll() noexcept(false)

inline void Popen::kill(int sig_num)
{
#ifdef _MSC_VER
#ifdef __USING_WINDOWS__
if (!TerminateProcess(this->process_handle_, (UINT)sig_num)) {
throw OSError("TerminateProcess", 0);
}
Expand All @@ -1369,7 +1373,7 @@ inline void Popen::kill(int sig_num)

inline void Popen::execute_process() noexcept(false)
{
#ifdef _MSC_VER
#ifdef __USING_WINDOWS__
if (this->shell_) {
throw OSError("shell not currently supported on windows", 0);
}
Expand Down Expand Up @@ -1587,7 +1591,7 @@ namespace detail {


inline void Child::execute_child() {
#ifndef _MSC_VER
#ifndef __USING_WINDOWS__
int sys_ret = -1;
auto& stream = parent_->stream_;

Expand Down Expand Up @@ -1686,7 +1690,7 @@ namespace detail {

inline void Streams::setup_comm_channels()
{
#ifdef _MSC_VER
#ifdef __USING_WINDOWS__
util::configure_pipe(&this->g_hChildStd_IN_Rd, &this->g_hChildStd_IN_Wr, &this->g_hChildStd_IN_Wr);
this->input(util::file_from_handle(this->g_hChildStd_IN_Wr, "w"));
this->write_to_child_ = _fileno(this->input());
Expand Down