Skip to content

Source · fairmq/tools/

IO.hpublic header

Defines struct fair::mq::tools::NonCanonicalInput.

fairmq/tools/IO.h49 lines · C++API: NonCanonicalInputGitHub ↗
1/********************************************************************************2 *    Copyright (C) 2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH    *3 *                                                                              *4 *              This software is distributed under the terms of the             *5 *              GNU Lesser General Public Licence (LGPL) version 3,             *6 *                  copied verbatim in the file "LICENSE"                       *7 ********************************************************************************/8 9#ifndef FAIR_MQ_TOOLS_IO_H10#define FAIR_MQ_TOOLS_IO_H11 12#include <termios.h>13#include <unistd.h>14 15namespace fair::mq::tools16{17 18/// Enables noncanonical mode for the input terminal for the lifetime of this object.19/// (input becomes available to the application without user having to type a line-delimiter)20/// Read `man termios` for more details21struct NonCanonicalInput22{23    NonCanonicalInput()24    {25        termios t;26        tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure27        t.c_lflag &= ~ICANON; // disable canonical input28        t.c_lflag &= ~ECHO; // do not echo input chars29        tcsetattr(STDIN_FILENO, TCSANOW, &t); // apply the new settings30    }31 32    NonCanonicalInput(const NonCanonicalInput&) = delete;33    NonCanonicalInput(NonCanonicalInput&&) = delete;34    NonCanonicalInput& operator=(const NonCanonicalInput&) = delete;35    NonCanonicalInput& operator=(NonCanonicalInput&&) = delete;36 37    ~NonCanonicalInput()38    {39        termios t;40        tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure41        t.c_lflag |= ICANON; // re-enable canonical input42        t.c_lflag |= ECHO; // echo input chars43        tcsetattr(STDIN_FILENO, TCSANOW, &t); // apply the new settings44    }45};46 47} // namespace fair::mq::tools48 49#endif /* FAIR_MQ_TOOLS_IO_H */