diff --git a/Common/Gmsh.cpp b/Common/Gmsh.cpp index aa0d9f0a1ecf9c34ba172050527be787ca27ef44..098b16b274c07c5e92713b42062e4683e2339426 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 5685f92a0b8d068223b69523697fa95191d31c28..dd8bfdd1f4b416b3b0d45cd8ce3434b4669763d2 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 dacf126e04fb5c9e92883902b931a2d38c448140..397a80499057b4f35245af9a7770304a5de358c7 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