From 8541fd553ea001c21a3480c4dc4ac1d6d6460f38 Mon Sep 17 00:00:00 2001 From: Alexey Rybalchenko Date: Sat, 2 Oct 2021 11:43:41 +0200 Subject: [PATCH] feat: add tool for noncanonical input --- fairmq/CMakeLists.txt | 1 + fairmq/plugins/control/Control.cxx | 31 ++----------------- fairmq/shmem/Monitor.cxx | 30 ++---------------- fairmq/tools/IO.h | 49 ++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 56 deletions(-) create mode 100644 fairmq/tools/IO.h diff --git a/fairmq/CMakeLists.txt b/fairmq/CMakeLists.txt index 19a5d34e..cfdb788f 100644 --- a/fairmq/CMakeLists.txt +++ b/fairmq/CMakeLists.txt @@ -31,6 +31,7 @@ if(BUILD_FAIRMQ OR BUILD_SDK) set(TOOLS_PUBLIC_HEADER_FILES tools/CppSTL.h tools/InstanceLimit.h + tools/IO.h tools/Network.h tools/Process.h tools/RateLimit.h diff --git a/fairmq/plugins/control/Control.cxx b/fairmq/plugins/control/Control.cxx index 7e690336..3e6e76ff 100644 --- a/fairmq/plugins/control/Control.cxx +++ b/fairmq/plugins/control/Control.cxx @@ -8,6 +8,8 @@ #include "Control.h" +#include + #include #include #include // catching system signals @@ -17,7 +19,6 @@ #include #include // for the interactive mode -#include // for the interactive mode using namespace std; @@ -119,32 +120,6 @@ auto ControlPluginProgramOptions() -> Plugin::ProgOptions return pluginOptions; } -struct TerminalConfig -{ - TerminalConfig() - { - termios t; - tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure - t.c_lflag &= ~ICANON; // disable canonical input - t.c_lflag &= ~ECHO; // do not echo input chars - tcsetattr(STDIN_FILENO, TCSANOW, &t); // apply the new settings - } - - TerminalConfig(const TerminalConfig&) = delete; - TerminalConfig(TerminalConfig&&) = delete; - TerminalConfig& operator=(const TerminalConfig&) = delete; - TerminalConfig& operator=(TerminalConfig&&) = delete; - - ~TerminalConfig() - { - termios t; - tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure - t.c_lflag |= ICANON; // re-enable canonical input - t.c_lflag |= ECHO; // echo input chars - tcsetattr(STDIN_FILENO, TCSANOW, &t); // apply the new settings - } -}; - auto Control::InteractiveMode() -> void try { RunStartupSequence(); @@ -155,7 +130,7 @@ try { cinfd[0].events = POLLIN; cinfd[0].revents = 0; - TerminalConfig tconfig; + tools::NonCanonicalInput nci; bool color = GetProperty("color"); diff --git a/fairmq/shmem/Monitor.cxx b/fairmq/shmem/Monitor.cxx index 6146bdbe..ccc6bfc9 100644 --- a/fairmq/shmem/Monitor.cxx +++ b/fairmq/shmem/Monitor.cxx @@ -9,6 +9,7 @@ #include "Monitor.h" #include "Common.h" +#include #include #include @@ -26,7 +27,6 @@ #include #include -#include #include #if FAIRMQ_HAS_STD_FILESYSTEM @@ -49,32 +49,6 @@ namespace namespace fair::mq::shmem { -struct TerminalConfig -{ - TerminalConfig() - { - termios t; - tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure - t.c_lflag &= ~ICANON; // disable canonical input - t.c_lflag &= ~ECHO; // do not echo input chars - tcsetattr(STDIN_FILENO, TCSANOW, &t); // apply the new settings - } - - TerminalConfig(const TerminalConfig&) = delete; - TerminalConfig(TerminalConfig&&) = delete; - TerminalConfig& operator=(const TerminalConfig&) = delete; - TerminalConfig& operator=(TerminalConfig&&) = delete; - - ~TerminalConfig() - { - termios t; - tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure - t.c_lflag |= ICANON; // re-enable canonical input - t.c_lflag |= ECHO; // echo input chars - tcsetattr(STDIN_FILENO, TCSANOW, &t); // apply the new settings - } -}; - void signalHandler(int signal) { gSignalStatus = signal; @@ -339,7 +313,7 @@ void Monitor::Interactive() cinfd[0].fd = fileno(stdin); cinfd[0].events = POLLIN; - TerminalConfig tcfg; + tools::NonCanonicalInput nci; LOG(info) << "\n"; PrintHelp(); diff --git a/fairmq/tools/IO.h b/fairmq/tools/IO.h new file mode 100644 index 00000000..466ee468 --- /dev/null +++ b/fairmq/tools/IO.h @@ -0,0 +1,49 @@ +/******************************************************************************** + * Copyright (C) 2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * * + * This software is distributed under the terms of the * + * GNU Lesser General Public Licence (LGPL) version 3, * + * copied verbatim in the file "LICENSE" * + ********************************************************************************/ + +#ifndef FAIR_MQ_TOOLS_IO_H +#define FAIR_MQ_TOOLS_IO_H + +#include +#include + +namespace fair::mq::tools +{ + +/// Enables noncanonical mode for the input terminal for the lifetime of this object. +/// (input becomes available to the application without user having to type a line-delimiter) +/// Read `man termios` for more details +struct NonCanonicalInput +{ + NonCanonicalInput() + { + termios t; + tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure + t.c_lflag &= ~ICANON; // disable canonical input + t.c_lflag &= ~ECHO; // do not echo input chars + tcsetattr(STDIN_FILENO, TCSANOW, &t); // apply the new settings + } + + NonCanonicalInput(const NonCanonicalInput&) = delete; + NonCanonicalInput(NonCanonicalInput&&) = delete; + NonCanonicalInput& operator=(const NonCanonicalInput&) = delete; + NonCanonicalInput& operator=(NonCanonicalInput&&) = delete; + + ~NonCanonicalInput() + { + termios t; + tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure + t.c_lflag |= ICANON; // re-enable canonical input + t.c_lflag |= ECHO; // echo input chars + tcsetattr(STDIN_FILENO, TCSANOW, &t); // apply the new settings + } +}; + +} // namespace fair::mq::tools + +#endif /* FAIR_MQ_TOOLS_IO_H */