/**
* Relational pipes
* Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <memory>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <locale>
#include <codecvt>
#include <regex>
#include <unistd.h>
#include <wait.h>
#include <ext/stdio_filebuf.h>
#include <relpipe/reader/typedefs.h>
#include <relpipe/reader/TypeId.h>
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
#include <relpipe/reader/handlers/AttributeMetadata.h>
#include <relpipe/writer/Factory.h>
#include <relpipe/cli/RelpipeCLIException.h>
#include "Configuration.h"
namespace relpipe {
namespace tr {
namespace awk {
using namespace std;
using namespace relpipe;
using namespace relpipe::reader;
using namespace relpipe::reader::handlers;
class AwkHandler : public RelationalReaderStringHandler {
private:
Configuration configuration;
writer::RelationalWriter* relationalWriter;
int awkInputWriterFD = -1;
void createPipe(int& readerFD, int& writerFD) {
int fds[2];
int result = pipe(fds);
readerFD = fds[0];
writerFD = fds[1];
if (result < 0) throw cli::RelpipeCLIException(L"Unable to create a pipe.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
}
void redirectFD(int oldfd, int newfd) {
int result = dup2(oldfd, newfd);
if (result < 0) throw cli::RelpipeCLIException(L"Unable redirect FD.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
}
void closeOrThrow(int fd) {
int error = close(fd);
if (error) throw cli::RelpipeCLIException(L"Unable to close FD: " + to_wstring(fd) + L" from PID: " + to_wstring(getpid()), cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
}
void cleanUp() {
if (awkInputWriterFD >= 0) {
closeOrThrow(awkInputWriterFD);
fwprintf(stderr, L"writing done and closed\n");
__pid_t waitResult1 = wait(NULL);
fwprintf(stderr, L"wait 1 done: %d\n", waitResult1);
__pid_t waitResult2 = wait(NULL);
fwprintf(stderr, L"wait 2 done: %d\n", waitResult2);
awkInputWriterFD = -1;
}
}
public:
AwkHandler(writer::RelationalWriter* relationalWriter, Configuration& configuration) : relationalWriter(relationalWriter), configuration(configuration) {
}
void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
cleanUp();
int awkInputReaderFD;
int awkOutputReaderFD;
int awkOutputWriterFD;
createPipe(awkInputReaderFD, awkInputWriterFD);
createPipe(awkOutputReaderFD, awkOutputWriterFD);
__pid_t awkPid = fork();
if (awkPid < 0) {
throw cli::RelpipeCLIException(L"Unable to fork AWK process.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
} else if (awkPid == 0) {
// AWK child process
closeOrThrow(awkInputWriterFD);
closeOrThrow(awkOutputReaderFD);
redirectFD(awkInputReaderFD, STDIN_FILENO);
redirectFD(awkOutputWriterFD, STDOUT_FILENO);
fwprintf(stderr, L"I am child AWK with PID: %d\n", getpid());
execlp("awk", "awk", "{print \"AWK says: line \" NR \" = \" $0;}", nullptr);
} else {
// Parent process
closeOrThrow(awkInputReaderFD);
closeOrThrow(awkOutputWriterFD);
fwprintf(stderr, L"Forked AWK has PID: %d\n", awkPid);
__pid_t writerPid = fork();
if (writerPid < 0) {
throw cli::RelpipeCLIException(L"Unable to fork Writer process.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
} else if (writerPid == 0) {
// Writer child process
closeOrThrow(awkInputWriterFD);
fwprintf(stderr, L"I am child Writer with PID: %d\n", getpid());
__gnu_cxx::stdio_filebuf<char> awkOutputReaderBuffer(awkOutputReaderFD, std::ios::in);
std::istream awkOutputReader(&awkOutputReaderBuffer);
std::cout << "--- PIPE start --------" << std::endl;
for (char ch = awkOutputReader.get(); awkOutputReader.good(); ch = awkOutputReader.get()) {
std::cout << ch;
}
std::cout << "--- PIPE end ----------" << std::endl;
closeOrThrow(awkOutputReaderFD);
exit(0);
} else {
// Parent process
closeOrThrow(awkOutputReaderFD);
fwprintf(stderr, L"Forked Writer has PID: %d\n", writerPid);
dprintf(awkInputWriterFD, "hello world :-)\n");
//closeOrThrow(awkInputWriterFD);
/*
__gnu_cxx::stdio_filebuf<char> awkInputWriterBuffer(awkInputWriterFD, std::ios::out);
std::ostream awkInputWriter(&awkInputWriterBuffer);
awkInputWriter << "hello world :-)" << std::endl;
awkInputWriter.flush();
closeOrThrow(awkInputWriterFD);
*/
}
}
}
void attribute(const string_t& value) override {
dprintf(awkInputWriterFD, "attribute!\n");
}
void endOfPipe() {
cleanUp();
}
};
}
}
}