mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-11 16:32:14 -06:00
StdIo: Restore Windows Console I/O modes on Ctrl-C
Extend commit fb3e7825f3 (StdIo: Restore Windows Console I/O modes on
exit, 2025-05-23, v4.1.0-rc1~114^2) to cover termination by Ctrl-C.
Fixes: #27427
This commit is contained in:
@@ -91,9 +91,26 @@ public:
|
||||
OStream StdOut{ std::cout, stdout };
|
||||
OStream StdErr{ std::cerr, stderr };
|
||||
|
||||
Globals();
|
||||
|
||||
static Globals& Get();
|
||||
};
|
||||
|
||||
#ifdef _WIN32
|
||||
Globals::Globals()
|
||||
{
|
||||
static auto const ctrlHandler = [](DWORD /*dwCtrlType*/) -> BOOL {
|
||||
Get().StdErr.Destroy();
|
||||
Get().StdOut.Destroy();
|
||||
Get().StdIn.Destroy();
|
||||
return FALSE;
|
||||
};
|
||||
SetConsoleCtrlHandler(ctrlHandler, TRUE);
|
||||
}
|
||||
#else
|
||||
Globals::Globals() = default;
|
||||
#endif
|
||||
|
||||
Globals& Globals::Get()
|
||||
{
|
||||
static Globals globals;
|
||||
|
||||
@@ -145,10 +145,16 @@ Stream::Stream(std::ios& s, FILE* file, Direction direction)
|
||||
|
||||
#ifdef _WIN32
|
||||
Stream::~Stream()
|
||||
{
|
||||
this->Destroy();
|
||||
}
|
||||
|
||||
void Stream::Destroy()
|
||||
{
|
||||
if (this->Console_) {
|
||||
this->IOS_.rdbuf()->pubsync();
|
||||
SetConsoleMode(this->Console_, this->ConsoleOrigMode_);
|
||||
this->Console_ = nullptr;
|
||||
}
|
||||
}
|
||||
#else
|
||||
|
||||
@@ -30,6 +30,11 @@ enum class TermKind
|
||||
*/
|
||||
class Stream
|
||||
{
|
||||
#ifdef _WIN32
|
||||
friend class Globals;
|
||||
void Destroy();
|
||||
#endif
|
||||
|
||||
public:
|
||||
/** The kind of terminal to which the stream is attached, if any. */
|
||||
TermKind Kind() const { return this->Kind_; }
|
||||
|
||||
Reference in New Issue
Block a user