Skip to content

Commit 808d1be

Browse files
committed
fix
1 parent 8b45a74 commit 808d1be

File tree

6 files changed

+61
-11
lines changed

6 files changed

+61
-11
lines changed

cpp-terminal/private/file.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,25 @@ std::string Term::Private::InputFileHandler::read() const
132132
ReadConsole(Private::in.handle(), &ret[0], static_cast<DWORD>(ret.size()), &nread, nullptr);
133133
return ret.c_str();
134134
#else
135-
std::size_t nread{0};
136-
Term::Private::Errno().check_if(::ioctl(Private::in.fd(), FIONREAD, &nread) != 0).throw_exception("::ioctl(Private::in.fd(), FIONREAD, &nread)"); //NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
135+
#if defined(MAX_INPUT)
136+
static const constexpr std::size_t max_input{MAX_INPUT};
137+
#else
138+
static const constexpr std::size_t max_input{256};
139+
#endif
140+
#if defined(_POSIX_MAX_INPUT)
141+
static const constexpr std::size_t posix_max_input{MAX_INPUT};
142+
#else
143+
static const constexpr std::size_t posix_max_input{256};
144+
#endif
145+
static std::size_t nread{std::max(max_input, posix_max_input)};
146+
try
147+
{
148+
Term::Private::Errno().check_if(::ioctl(Private::in.fd(), FIONREAD, &nread) != 0).throw_exception("::ioctl(Private::in.fd(), FIONREAD, &nread)"); //NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
149+
}
150+
catch(const ErrnoException& exception)
151+
{
152+
if(exception.code() != 25) throw;
153+
}
137154
std::string ret(nread, '\0');
138155
if(nread != 0)
139156
{

cpp-terminal/private/input.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ void sendString(Term::Private::BlockingQueue& events, std::wstring& str)
7777
}
7878

7979
#endif
80-
8180
Term::Private::Input::~Input()
8281
{
8382
if(m_thread.joinable()) m_thread.join();
@@ -91,10 +90,6 @@ int Term::Private::Input::m_poll{-1};
9190

9291
void Term::Private::Input::init_thread()
9392
{
94-
if(m_thread.joinable()) m_thread.join();
95-
std::thread thread(Term::Private::Input::read_event);
96-
m_thread.swap(thread);
97-
Term::Private::Sigwinch::unblockSigwinch();
9893
#if defined(__linux__)
9994
m_poll = {::epoll_create1(EPOLL_CLOEXEC)};
10095
::epoll_event signal;
@@ -106,6 +101,9 @@ void Term::Private::Input::init_thread()
106101
input.data.fd = {Term::Private::in.fd()};
107102
::epoll_ctl(m_poll, EPOLL_CTL_ADD, Term::Private::in.fd(), &input);
108103
#endif
104+
if(m_thread.joinable()) m_thread.join();
105+
std::thread thread(Term::Private::Input::read_event);
106+
m_thread.swap(thread);
109107
}
110108

111109
void Term::Private::Input::read_event()

cpp-terminal/private/terminal_impl.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "cpp-terminal/private/env.hpp"
1212
#include "cpp-terminal/private/exception.hpp"
1313
#include "cpp-terminal/private/file.hpp"
14+
#include "cpp-terminal/private/sigwinch.hpp"
1415
#include "cpp-terminal/terminal.hpp"
1516

1617
#if defined(_WIN32)
@@ -108,11 +109,14 @@ try
108109
static termios orig_termios;
109110
if(!enabled)
110111
{
112+
Term::Private::Sigwinch::blockSigwinch();
113+
Term::Private::Sigwinch::registerSigwinch();
111114
if(!Private::out.null()) { Term::Private::Errno().check_if(tcgetattr(Private::out.fd(), &orig_termios) == -1).throw_exception("tcgetattr() failed"); }
112115
enabled = true;
113116
}
114117
else
115118
{
119+
Term::Private::Sigwinch::unblockSigwinch();
116120
unsetMouseEvents();
117121
unsetFocusEvents();
118122
if(!Private::out.null()) { Term::Private::Errno().check_if(tcsetattr(Private::out.fd(), TCSAFLUSH, &orig_termios) == -1).throw_exception("tcsetattr() failed in destructor"); }
@@ -220,7 +224,7 @@ void Term::Terminal::setMode() const
220224
unsetFocusEvents();
221225
}
222226
if(m_options.has(Option::NoSignalKeys)) { send.c_lflag &= ~static_cast<std::size_t>(ISIG); } //FIXME need others flags !
223-
else if(m_options.has(Option::NoSignalKeys)) { send.c_lflag |= ISIG; }
227+
else if(m_options.has(Option::SignalKeys)) { send.c_lflag |= ISIG; }
224228
Term::Private::Errno().check_if(tcsetattr(Private::out.fd(), TCSAFLUSH, &send) == -1).throw_exception("tcsetattr(Private::out.fd(), TCSAFLUSH, &send)");
225229
}
226230
#endif

cpp-terminal/terminal_impl.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "cpp-terminal/options.hpp"
1414
#include "cpp-terminal/private/exception.hpp"
1515
#include "cpp-terminal/private/file.hpp"
16-
#include "cpp-terminal/private/sigwinch.hpp"
1716
#include "cpp-terminal/screen.hpp"
1817
#include "cpp-terminal/style.hpp"
1918
#include "cpp-terminal/terminal.hpp" //FIXME avoid recursion
@@ -23,8 +22,6 @@ Term::Options Term::Terminal::getOptions() const noexcept { return m_options; }
2322
Term::Terminal::Terminal() noexcept
2423
try
2524
{
26-
Term::Private::Sigwinch::blockSigwinch();
27-
Term::Private::Sigwinch::registerSigwinch();
2825
store_and_restore();
2926
setMode(); //Save the default cpp-terminal mode done in store_and_restore();
3027
set_unset_utf8();

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ cppterminal_example(SOURCE prompt_not_immediate)
3939
cppterminal_example(SOURCE prompt_simple)
4040
cppterminal_example(SOURCE styles)
4141
cppterminal_example(SOURCE utf8)
42+
cppterminal_example(SOURCE signal)
4243
cppterminal_example(SOURCE attach_console WIN32)
4344
cppterminal_example(SOURCE attach_console_minimal WIN32)

examples/signal.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
/*
3+
* cpp-terminal
4+
* C++ library for writing multi-platform terminal applications.
5+
*
6+
* SPDX-FileCopyrightText: 2019-2024 cpp-terminal
7+
*
8+
* SPDX-License-Identifier: MIT
9+
*/
10+
11+
#include "cpp-terminal/iostream.hpp"
12+
#include "cpp-terminal/terminal.hpp"
13+
14+
#include <iostream>
15+
16+
int main()
17+
{
18+
std::at_quick_exit(
19+
[]()
20+
{
21+
std::cout << "Unhandled exception\n" << std::flush;
22+
//std::abort();
23+
});
24+
std::set_terminate(
25+
[]()
26+
{
27+
std::cout << "Unhandled exception\n" << std::flush;
28+
//std::abort();
29+
});
30+
Term::terminal.setOptions(Term::Option::Raw, Term::Option::SignalKeys);
31+
while(true) {}
32+
return 0;
33+
}

0 commit comments

Comments
 (0)