# HG changeset patch # User František Kučera # Date 1534021001 -7200 # Node ID 21ef3f6cd5e9505348abfe0124eacce5ecb360bd # Parent cac146f5345a3ce8d60ac2178d412e9b56927865 fstab logic ported from the prototype: FstabRelationalGenerator.h diff -r cac146f5345a -r 21ef3f6cd5e9 nbproject/Makefile-Debug.mk --- a/nbproject/Makefile-Debug.mk Sat Aug 11 22:16:13 2018 +0200 +++ b/nbproject/Makefile-Debug.mk Sat Aug 11 22:56:41 2018 +0200 @@ -35,7 +35,7 @@ # Object Files OBJECTFILES= \ - ${OBJECTDIR}/relpipe-in-cli.o + ${OBJECTDIR}/relpipe-in-fstab.o # C Compiler Flags @@ -62,10 +62,10 @@ ${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM} ${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/relpipe-in-fstab.cpp ${OBJECTFILES} ${LDLIBSOPTIONS} -${OBJECTDIR}/relpipe-in-cli.o: relpipe-in-cli.cpp +${OBJECTDIR}/relpipe-in-fstab.o: relpipe-in-fstab.cpp ${MKDIR} -p ${OBJECTDIR} ${RM} "$@.d" - $(COMPILE.cc) -g `pkg-config --cflags relpipe-lib-writer.cpp` -std=c++14 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/relpipe-in-cli.o relpipe-in-cli.cpp + $(COMPILE.cc) -g `pkg-config --cflags relpipe-lib-writer.cpp` -std=c++14 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/relpipe-in-fstab.o relpipe-in-fstab.cpp # Subprojects .build-subprojects: diff -r cac146f5345a -r 21ef3f6cd5e9 nbproject/Makefile-Release.mk --- a/nbproject/Makefile-Release.mk Sat Aug 11 22:16:13 2018 +0200 +++ b/nbproject/Makefile-Release.mk Sat Aug 11 22:56:41 2018 +0200 @@ -35,7 +35,7 @@ # Object Files OBJECTFILES= \ - ${OBJECTDIR}/relpipe-in-cli.o + ${OBJECTDIR}/relpipe-in-fstab.o # C Compiler Flags @@ -62,10 +62,10 @@ ${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM} ${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/relpipe-in-fstab.cpp ${OBJECTFILES} ${LDLIBSOPTIONS} -${OBJECTDIR}/relpipe-in-cli.o: relpipe-in-cli.cpp +${OBJECTDIR}/relpipe-in-fstab.o: relpipe-in-fstab.cpp ${MKDIR} -p ${OBJECTDIR} ${RM} "$@.d" - $(COMPILE.cc) -O2 `pkg-config --cflags relpipe-lib-writer.cpp` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/relpipe-in-cli.o relpipe-in-cli.cpp + $(COMPILE.cc) -O2 `pkg-config --cflags relpipe-lib-writer.cpp` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/relpipe-in-fstab.o relpipe-in-fstab.cpp # Subprojects .build-subprojects: diff -r cac146f5345a -r 21ef3f6cd5e9 nbproject/configurations.xml --- a/nbproject/configurations.xml Sat Aug 11 22:16:13 2018 +0200 +++ b/nbproject/configurations.xml Sat Aug 11 22:56:41 2018 +0200 @@ -13,7 +13,7 @@ - relpipe-in-cli.cpp + relpipe-in-fstab.cpp - + @@ -78,7 +78,7 @@ - + diff -r cac146f5345a -r 21ef3f6cd5e9 relpipe-in-cli.cpp --- a/relpipe-in-cli.cpp Sat Aug 11 22:16:13 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,30 +0,0 @@ -#include -#include - -#include -#include -#include -#include - -#include "CLI.h" - -using namespace relpipe::cli; -using namespace relpipe::writer; - -int main(int argc, char** argv) { - setlocale(LC_ALL, ""); - CLI cli(argc, argv); - - int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; - - try { - fwprintf(stderr, L"TODO: implement fstab reading\n"); - - } 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()); - resultCode = CLI::EXIT_CODE_DATA_ERROR; - } - - return resultCode; -} diff -r cac146f5345a -r 21ef3f6cd5e9 relpipe-in-fstab.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/relpipe-in-fstab.cpp Sat Aug 11 22:56:41 2018 +0200 @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "CLI.h" + +using namespace relpipe::cli; +using namespace relpipe::writer; + +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); + } + } + +} + +int main(int argc, char** argv) { + setlocale(LC_ALL, ""); + //CLI cli(argc, argv); + + int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; + + try { + if (isatty(fileno(stdin))) { + ifstream s("/etc/fstab"); + processDataStream(cout, &s); + } else { + processDataStream(cout, &cin); + } + resultCode = CLI::EXIT_CODE_SUCCESS; + + } 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()); + resultCode = CLI::EXIT_CODE_DATA_ERROR; + } + + return resultCode; +}