# HG changeset patch # User František Kučera # Date 1620573630 -7200 # Node ID f71eb7aefd257093da9309c739591ad0179f77ff # Parent bbebee4044d994d2b322e5769ba73a2c7209577e add CLIParser and Configuration: --relation option diff -r bbebee4044d9 -r f71eb7aefd25 bash-completion.sh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bash-completion.sh Sun May 09 17:20:30 2021 +0200 @@ -0,0 +1,34 @@ +# Relational pipes +# Copyright © 2021 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, version 3 of the License. +# +# 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 . + +_relpipe_in_fstab_completion() { + local w0 w1 w2 w3 + + COMPREPLY=() + w0=${COMP_WORDS[COMP_CWORD]} + w1=${COMP_WORDS[COMP_CWORD-1]} + w2=${COMP_WORDS[COMP_CWORD-2]} + w3=${COMP_WORDS[COMP_CWORD-3]} + + if [[ "$w1" == "--relation" && "x$w0" == "x" ]]; then COMPREPLY=("''") + else + OPTIONS=( + "--relation" + ) + COMPREPLY=($(compgen -W "${OPTIONS[*]}" -- "$w0")) + fi +} + +complete -F _relpipe_in_fstab_completion relpipe-in-fstab diff -r bbebee4044d9 -r f71eb7aefd25 nbproject/configurations.xml --- a/nbproject/configurations.xml Sat Oct 24 00:08:17 2020 +0200 +++ b/nbproject/configurations.xml Sun May 09 17:20:30 2021 +0200 @@ -42,6 +42,7 @@ + FstabCommand.cpp relpipe-in-fstab.cpp @@ -91,7 +92,11 @@ true - + + + + + diff -r bbebee4044d9 -r f71eb7aefd25 nbproject/project.xml --- a/nbproject/project.xml Sat Oct 24 00:08:17 2020 +0200 +++ b/nbproject/project.xml Sun May 09 17:20:30 2021 +0200 @@ -6,7 +6,7 @@ relpipe-in-fstab.cpp cpp - + h UTF-8 diff -r bbebee4044d9 -r f71eb7aefd25 src/CLIParser.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/CLIParser.h Sun May 09 17:20:30 2021 +0200 @@ -0,0 +1,66 @@ +/** + * Relational pipes + * Copyright © 2021 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, version 3 of the License. + * + * 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 . + */ +#pragma once + +#include +#include + +#include +#include +#include + +#include "Configuration.h" + +namespace relpipe { +namespace in { +namespace fstab { + +class CLIParser { +private: + + relpipe::writer::string_t readNext(const std::vector& arguments, int& i) { + if (i < arguments.size()) return arguments[i++]; + else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); + } + +public: + + static const relpipe::writer::string_t OPTION_RELATION; + + Configuration parse(const std::vector& arguments) { + Configuration c; + + for (int i = 0; i < arguments.size();) { + relpipe::writer::string_t option = readNext(arguments, i); + + if (option == OPTION_RELATION) { + c.relation = readNext(arguments, i); + } else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); + } + + return c; + } + + virtual ~CLIParser() { + } +}; + +const relpipe::writer::string_t CLIParser::OPTION_RELATION = L"--relation"; + +} +} +} diff -r bbebee4044d9 -r f71eb7aefd25 src/CMakeLists.txt --- a/src/CMakeLists.txt Sat Oct 24 00:08:17 2020 +0200 +++ b/src/CMakeLists.txt Sun May 09 17:20:30 2021 +0200 @@ -29,6 +29,7 @@ # Executable output: add_executable( ${EXECUTABLE_FILE} + FstabCommand.cpp relpipe-in-fstab.cpp ) diff -r bbebee4044d9 -r f71eb7aefd25 src/Configuration.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Configuration.h Sun May 09 17:20:30 2021 +0200 @@ -0,0 +1,39 @@ +/** + * Relational pipes + * Copyright © 2021 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, version 3 of the License. + * + * 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 . + */ +#pragma once + +#include +#include + +#include + + +namespace relpipe { +namespace in { +namespace fstab { + +class Configuration { +public: + relpipe::writer::string_t relation = L"fstab"; + + virtual ~Configuration() { + } +}; + +} +} +} \ No newline at end of file diff -r bbebee4044d9 -r f71eb7aefd25 src/FstabCommand.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/FstabCommand.cpp Sun May 09 17:20:30 2021 +0200 @@ -0,0 +1,100 @@ +/** + * Relational pipes + * Copyright © 2018 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, version 3 of the License. + * + * 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 . + */ +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include "FstabCommand.h" + +using namespace std; +using namespace relpipe::cli; +using namespace relpipe::writer; + +namespace relpipe { +namespace in { +namespace fstab { + +using namespace relpipe::cli; +using namespace relpipe::writer; + +/** + * see https://hg.frantovo.cz/sql-api/file/tip/prototyp/prototyp.sql#l49 + */ +void FstabCommand::process(std::istream& input, std::shared_ptr writer, Configuration& configuration) { + wregex devicePattern = wregex(L"(LABEL|UUID)=(.*)"); + wregex linePattern = wregex(L"^([^\\s#]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+(\\d+)\\s+(\\d+)\\s*$"); + wstring_convert < codecvt_utf8> convertor; // TODO: support also other encodings. + + writer->startRelation(configuration.relation,{ + {L"scheme", TypeId::STRING}, + {L"device", TypeId::STRING}, + {L"mount_point", TypeId::STRING}, + {L"type", TypeId::STRING}, + // {L"types", TypeId::STRING}, // TODO: array + {L"options", TypeId::STRING}, // TODO: array + {L"dump", TypeId::INTEGER}, + {L"pass", TypeId::INTEGER} + }, true); + + string lineBytes; + while (getline(input, lineBytes)) { + + wstring line = convertor.from_bytes(lineBytes); + wsmatch lineMatch; + if (regex_search(line, lineMatch, linePattern) && lineMatch.size() > 0) { + int g = 1; + wstring device = lineMatch[g++]; + wstring mountPoint = lineMatch[g++]; + wstring type = lineMatch[g++]; + wstring options = lineMatch[g++]; + wstring dump = lineMatch[g++]; + wstring pass = lineMatch[g++]; + + wsmatch deviceMatch; + if (regex_search(device, deviceMatch, devicePattern)) { + writer->writeAttribute(deviceMatch[1]); + writer->writeAttribute(deviceMatch[2]); + } else { + writer->writeAttribute(L""); // TODO: null (requires bitmap) + writer->writeAttribute(device); + } + + if (mountPoint == L"none") mountPoint = L""; // TODO: null (requires bitmap) + writer->writeAttribute(mountPoint); + writer->writeAttribute(type); + writer->writeAttribute(options); + writer->writeAttribute(dump); + writer->writeAttribute(pass); + } + } +} + +} +} +} \ No newline at end of file diff -r bbebee4044d9 -r f71eb7aefd25 src/FstabCommand.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/FstabCommand.h Sun May 09 17:20:30 2021 +0200 @@ -0,0 +1,41 @@ +/** + * Relational pipes + * Copyright © 2021 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, version 3 of the License. + * + * 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 . + */ +#pragma once + +#include +#include +#include +#include + +#include + +#include "Configuration.h" + +namespace relpipe { +namespace in { +namespace fstab { + +class FstabCommand { +private: +public: + void process(std::istream& input, std::shared_ptr writer, Configuration& configuration); + +}; + +} +} +} diff -r bbebee4044d9 -r f71eb7aefd25 src/relpipe-in-fstab.cpp --- a/src/relpipe-in-fstab.cpp Sat Oct 24 00:08:17 2020 +0200 +++ b/src/relpipe-in-fstab.cpp Sun May 09 17:20:30 2021 +0200 @@ -28,72 +28,27 @@ #include +#include "CLIParser.h" +#include "Configuration.h" +#include "FstabCommand.h" + using namespace relpipe::cli; using namespace relpipe::writer; - -/** - * see https://hg.frantovo.cz/sql-api/file/tip/prototyp/prototyp.sql#l49 - */ -void processDataStream(ostream &output, istream* input) { - wregex devicePattern = wregex(L"(LABEL|UUID)=(.*)"); - wregex linePattern = wregex(L"^([^\\s#]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+(\\d+)\\s+(\\d+)\\s*$"); - wstring_convert < codecvt_utf8> convertor; // TODO: support also other encodings. - - std::shared_ptr writer(Factory::create(output)); - - writer->startRelation(L"fstab",{ - {L"scheme", TypeId::STRING}, - {L"device", TypeId::STRING}, - {L"mount_point", TypeId::STRING}, - {L"type", TypeId::STRING}, - // {L"types", TypeId::STRING}, // TODO: array - {L"options", TypeId::STRING}, // TODO: array - {L"dump", TypeId::INTEGER}, - {L"pass", TypeId::INTEGER} - }, true); - - string lineBytes; - while (getline(*input, lineBytes)) { - - wstring line = convertor.from_bytes(lineBytes); - wsmatch lineMatch; - if (regex_search(line, lineMatch, linePattern) && lineMatch.size() > 0) { - int g = 1; - wstring device = lineMatch[g++]; - wstring mountPoint = lineMatch[g++]; - wstring type = lineMatch[g++]; - wstring options = lineMatch[g++]; - wstring dump = lineMatch[g++]; - wstring pass = lineMatch[g++]; - - wsmatch deviceMatch; - if (regex_search(device, deviceMatch, devicePattern)) { - writer->writeAttribute(deviceMatch[1]); - writer->writeAttribute(deviceMatch[2]); - } else { - writer->writeAttribute(L""); // TODO: null (requires bitmap) - writer->writeAttribute(device); - } - - if (mountPoint == L"none") mountPoint = L""; // TODO: null (requires bitmap) - writer->writeAttribute(mountPoint); - writer->writeAttribute(type); - writer->writeAttribute(options); - writer->writeAttribute(dump); - writer->writeAttribute(pass); - } - } - -} +using namespace relpipe::in::fstab; int main(int argc, char** argv) { setlocale(LC_ALL, ""); CLI::untieStdIO(); - //CLI cli(argc, argv); + CLI cli(argc, argv); int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; try { + CLIParser cliParser; + Configuration configuration = cliParser.parse(cli.arguments()); + FstabCommand command; + std::shared_ptr writer(Factory::create(std::cout)); + if (isatty(fileno(stdin))) { /** * Our program is executed on TTY without input stream redirection → read from default file location. @@ -101,7 +56,7 @@ * (we don't expect that user is writing the content of fstab by hand on the terminal) */ ifstream s("/etc/fstab"); - processDataStream(cout, &s); + command.process(s, writer, configuration); } else { /** * Input stream comes from a file or is piped from other command → read it instead of default file. @@ -109,11 +64,16 @@ * or $ relpipe-in-fstab < /etc/fstab | … # without UUoC * or $ ssh example.com cat /etc/fstab | relpipe-in-fstab | … * or $ cat | relpipe-in-fstab | … # user writes the fstab content by hand + * or $ cat /etc/mtab | relpipe-in-fstab | relpipe-out-tabular | less -RSi # currently mounted filesystems */ - processDataStream(cout, &cin); + command.process(std::cin, writer, configuration); } + resultCode = CLI::EXIT_CODE_SUCCESS; - + } catch (RelpipeCLIException e) { + fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str()); + fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); + resultCode = e.getExitCode(); } catch (RelpipeWriterException e) { fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessge().c_str()); fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());