xaizek / pipedial (License: GPLv3 only) (since 2019-01-08)
One more tool for selecting something in console.
<root> / src / utils.cpp (a564f9cbb5480248c1143e7af659c0e4e33969cc) (2,869B) (mode 100644) [raw]
// pipedial -- terminal element picker
// Copyright (C) 2019 xaizek <xaizek@posteo.net>
//
// This file is part of pipedial.
//
// pipedial is free software: you can redistribute it and/or modify
// it under the terms of version 3 of the GNU General Public License
// as published by the Free Software Foundation.
//
// pipedial is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with pipedial.  If not, see <https://www.gnu.org/licenses/>.

#include "utils.hpp"

#include <fcntl.h>
#include <unistd.h>

#include <cstddef>
#include <cstdio>

#include <iostream>

static int openTty();

std::unique_ptr<FILE, int (*)(FILE *)>
reopenTermStdout()
{
    std::unique_ptr<FILE, int (*)(FILE *)> errorValue(nullptr,
                                                      [](FILE *) { return 0; });

    int outfd = dup(STDOUT_FILENO);
    if (outfd == -1) {
        std::cerr << "Failed to store original output stream.\n";
        return errorValue;
    }

    std::unique_ptr<FILE, int (*)(FILE *)> fp(fdopen(outfd, "w"), &fclose);
    if (fp == nullptr) {
        close(outfd);
        std::cerr << "Failed to store original output stream.\n";
        return errorValue;
    }

    int ttyfd = openTty();
    if (ttyfd == -1) {
        std::cerr << "Failed to open terminal for output\n";
        return errorValue;
    }
    if (dup2(ttyfd, STDOUT_FILENO) == -1) {
        close(ttyfd);
        std::cerr << "Failed to setup terminal as standard output stream.";
        return errorValue;
    }

    close(ttyfd);
    return fp;
}

int
reopenTermStdin()
{
    if (close(STDIN_FILENO)) {
        std::cerr << "Failed to close original input stream.\n";
        return 1;
    }

    int ttyfd = openTty();
    if (ttyfd != STDIN_FILENO) {
        if(ttyfd != -1) {
            close(ttyfd);
        }

        std::cerr << "Failed to open terminal for input.\n";
        return 1;
    }

    return 0;
}

// Opens controlling terminal doing its best at guessing its name.  This might
// not work if all the standard streams are redirected, in which case `/dev/tty`
// is used.  Returns opened file descriptor or -1 on error.
static int
openTty()
{
    int fd = -1;

    // XXX: we might be better off duplicating descriptor if possible.

    if (isatty(STDIN_FILENO)) {
        fd = open(ttyname(STDIN_FILENO), O_RDWR);
    }
    if (fd == -1 && isatty(STDOUT_FILENO)) {
        fd = open(ttyname(STDOUT_FILENO), O_RDWR);
    }
    if (fd == -1 && isatty(STDERR_FILENO)) {
        fd = open(ttyname(STDERR_FILENO), O_RDWR);
    }

    if (fd == -1) {
        fd = open("/dev/tty", O_RDWR);
    }

    return fd;
}
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/pipedial

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/pipedial

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master