feat: add tool for noncanonical input

This commit is contained in:
Alexey Rybalchenko 2021-10-02 11:43:41 +02:00
parent fbf11d4547
commit 8541fd553e
4 changed files with 55 additions and 56 deletions

View File

@ -31,6 +31,7 @@ if(BUILD_FAIRMQ OR BUILD_SDK)
set(TOOLS_PUBLIC_HEADER_FILES set(TOOLS_PUBLIC_HEADER_FILES
tools/CppSTL.h tools/CppSTL.h
tools/InstanceLimit.h tools/InstanceLimit.h
tools/IO.h
tools/Network.h tools/Network.h
tools/Process.h tools/Process.h
tools/RateLimit.h tools/RateLimit.h

View File

@ -8,6 +8,8 @@
#include "Control.h" #include "Control.h"
#include <fairmq/tools/IO.h>
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
#include <csignal> // catching system signals #include <csignal> // catching system signals
@ -17,7 +19,6 @@
#include <thread> #include <thread>
#include <poll.h> // for the interactive mode #include <poll.h> // for the interactive mode
#include <termios.h> // for the interactive mode
using namespace std; using namespace std;
@ -119,32 +120,6 @@ auto ControlPluginProgramOptions() -> Plugin::ProgOptions
return pluginOptions; 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 auto Control::InteractiveMode() -> void
try { try {
RunStartupSequence(); RunStartupSequence();
@ -155,7 +130,7 @@ try {
cinfd[0].events = POLLIN; cinfd[0].events = POLLIN;
cinfd[0].revents = 0; cinfd[0].revents = 0;
TerminalConfig tconfig; tools::NonCanonicalInput nci;
bool color = GetProperty<bool>("color"); bool color = GetProperty<bool>("color");

View File

@ -9,6 +9,7 @@
#include "Monitor.h" #include "Monitor.h"
#include "Common.h" #include "Common.h"
#include <fairmq/tools/IO.h>
#include <fairmq/tools/Strings.h> #include <fairmq/tools/Strings.h>
#include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/managed_shared_memory.hpp>
@ -26,7 +27,6 @@
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
#include <termios.h>
#include <poll.h> #include <poll.h>
#if FAIRMQ_HAS_STD_FILESYSTEM #if FAIRMQ_HAS_STD_FILESYSTEM
@ -49,32 +49,6 @@ namespace
namespace fair::mq::shmem 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) void signalHandler(int signal)
{ {
gSignalStatus = signal; gSignalStatus = signal;
@ -339,7 +313,7 @@ void Monitor::Interactive()
cinfd[0].fd = fileno(stdin); cinfd[0].fd = fileno(stdin);
cinfd[0].events = POLLIN; cinfd[0].events = POLLIN;
TerminalConfig tcfg; tools::NonCanonicalInput nci;
LOG(info) << "\n"; LOG(info) << "\n";
PrintHelp(); PrintHelp();

49
fairmq/tools/IO.h Normal file
View File

@ -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 <termios.h>
#include <unistd.h>
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 */