From d74af1f6b0662aea66c75fa575a21aae1608723a Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Fri, 16 Nov 2012 20:22:13 +0000
Subject: [PATCH] redirect stdio to DOS console on windows

---
 Common/Gmsh.cpp |  4 ++++
 Common/OS.cpp   | 35 +++++++++++++++++++++++++++++++++--
 Common/OS.h     |  1 +
 3 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/Common/Gmsh.cpp b/Common/Gmsh.cpp
index aa0d9f0a1e..098b16b274 100644
--- a/Common/Gmsh.cpp
+++ b/Common/Gmsh.cpp
@@ -45,6 +45,10 @@
 
 int GmshInitialize(int argc, char **argv)
 {
+#if defined(HAVE_FLTK)
+  RedirectIOToConsole();
+#endif
+
   // we need at least one model during option parsing
   GModel *dummy = 0;
   if(GModel::list.empty()) dummy = new GModel();
diff --git a/Common/OS.cpp b/Common/OS.cpp
index 5685f92a0b..dd8bfdd1f4 100644
--- a/Common/OS.cpp
+++ b/Common/OS.cpp
@@ -26,6 +26,10 @@
 #include <process.h>
 #include <io.h>
 #include <direct.h>
+#include <fcntl.h>
+#include <io.h>
+#include <iostream>
+#include <fstream>
 #endif
 
 #if defined(__APPLE__)
@@ -238,8 +242,8 @@ std::string getCurrentWorkdir()
   if(!getcwd(path, sizeof(path))) return "";
 #endif
   std::string str(path);
-  // match the convention of SplitFileName that delivers directory path endig
-  // with a directory separator
+  // match the convention of SplitFileName that delivers directory path 
+  // ending with a directory separator
 #if defined(WIN32)
   str.append("\\");
 #else
@@ -247,3 +251,30 @@ std::string getCurrentWorkdir()
 #endif
   return str;
 }
+
+void RedirectIOToConsole()
+{
+#if defined(WIN32) && !defined(__CYGWIN__)
+  // Win32 GUI apps do not write to the DOS console; make it work again by
+  // attaching to parent console, which allows to use the DOS shell to work 
+  // with Gmsh on the command line (without this hack, you need to either use
+  // a better shell (e.g. bash), or compile a /subsystem:console version
+  AttachConsole(ATTACH_PARENT_PROCESS);
+  // redirect unbuffered stdout, stdin and stderr to the console
+  intptr_t lStdHandle = (intptr_t)GetStdHandle(STD_OUTPUT_HANDLE);
+  int hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
+  *stdout = _fdopen(hConHandle, "w");
+  setvbuf(stdout, NULL, _IONBF, 0);
+  lStdHandle = (intptr_t)GetStdHandle(STD_INPUT_HANDLE);
+  hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
+  *stdin = _fdopen(hConHandle, "r");
+  setvbuf(stdin, NULL, _IONBF, 0);
+  lStdHandle = (intptr_t)GetStdHandle(STD_ERROR_HANDLE);
+  hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
+  *stderr = _fdopen(hConHandle, "w");
+  setvbuf(stderr, NULL, _IONBF, 0);
+  // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog point to 
+  // console as well
+  std::ios::sync_with_stdio();
+#endif
+}
diff --git a/Common/OS.h b/Common/OS.h
index dacf126e04..397a804990 100644
--- a/Common/OS.h
+++ b/Common/OS.h
@@ -22,5 +22,6 @@ int KillProcess(int pid);
 int CreateDirectory(const std::string &dirName);
 int SystemCall(const std::string &command, bool blocking=false);
 std::string getCurrentWorkdir();
+void RedirectIOToConsole();
 
 #endif
-- 
GitLab