src/AwkHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 05 May 2019 11:54:53 +0200
branchv_0
changeset 4 050ec7c1f2e7
parent 3 e086ae6a19c3
child 5 86de8e6ab231
permissions -rw-r--r--
remove debug messages

/**
 * 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);
			__pid_t waitResult1 = wait(NULL);
			__pid_t waitResult2 = wait(NULL);
			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);


			execlp("awk", "awk", "{print \"AWK says: line \" NR \" = \" $0;}", nullptr);

		} else {
			// Parent process
			closeOrThrow(awkInputReaderFD);
			closeOrThrow(awkOutputWriterFD);

			__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);

				__gnu_cxx::stdio_filebuf<wchar_t> awkOutputReaderBuffer(awkOutputReaderFD, std::ios::in);
				std::wistream awkOutputReader(&awkOutputReaderBuffer);

				relationalWriter->startRelation(L"writer_debug",{
					{L"message", writer::TypeId::STRING},
				}, true);

				for (wchar_t ch = awkOutputReader.get(); awkOutputReader.good(); ch = awkOutputReader.get()) {
					relationalWriter->writeAttribute(string_t(1, ch));
				}

				closeOrThrow(awkOutputReaderFD);
				exit(0);
			} else {
				// Parent process
				closeOrThrow(awkOutputReaderFD);
			}
		}

	}

	void attribute(const string_t& value) override {
		dprintf(awkInputWriterFD, "attribute!\n");
	}

	void endOfPipe() {
		cleanUp();
	}

};

}
}
}