diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 14be2b555..2de293e15 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -79,8 +79,14 @@ set(MAIN_SOURCES RemoteTools.cpp ) +if(WIN32) + set(CONSOLE_SOURCES Console-windows.cpp) +else() + set(CONSOLE_SOURCES Console-posix.cpp) +endif() + set(MAIN_SOURCES_WINDOWS - Console-windows.cpp + ${CONSOLE_SOURCES} Hooks-windows.cpp PlugLoad-windows.cpp Process-windows.cpp @@ -92,21 +98,21 @@ if(WIN32) endif() set(MAIN_SOURCES_LINUX - Console-posix.cpp + ${CONSOLE_SOURCES} Hooks-linux.cpp PlugLoad-posix.cpp Process-linux.cpp ) set(MAIN_SOURCES_DARWIN - Console-posix.cpp + ${CONSOLE_SOURCES} Hooks-darwin.cpp PlugLoad-posix.cpp Process-darwin.cpp ) set(MAIN_SOURCES_LINUX_EGGY - Console-linux.cpp + ${CONSOLE_SOURCES} Hooks-egg.cpp PlugLoad-linux.cpp Process-linux.cpp @@ -359,7 +365,7 @@ add_library(dfhack SHARED ${PROJECT_SOURCES}) add_dependencies(dfhack generate_proto_core) add_dependencies(dfhack generate_headers) -add_library(dfhack-client SHARED RemoteClient.cpp ColorText.cpp MiscUtils.cpp Error.cpp ${PROJECT_PROTO_SRCS}) +add_library(dfhack-client SHARED RemoteClient.cpp ColorText.cpp MiscUtils.cpp Error.cpp ${PROJECT_PROTO_SRCS} ${CONSOLE_SOURCES}) add_dependencies(dfhack-client dfhack) add_executable(dfhack-run dfhack-run.cpp) diff --git a/library/Console-posix.cpp b/library/Console-posix.cpp index 696672861..9633f80e0 100644 --- a/library/Console-posix.cpp +++ b/library/Console-posix.cpp @@ -827,21 +827,23 @@ Console::~Console() delete d; } -bool Console::init(bool sharing) +bool Console::init(bool dont_redirect) { - if(sharing) - { - inited = false; - return false; - } - if (!freopen("stdout.log", "w", stdout)) - ; d = new Private(); // make our own weird streams so our IO isn't redirected - d->dfout_C = fopen("/dev/tty", "w"); + if (dont_redirect) + { + d->dfout_C = fopen("/dev/stdout", "w"); + } + else + { + if (!freopen("stdout.log", "w", stdout)) + ; + d->dfout_C = fopen("/dev/tty", "w"); + } std::cin.tie(this); clear(); - d->supported_terminal = !isUnsupportedTerm() && isatty(STDIN_FILENO); + d->supported_terminal = !isUnsupportedTerm() && isatty(STDIN_FILENO); // init the exit mechanism if (pipe(d->exit_pipe) == -1) ; @@ -858,6 +860,8 @@ bool Console::shutdown(void) return true; lock_guard g(*wlock); close(d->exit_pipe[1]); + if (d->state != Private::con_lineedit) + inited = false; return true; } diff --git a/library/Core.cpp b/library/Core.cpp index 00a8879fc..c0a1155c7 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -1707,7 +1707,6 @@ bool Core::Init() } if (is_text_mode && !is_headless) { - con.init(true); cerr << "Console is not available. Use dfhack-run to send commands.\n"; if (!is_text_mode) { diff --git a/library/dfhack-run.cpp b/library/dfhack-run.cpp index af00e4c04..df10bf148 100644 --- a/library/dfhack-run.cpp +++ b/library/dfhack-run.cpp @@ -45,6 +45,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include +#include "Console.h" #include "RemoteClient.h" #include @@ -55,11 +56,10 @@ POSSIBILITY OF SUCH DAMAGE. using namespace DFHack; using namespace dfproto; -using std::cout; int main (int argc, char *argv[]) { - color_ostream_wrapper out(cout); + Console out; if (argc <= 1) { @@ -85,12 +85,15 @@ int main (int argc, char *argv[]) if (!client.connect()) return 2; + out.init(true); + command_result rv; if (strcmp(argv[1], "--lua") == 0) { if (argc <= 3) { + out.shutdown(); fprintf(stderr, "Usage: dfhack-run --lua [args...]\n"); return 2; } @@ -99,6 +102,7 @@ int main (int argc, char *argv[]) if (!run_call.bind(&client, "RunLua")) { + out.shutdown(); fprintf(stderr, "No RunLua protocol function found."); return 3; } @@ -130,6 +134,7 @@ int main (int argc, char *argv[]) } out.flush(); + out.shutdown(); if (rv != CR_OK) return 1; diff --git a/library/include/Console.h b/library/include/Console.h index 0517704d4..0882ba449 100644 --- a/library/include/Console.h +++ b/library/include/Console.h @@ -135,7 +135,7 @@ namespace DFHack ///dtor, NOT thread-safe ~Console(); /// initialize the console. NOT thread-safe - bool init( bool sharing ); + bool init( bool dont_redirect ); /// shutdown the console. NOT thread-safe bool shutdown( void );