Skip to content

Commit

Permalink
chore: Platform fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
shdwmtr committed Oct 6, 2024
1 parent 44ebf0b commit a94ca91
Showing 1 changed file with 33 additions and 35 deletions.
68 changes: 33 additions & 35 deletions src/pipes/terminal_pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,46 @@
#include <sys/log.h>
#include <fcntl.h>

#ifdef _WIN32
extern "C" {
__declspec(dllexport) const int CreateTerminalPipe()
{
#ifdef _WIN32
{
HANDLE hPipe = CreateFileA(R"(\\.\pipe\MillenniumStdoutPipe)", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

if (hPipe == INVALID_HANDLE_VALUE)
{
std::cerr << "Failed to connect to pipe." << std::endl;
return 1;
}
HANDLE hPipe = CreateFileA(R"(\\.\pipe\MillenniumStdoutPipe)", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

int pipeDescriptor = _open_osfhandle((intptr_t)hPipe, _O_WRONLY);
if (pipeDescriptor == -1)
{
std::cerr << "Failed to get pipe file descriptor." << std::endl;
CloseHandle(hPipe);
return 1;
}
if (hPipe == INVALID_HANDLE_VALUE)
{
std::cerr << "Failed to connect to pipe." << std::endl;
return 1;
}

FILE* pipeFile = _fdopen(pipeDescriptor, "w");
if (!pipeFile)
{
std::cerr << "Failed to open pipe file descriptor as FILE*." << std::endl;
CloseHandle(hPipe);
return 1;
}
int pipeDescriptor = _open_osfhandle((intptr_t)hPipe, _O_WRONLY);
if (pipeDescriptor == -1)
{
std::cerr << "Failed to get pipe file descriptor." << std::endl;
CloseHandle(hPipe);
return 1;
}

// Redirect stdout to the named pipe
if (dup2(_fileno(pipeFile), _fileno(stdout)) == -1)
{
std::cerr << "Failed to redirect stdout to pipe." << std::endl;
CloseHandle(hPipe);
fclose(pipeFile); // Close the FILE* on failure
return 1;
}
setvbuf(stdout, NULL, _IONBF, 0);
FILE* pipeFile = _fdopen(pipeDescriptor, "w");
if (!pipeFile)
{
std::cerr << "Failed to open pipe file descriptor as FILE*." << std::endl;
CloseHandle(hPipe);
return 1;
}

std::shared_ptr<FILE*> pipePtr = std::make_shared<FILE*>(pipeFile);
// Redirect stdout to the named pipe
if (dup2(_fileno(pipeFile), _fileno(stdout)) == -1)
{
std::cerr << "Failed to redirect stdout to pipe." << std::endl;
CloseHandle(hPipe);
fclose(pipeFile); // Close the FILE* on failure
return 1;
}
#endif
setvbuf(stdout, NULL, _IONBF, 0);

std::shared_ptr<FILE*> pipePtr = std::make_shared<FILE*>(pipeFile);
return 0;
}
}
}
#endif

0 comments on commit a94ca91

Please sign in to comment.