src/relpipe-in-posixmq.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 30 Apr 2022 23:19:08 +0200
branchv_0
changeset 7 12e975f807ed
parent 3 b71fc3b5e56b
permissions -rw-r--r--
add signal handler + optional stop
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
2
1eef3d465863 update copyright year
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#include <cstdlib>
7
12e975f807ed add signal handler + optional stop
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    18
#include <csignal>
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <memory>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <regex>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <algorithm>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <unistd.h>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <relpipe/writer/RelationalWriter.h>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <relpipe/writer/RelpipeWriterException.h>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/writer/AttributeMetadata.h>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/writer/Factory.h>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/writer/TypeId.h>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/cli/CLI.h>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include "PosixMQCommand.h"
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include "CLIParser.h"
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
#include "Configuration.h"
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
using namespace relpipe::cli;
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
using namespace relpipe::writer;
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
using namespace relpipe::in::posixmq;
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
7
12e975f807ed add signal handler + optional stop
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    41
static std::shared_ptr<PosixMQCommand> command = nullptr;
12e975f807ed add signal handler + optional stop
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    42
12e975f807ed add signal handler + optional stop
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    43
void finish(int sig) {
12e975f807ed add signal handler + optional stop
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    44
	if (command) command->finish(sig);
12e975f807ed add signal handler + optional stop
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    45
}
12e975f807ed add signal handler + optional stop
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    46
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
int main(int argc, char** argv) {
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	setlocale(LC_ALL, "");
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	CLI::untieStdIO();
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	CLI cli(argc, argv);
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	try {
7
12e975f807ed add signal handler + optional stop
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    55
		signal(SIGHUP, finish);
12e975f807ed add signal handler + optional stop
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    56
		signal(SIGINT, finish);
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		CLIParser cliParser;
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		Configuration configuration = cliParser.parse(cli.arguments());
7
12e975f807ed add signal handler + optional stop
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    59
		command.reset(new PosixMQCommand());
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		std::shared_ptr<RelationalWriter> writer(Factory::create(std::cout));
7
12e975f807ed add signal handler + optional stop
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    61
		writer->setBufferingMode(BufferingMode::ENVIRONMENT, BufferingMode::RECORD);
12e975f807ed add signal handler + optional stop
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    62
		command->process(writer, configuration);
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		resultCode = CLI::EXIT_CODE_SUCCESS;
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	} catch (RelpipeCLIException e) {
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessage().c_str());
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
		resultCode = e.getExitCode();
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	} catch (RelpipeWriterException e) {
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessage().c_str());
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	}
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	return resultCode;
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
}