From 26d6eb4a8ab4052d7f873ca0cc2c6b68f3d41a44 Mon Sep 17 00:00:00 2001 From: Martin Herkt Date: Thu, 16 Jan 2014 11:25:52 +0100 Subject: io/win32: move mp_attach_console to terminal-win.c Why didn't I put it there from the start? --- osdep/terminal-win.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'osdep/terminal-win.c') diff --git a/osdep/terminal-win.c b/osdep/terminal-win.c index 1ecb520f94..9d49936d70 100644 --- a/osdep/terminal-win.c +++ b/osdep/terminal-win.c @@ -24,6 +24,7 @@ #include "config.h" +#include #include #include #include @@ -238,6 +239,22 @@ void terminal_set_foreground_color(FILE *stream, int c) int terminal_init(void) { + if (AttachConsole(ATTACH_PARENT_PROCESS)) { + // We have been started by something with a console window. + // Redirect output streams to that console's low-level handles, + // so we can actually use WriteConsole later on. + + int hConHandle; + + hConHandle = _open_osfhandle((intptr_t)hSTDOUT, _O_TEXT); + *stdout = *_fdopen(hConHandle, "w"); + setvbuf(stdout, NULL, _IONBF, 0); + + hConHandle = _open_osfhandle((intptr_t)hSTDERR, _O_TEXT); + *stderr = *_fdopen(hConHandle, "w"); + setvbuf(stderr, NULL, _IONBF, 0); + } + CONSOLE_SCREEN_BUFFER_INFO cinfo; DWORD cmode = 0; GetConsoleMode(hSTDOUT, &cmode); -- cgit v1.2.3