diff options
author | Greg Hudson <ghudson@mit.edu> | 2020-08-18 12:46:50 -0400 |
---|---|---|
committer | Greg Hudson <ghudson@mit.edu> | 2020-09-09 17:11:36 -0400 |
commit | 01164c45c84e2292a2c29ff125d5d2e8fb9cd675 (patch) | |
tree | 50d2eea0e11d05b506e62bfdb4d4a32cc3fb9652 | |
parent | 64f4b75a22212681ca293f8f09ddd24b0244d5b4 (diff) | |
download | krb5-01164c45c84e2292a2c29ff125d5d2e8fb9cd675.zip krb5-01164c45c84e2292a2c29ff125d5d2e8fb9cd675.tar.gz krb5-01164c45c84e2292a2c29ff125d5d2e8fb9cd675.tar.bz2 |
Fix Leash console option
Remove out2con.cpp, which no longer works. Replace it with a simple
method for creating a console and redirecting stdout and stderr to it.
ticket: 8937 (new)
-rw-r--r-- | src/windows/leash/Leash.cpp | 6 | ||||
-rw-r--r-- | src/windows/leash/Makefile.in | 1 | ||||
-rw-r--r-- | src/windows/leash/out2con.cpp | 124 | ||||
-rw-r--r-- | src/windows/leash/out2con.h | 38 |
4 files changed, 4 insertions, 165 deletions
diff --git a/src/windows/leash/Leash.cpp b/src/windows/leash/Leash.cpp index a9bb8ea..5aada73 100644 --- a/src/windows/leash/Leash.cpp +++ b/src/windows/leash/Leash.cpp @@ -25,7 +25,6 @@ #include "reminder.h" #include <leasherr.h> #include "lglobals.h" -#include "out2con.h" #include <krb5.h> #include <com_err.h> @@ -291,7 +290,10 @@ BOOL CLeashApp::InitInstance() else if (0 == stricmp(optionParam+1, "console") || 0 == stricmp(optionParam+1, "c")) { - CreateConsoleEcho(); + FILE *dummy; + AllocConsole(); + freopen_s(&dummy, "CONOUT$", "w", stderr); + freopen_s(&dummy, "CONOUT$", "w", stdout); } else if (0 == stricmp(optionParam+1, "noribbon")) { diff --git a/src/windows/leash/Makefile.in b/src/windows/leash/Makefile.in index 4ed42f7..645cdec 100644 --- a/src/windows/leash/Makefile.in +++ b/src/windows/leash/Makefile.in @@ -16,7 +16,6 @@ OBJS= \ $(OUTPRE)LeashUICommandHandler.obj \ $(OUTPRE)LeashView.obj \ $(OUTPRE)MainFrm.obj \ - $(OUTPRE)out2con.obj \ $(OUTPRE)StdAfx.obj \ $(OUTPRE)KrbListTickets.obj diff --git a/src/windows/leash/out2con.cpp b/src/windows/leash/out2con.cpp deleted file mode 100644 index 877eac1..0000000 --- a/src/windows/leash/out2con.cpp +++ /dev/null @@ -1,124 +0,0 @@ -#include "out2con.h" - -#include <windows.h> -#include <stdio.h> -#include <io.h> - -class ConsoleEcho -{ -public: - ConsoleEcho(); - ~ConsoleEcho(); - -private: - DWORD ThreadLoop(); - - static DWORD WINAPI ThreadFunc(void* param); - - FILE m_originalStdout; - int m_stdoutfd; - int m_pipefd; - HANDLE m_hReadPipe, m_hWritePipe; - HANDLE m_hThread; - - static const int BUFSIZE=512; -}; - - -ConsoleEcho * -CreateConsoleEcho() -{ - return new ConsoleEcho; -} - -void -DestroyConsoleEcho(ConsoleEcho *echo) -{ - delete echo; -} - - -DWORD WINAPI ConsoleEcho::ThreadFunc(void* param) -{ - return ((ConsoleEcho*)(param))->ThreadLoop(); -} - - -DWORD ConsoleEcho::ThreadLoop() -{ - DWORD dwRead, dwWritten; - CHAR chBuf[BUFSIZE]; - BOOL bSuccess = FALSE; - // Note that the following does not work when running in the msvc2010 - // debugger with redirected output; you still get the redirected file - // handle, not the console: - //HANDLE hConsoleStdOut = GetStdHandle(STD_OUTPUT_HANDLE); - // This seems to be more reliable: - HANDLE hConsoleStdOut = CreateFile("CONOUT$", - GENERIC_WRITE, - FILE_SHARE_WRITE, - NULL, OPEN_EXISTING, 0, 0); - for (;;) { - // read from redirected stdout - bSuccess = ReadFile(m_hReadPipe, chBuf, BUFSIZE, &dwRead, NULL); - if (!bSuccess || (dwRead == 0)) - break; - - // write to console - WriteFile(hConsoleStdOut, chBuf, dwRead, &dwWritten, NULL); - // also write to original stdout - if (m_stdoutfd>=0) { - _write(m_stdoutfd, chBuf, dwRead); - // _commit() causes assert if m_stdoutfd is a device (e.g., console or NUL). - if (!_isatty(m_stdoutfd)) - _commit(m_stdoutfd); - } - } - CloseHandle(hConsoleStdOut); - return 0; -} - -ConsoleEcho::ConsoleEcho() -{ - // setup console - AllocConsole(); - // create pipe - CreatePipe(&m_hReadPipe, &m_hWritePipe, NULL, 0); - // save original stdout to preserve commandline-specified redirection - m_stdoutfd = _fileno(stdout); - // and copy the whole damn FILE structure so we can restore it - // when we're done. I don't know any other way to restore the - // crazy windows gui default '-2' filedesc stdout. - m_originalStdout = *stdout; - // hook up the write end of our pipe to stdout - m_pipefd = _open_osfhandle((intptr_t)m_hWritePipe, 0); - // take our os file handle and allocate a crt FILE for it - FILE* fp = _fdopen(m_pipefd, "w"); - // copy to stdout - *stdout = *fp; - // fp leaks, but we can't close it without closing the OS file handle - - // disable buffering - setvbuf(stdout, NULL, _IONBF, 0); - - // Create a thread to process our pipe, forwarding output - // to both the console and the original stdout - m_hThread = CreateThread(NULL, 0, &ThreadFunc, this, 0, NULL); -} - -ConsoleEcho::~ConsoleEcho() -{ - // fclose() unfortunately immediately invalidates the read pipe before the - // pipe thread has a chance to flush it, so don't do that. - //fclose(stdout); - - // instead, just slam the original stdout - *stdout = m_originalStdout; - //printf("Safe to printf now and no longer echoed to console.\n"); - // Close write pipe - _close(m_pipefd); - // and wait here for pipe thread to exit - WaitForSingleObject(m_hThread, 1000); - // now close read pipe as well - CloseHandle(m_hReadPipe); -} diff --git a/src/windows/leash/out2con.h b/src/windows/leash/out2con.h deleted file mode 100644 index ebd3859..0000000 --- a/src/windows/leash/out2con.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef OUT2CON_H -#define OUT2CON_H - -/* Call CreateConsoleEcho() to create a console and begin echoing stdout to it. - * The original stream (if any) will still receive output from stdout. - * Call DestroyConsoleEcho() to stop echoing stdout to the console. - * The original stream continues to receive stdout. - * - * WARNING: it is not safe to use stdout from another thread during - * CreateConsoleEcho() or DestroyConsoleEcho() - */ - -class ConsoleEcho; - -ConsoleEcho * -CreateConsoleEcho(); - -void -DestroyConsoleEcho(ConsoleEcho *consoleEcho); - -// Convenience class to automatically echo to console within a scope -class AutoConsoleEcho -{ -public: - AutoConsoleEcho() : m_echo(CreateConsoleEcho()) - { - } - - ~AutoConsoleEcho() - { - DestroyConsoleEcho(m_echo); - } -private: - ConsoleEcho* m_echo; -}; - - -#endif |