diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2026-09-22 22:47:33 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2026-09-22 22:47:33 +0200 |
| commit | d05b9343f69a0841043963088e0003368e205bd9 (patch) | |
| tree | 8cde07ff86d27cbc808a0a149b89d0024b4a586c /ttwhy/terminal/signal_handling.cppm | |
| parent | 22f6534081acfabdab4956d337604a21e7da5b64 (diff) | |
| download | ttwhy-master.tar.xz ttwhy-master.zip | |
Diffstat (limited to 'ttwhy/terminal/signal_handling.cppm')
| -rw-r--r-- | ttwhy/terminal/signal_handling.cppm | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/ttwhy/terminal/signal_handling.cppm b/ttwhy/terminal/signal_handling.cppm new file mode 100644 index 0000000..a3b5d4d --- /dev/null +++ b/ttwhy/terminal/signal_handling.cppm @@ -0,0 +1,58 @@ +module; + +#include <asio.hpp> + +#include <csignal> +#include <format> +#include <system_error> + +export module ttwhy.terminal:signal_handling; + +namespace ttwhy::terminal +{ + + export template<typename Stream, typename TerminalAttributes> + auto handle_signals(Stream & stream, TerminalAttributes & attributes) -> asio::awaitable<std::error_code> + { + auto executor = co_await asio::this_coro::executor; + auto signals = asio::signal_set{executor, SIGINT, SIGTERM}; + signals.add(SIGTSTP); + signals.add(SIGCONT); + + while (true) + { + auto [error, signal] = co_await signals.async_wait(asio::as_tuple(asio::use_awaitable)); + if (error) + { + co_return error; + } + + switch (signal) + { + case SIGINT: + case SIGTERM: + { + auto message = std::format("Received signal {}, exiting ...\n", signal); + co_await asio::async_write(stream, asio::buffer(message), asio::use_awaitable); + co_return asio::error_code{}; + } + case SIGTSTP: + { + attributes.canonical_mode(true).echo(true); + ::raise(SIGSTOP); + break; + } + case SIGCONT: + { + attributes.canonical_mode(false).echo(false); + break; + } + } + + if (!error) + { + } + } + } + +} // namespace ttwhy::terminal |
