|
1 /** |
|
2 * Relational pipes |
|
3 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 #include <cstdlib> |
|
19 #include <fstream> |
|
20 #include <memory> |
|
21 #include <regex> |
|
22 #include <algorithm> |
|
23 #include <unistd.h> |
|
24 |
|
25 #include <relpipe/writer/RelationalWriter.h> |
|
26 #include <relpipe/writer/RelpipeWriterException.h> |
|
27 #include <relpipe/writer/Factory.h> |
|
28 #include <relpipe/writer/TypeId.h> |
|
29 |
|
30 #include <relpipe/cli/CLI.h> |
|
31 |
|
32 using namespace relpipe::cli; |
|
33 using namespace relpipe::writer; |
|
34 |
|
35 /** |
|
36 * see https://hg.frantovo.cz/sql-api/file/tip/prototyp/prototyp.sql#l49 |
|
37 */ |
|
38 void processDataStream(ostream &output, istream* input) { |
|
39 wregex devicePattern = wregex(L"(LABEL|UUID)=(.*)"); |
|
40 wregex linePattern = wregex(L"^([^\\s#]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+(\\d+)\\s+(\\d+)\\s*$"); |
|
41 wstring_convert < codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
|
42 |
|
43 std::shared_ptr<RelationalWriter> writer(Factory::create(output)); |
|
44 |
|
45 writer->startRelation(L"fstab",{ |
|
46 {L"scheme", TypeId::STRING}, |
|
47 {L"device", TypeId::STRING}, |
|
48 {L"mount_point", TypeId::STRING}, |
|
49 {L"type", TypeId::STRING}, |
|
50 // {L"types", TypeId::STRING}, // TODO: array |
|
51 {L"options", TypeId::STRING}, // TODO: array |
|
52 {L"dump", TypeId::INTEGER}, |
|
53 {L"pass", TypeId::INTEGER} |
|
54 }, true); |
|
55 |
|
56 string lineBytes; |
|
57 while (getline(*input, lineBytes)) { |
|
58 |
|
59 wstring line = convertor.from_bytes(lineBytes); |
|
60 wsmatch lineMatch; |
|
61 if (regex_search(line, lineMatch, linePattern) && lineMatch.size() > 0) { |
|
62 int g = 1; |
|
63 wstring device = lineMatch[g++]; |
|
64 wstring mountPoint = lineMatch[g++]; |
|
65 wstring type = lineMatch[g++]; |
|
66 wstring options = lineMatch[g++]; |
|
67 wstring dump = lineMatch[g++]; |
|
68 wstring pass = lineMatch[g++]; |
|
69 |
|
70 wsmatch deviceMatch; |
|
71 if (regex_search(device, deviceMatch, devicePattern)) { |
|
72 writer->writeAttribute(deviceMatch[1]); |
|
73 writer->writeAttribute(deviceMatch[2]); |
|
74 } else { |
|
75 writer->writeAttribute(L""); // TODO: null (requires bitmap) |
|
76 writer->writeAttribute(device); |
|
77 } |
|
78 |
|
79 if (mountPoint == L"none") mountPoint = L""; // TODO: null (requires bitmap) |
|
80 writer->writeAttribute(mountPoint); |
|
81 writer->writeAttribute(type); |
|
82 writer->writeAttribute(options); |
|
83 writer->writeAttribute(dump); |
|
84 writer->writeAttribute(pass); |
|
85 } |
|
86 } |
|
87 |
|
88 } |
|
89 |
|
90 int main(int argc, char** argv) { |
|
91 setlocale(LC_ALL, ""); |
|
92 CLI::untieStdIO(); |
|
93 //CLI cli(argc, argv); |
|
94 |
|
95 int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; |
|
96 |
|
97 try { |
|
98 if (isatty(fileno(stdin))) { |
|
99 /** |
|
100 * Our program is executed on TTY without input stream redirection → read from default file location. |
|
101 * e.g. $ relpipe-in-fstab | … |
|
102 * (we don't expect that user is writing the content of fstab by hand on the terminal) |
|
103 */ |
|
104 ifstream s("/etc/fstab"); |
|
105 processDataStream(cout, &s); |
|
106 } else { |
|
107 /** |
|
108 * Input stream comes from a file or is piped from other command → read it instead of default file. |
|
109 * e.g. $ cat /etc/fstab | grep '/mnt/' | relpipe-in-fstab | … |
|
110 * or $ relpipe-in-fstab < /etc/fstab | … # without UUoC |
|
111 * or $ ssh example.com cat /etc/fstab | relpipe-in-fstab | … |
|
112 * or $ cat | relpipe-in-fstab | … # user writes the fstab content by hand |
|
113 */ |
|
114 processDataStream(cout, &cin); |
|
115 } |
|
116 resultCode = CLI::EXIT_CODE_SUCCESS; |
|
117 |
|
118 } catch (RelpipeWriterException e) { |
|
119 fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessge().c_str()); |
|
120 fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); |
|
121 resultCode = CLI::EXIT_CODE_DATA_ERROR; |
|
122 } |
|
123 |
|
124 return resultCode; |
|
125 } |