Skip to content
Snippets Groups Projects
Commit d74af1f6 authored by Christophe Geuzaine's avatar Christophe Geuzaine
Browse files

redirect stdio to DOS console on windows

parent 8d32f8e6
No related branches found
No related tags found
No related merge requests found
...@@ -45,6 +45,10 @@ ...@@ -45,6 +45,10 @@
int GmshInitialize(int argc, char **argv) int GmshInitialize(int argc, char **argv)
{ {
#if defined(HAVE_FLTK)
RedirectIOToConsole();
#endif
// we need at least one model during option parsing // we need at least one model during option parsing
GModel *dummy = 0; GModel *dummy = 0;
if(GModel::list.empty()) dummy = new GModel(); if(GModel::list.empty()) dummy = new GModel();
......
...@@ -26,6 +26,10 @@ ...@@ -26,6 +26,10 @@
#include <process.h> #include <process.h>
#include <io.h> #include <io.h>
#include <direct.h> #include <direct.h>
#include <fcntl.h>
#include <io.h>
#include <iostream>
#include <fstream>
#endif #endif
#if defined(__APPLE__) #if defined(__APPLE__)
...@@ -238,8 +242,8 @@ std::string getCurrentWorkdir() ...@@ -238,8 +242,8 @@ std::string getCurrentWorkdir()
if(!getcwd(path, sizeof(path))) return ""; if(!getcwd(path, sizeof(path))) return "";
#endif #endif
std::string str(path); std::string str(path);
// match the convention of SplitFileName that delivers directory path endig // match the convention of SplitFileName that delivers directory path
// with a directory separator // ending with a directory separator
#if defined(WIN32) #if defined(WIN32)
str.append("\\"); str.append("\\");
#else #else
...@@ -247,3 +251,30 @@ std::string getCurrentWorkdir() ...@@ -247,3 +251,30 @@ std::string getCurrentWorkdir()
#endif #endif
return str; 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
}
...@@ -22,5 +22,6 @@ int KillProcess(int pid); ...@@ -22,5 +22,6 @@ int KillProcess(int pid);
int CreateDirectory(const std::string &dirName); int CreateDirectory(const std::string &dirName);
int SystemCall(const std::string &command, bool blocking=false); int SystemCall(const std::string &command, bool blocking=false);
std::string getCurrentWorkdir(); std::string getCurrentWorkdir();
void RedirectIOToConsole();
#endif #endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment