|
1 #include <cstdlib> |
|
2 #include <fstream> |
|
3 #include <memory> |
|
4 #include <regex> |
|
5 #include <algorithm> |
|
6 #include <unistd.h> |
|
7 |
|
8 #include <RelationalWriter.h> |
|
9 #include <RelpipeWriterException.h> |
|
10 #include <Factory.h> |
|
11 #include <TypeId.h> |
|
12 |
|
13 #include "CLI.h" |
|
14 |
|
15 using namespace relpipe::cli; |
|
16 using namespace relpipe::writer; |
|
17 |
|
18 void processDataStream(ostream &output, istream* input) { |
|
19 wregex devicePattern = wregex(L"(LABEL|UUID)=(.*)"); |
|
20 wregex linePattern = wregex(L"^([^\\s#]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+(\\d+)\\s+(\\d+)\\s*$"); |
|
21 wstring_convert < codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
|
22 |
|
23 std::shared_ptr<RelationalWriter> writer(Factory::create(output)); |
|
24 |
|
25 writer->startRelation(L"fstab",{ |
|
26 {L"scheme", TypeId::STRING}, |
|
27 {L"device", TypeId::STRING}, |
|
28 {L"mount_point", TypeId::STRING}, |
|
29 {L"type", TypeId::STRING}, |
|
30 // {L"types", TypeId::STRING}, // TODO: array |
|
31 {L"options", TypeId::STRING}, // TODO: array |
|
32 {L"dump", TypeId::INTEGER}, |
|
33 {L"pass", TypeId::INTEGER} |
|
34 }, true); |
|
35 |
|
36 string lineBytes; |
|
37 while (getline(*input, lineBytes)) { |
|
38 |
|
39 wstring line = convertor.from_bytes(lineBytes); |
|
40 wsmatch lineMatch; |
|
41 if (regex_search(line, lineMatch, linePattern) && lineMatch.size() > 0) { |
|
42 int g = 1; |
|
43 wstring device = lineMatch[g++]; |
|
44 wstring mountPoint = lineMatch[g++]; |
|
45 wstring type = lineMatch[g++]; |
|
46 wstring options = lineMatch[g++]; |
|
47 wstring dump = lineMatch[g++]; |
|
48 wstring pass = lineMatch[g++]; |
|
49 |
|
50 wsmatch deviceMatch; |
|
51 if (regex_search(device, deviceMatch, devicePattern)) { |
|
52 writer->writeAttribute(deviceMatch[1]); |
|
53 writer->writeAttribute(deviceMatch[2]); |
|
54 } else { |
|
55 writer->writeAttribute(L""); // TODO: null (requires bitmap) |
|
56 writer->writeAttribute(device); |
|
57 } |
|
58 |
|
59 if (mountPoint == L"none") mountPoint = L""; // TODO: null (requires bitmap) |
|
60 writer->writeAttribute(mountPoint); |
|
61 writer->writeAttribute(type); |
|
62 writer->writeAttribute(options); |
|
63 writer->writeAttribute(dump); |
|
64 writer->writeAttribute(pass); |
|
65 } |
|
66 } |
|
67 |
|
68 } |
|
69 |
|
70 int main(int argc, char** argv) { |
|
71 setlocale(LC_ALL, ""); |
|
72 //CLI cli(argc, argv); |
|
73 |
|
74 int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; |
|
75 |
|
76 try { |
|
77 if (isatty(fileno(stdin))) { |
|
78 ifstream s("/etc/fstab"); |
|
79 processDataStream(cout, &s); |
|
80 } else { |
|
81 processDataStream(cout, &cin); |
|
82 } |
|
83 resultCode = CLI::EXIT_CODE_SUCCESS; |
|
84 |
|
85 } catch (RelpipeWriterException e) { |
|
86 fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessge().c_str()); |
|
87 fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); |
|
88 resultCode = CLI::EXIT_CODE_DATA_ERROR; |
|
89 } |
|
90 |
|
91 return resultCode; |
|
92 } |