From 6b46852902eba38672b96436771ffaab4ca6872b Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 20 Oct 2020 06:57:56 +1000 Subject: [PATCH] debug: add is_interactive_console query --- CMakeLists.txt | 2 ++ debug/system.hpp | 5 +++++ debug/system_posix.cpp | 10 ++++++++++ debug/system_win32.cpp | 14 ++++++++++++++ 4 files changed, 31 insertions(+) create mode 100644 debug/system_posix.cpp create mode 100644 debug/system_win32.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f212562a..0bdf74ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -124,6 +124,7 @@ if (NOT WIN32) memory/system.hpp debug_posix.cpp debug/crash_posix.cpp + debug/system_posix.cpp io_posix.cpp io_posix.hpp library_posix.hpp @@ -147,6 +148,7 @@ if (WIN32) APPEND UTIL_FILES debug_win32.cpp debug/crash_win32.cpp + debug/system_win32.cpp exe_win32.cpp io_win32.cpp io_win32.hpp diff --git a/debug/system.hpp b/debug/system.hpp index 1ec872d8..a0204216 100644 --- a/debug/system.hpp +++ b/debug/system.hpp @@ -16,4 +16,9 @@ void force_console (void); /////////////////////////////////////////////////////////////////////////////// namespace cruft::debug { void init (void); + + + /// Return true if the application appears to be running as an + /// interactive console application. + bool is_interactive_console (void); } diff --git a/debug/system_posix.cpp b/debug/system_posix.cpp new file mode 100644 index 00000000..a79fbb46 --- /dev/null +++ b/debug/system_posix.cpp @@ -0,0 +1,10 @@ +#include "./system.hpp" + +#include + + +bool +cruft::debug::is_interactive_console (void) +{ + return isatty (STDIN_FILENO); +} diff --git a/debug/system_win32.cpp b/debug/system_win32.cpp new file mode 100644 index 00000000..98600f1f --- /dev/null +++ b/debug/system_win32.cpp @@ -0,0 +1,14 @@ +#include "./system.hpp" + +#include + + +bool cruft::debug::is_interactive_console (void) +{ + HWND console_window = GetConsoleWindow (); + + DWORD dwProcessId; + GetWindowThreadProcessId (console_window, &dwProcessId); + + return GetCurrentProcessId () == dwProcessId; +} \ No newline at end of file