Skip to content

Commit

Permalink
refactor: Replace custom __USING_WINDOWS__ with WIN32 in subprocess.hpp
Browse files Browse the repository at this point in the history
The `WIN32` macro is used across the entire code base to designate
building for Windows.
  • Loading branch information
hebasto committed Apr 19, 2024
1 parent a00f14e commit c05204c
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions src/util/subprocess.hpp
Expand Up @@ -53,16 +53,12 @@ Documentation for C++ subprocessing library.
#include <string>
#include <vector>

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

#ifdef __USING_WINDOWS__
#ifdef WIN32
#include <codecvt>
#endif

extern "C" {
#ifdef __USING_WINDOWS__
#ifdef WIN32
#include <windows.h>
#include <io.h>
#include <cwchar>
Expand Down Expand Up @@ -157,7 +153,7 @@ class OSError: public std::runtime_error
//--------------------------------------------------------------------

//Environment Variable types
#ifndef __USING_WINDOWS__
#ifndef WIN32
using env_string_t = std::string;
using env_char_t = char;
#else
Expand Down Expand Up @@ -236,7 +232,7 @@ namespace util
}
}

#ifdef __USING_WINDOWS__
#ifdef WIN32
inline std::string get_last_error(DWORD errorMessageID)
{
if (errorMessageID == 0)
Expand Down Expand Up @@ -451,7 +447,7 @@ namespace util
}


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

#ifndef __USING_WINDOWS__
#ifndef WIN32
/*!
* Function: wait_for_child_exit
* Waits for the process with pid `pid` to exit
Expand Down Expand Up @@ -791,7 +787,7 @@ struct input
}
explicit input(IOTYPE typ) {
assert (typ == PIPE && "STDOUT/STDERR not allowed");
#ifndef __USING_WINDOWS__
#ifndef WIN32
std::tie(rd_ch_, wr_ch_) = util::pipe_cloexec();
#endif
}
Expand Down Expand Up @@ -824,7 +820,7 @@ struct output
}
explicit output(IOTYPE typ) {
assert (typ == PIPE && "STDOUT/STDERR not allowed");
#ifndef __USING_WINDOWS__
#ifndef WIN32
std::tie(rd_ch_, wr_ch_) = util::pipe_cloexec();
#endif
}
Expand Down Expand Up @@ -856,7 +852,7 @@ struct error
explicit error(IOTYPE typ) {
assert ((typ == PIPE || typ == STDOUT) && "STDERR not allowed");
if (typ == PIPE) {
#ifndef __USING_WINDOWS__
#ifndef WIN32
std::tie(rd_ch_, wr_ch_) = util::pipe_cloexec();
#endif
} else {
Expand Down Expand Up @@ -1159,7 +1155,7 @@ class Streams
std::shared_ptr<FILE> output_ = nullptr;
std::shared_ptr<FILE> error_ = nullptr;

#ifdef __USING_WINDOWS__
#ifdef WIN32
HANDLE g_hChildStd_IN_Rd = nullptr;
HANDLE g_hChildStd_IN_Wr = nullptr;
HANDLE g_hChildStd_OUT_Rd = nullptr;
Expand Down Expand Up @@ -1266,7 +1262,7 @@ class Popen
/*
~Popen()
{
#ifdef __USING_WINDOWS__
#ifdef WIN32
CloseHandle(this->process_handle_);
#endif
}
Expand Down Expand Up @@ -1342,7 +1338,7 @@ class Popen
private:
detail::Streams stream_;

#ifdef __USING_WINDOWS__
#ifdef WIN32
HANDLE process_handle_;
std::future<void> cleanup_future_;
#endif
Expand Down Expand Up @@ -1407,7 +1403,7 @@ inline void Popen::start_process() noexcept(false)

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

return 0;
Expand All @@ -1428,7 +1424,7 @@ inline int Popen::wait() noexcept(false)

inline int Popen::poll() noexcept(false)
{
#ifdef __USING_WINDOWS__
#ifdef WIN32
int ret = WaitForSingleObject(process_handle_, 0);
if (ret != WAIT_OBJECT_0) return -1;

Expand Down Expand Up @@ -1478,7 +1474,7 @@ inline int Popen::poll() noexcept(false)

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

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


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

Expand Down Expand Up @@ -1818,7 +1814,7 @@ namespace detail {

inline void Streams::setup_comm_channels()
{
#ifdef __USING_WINDOWS__
#ifdef WIN32
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

0 comments on commit c05204c

Please sign in to comment.