author | František Kučera <franta-hg@frantovo.cz> |
Sun, 05 May 2019 12:37:43 +0200 | |
branch | v_0 |
changeset 5 | 86de8e6ab231 |
parent 4 | 050ec7c1f2e7 |
child 6 | efa96f51b308 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2019 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 |
#pragma once |
|
19 |
||
20 |
#include <memory> |
|
21 |
#include <string> |
|
22 |
#include <vector> |
|
23 |
#include <iostream> |
|
24 |
#include <sstream> |
|
25 |
#include <locale> |
|
26 |
#include <codecvt> |
|
27 |
#include <regex> |
|
28 |
||
1 | 29 |
#include <unistd.h> |
30 |
#include <wait.h> |
|
31 |
#include <ext/stdio_filebuf.h> |
|
32 |
||
0 | 33 |
#include <relpipe/reader/typedefs.h> |
34 |
#include <relpipe/reader/TypeId.h> |
|
35 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
36 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
37 |
||
38 |
#include <relpipe/writer/Factory.h> |
|
39 |
||
40 |
#include <relpipe/cli/RelpipeCLIException.h> |
|
41 |
||
42 |
#include "Configuration.h" |
|
43 |
||
44 |
namespace relpipe { |
|
45 |
namespace tr { |
|
46 |
namespace awk { |
|
47 |
||
48 |
using namespace std; |
|
49 |
using namespace relpipe; |
|
50 |
using namespace relpipe::reader; |
|
51 |
using namespace relpipe::reader::handlers; |
|
52 |
||
5 | 53 |
/** |
54 |
* This transformation consists of three processes connected together using pipes. |
|
55 |
* |
|
56 |
* Processes: |
|
57 |
* - Parent: process RelationalReaderStringHandler events (read relational data, usually from STDIN) and pass them in the special text format to the pipe1 |
|
58 |
* - AWK: external program (/usr/bin/awk), read from the pipe1, writes to the pipe2 |
|
59 |
* - Writer: reads from the pipe2 and writes to relationalWriter (relational data, usually to STDOUT) |
|
60 |
*/ |
|
0 | 61 |
class AwkHandler : public RelationalReaderStringHandler { |
62 |
private: |
|
63 |
Configuration configuration; |
|
64 |
writer::RelationalWriter* relationalWriter; |
|
65 |
||
2 | 66 |
int awkInputWriterFD = -1; |
67 |
||
1 | 68 |
void createPipe(int& readerFD, int& writerFD) { |
69 |
int fds[2]; |
|
70 |
int result = pipe(fds); |
|
71 |
readerFD = fds[0]; |
|
72 |
writerFD = fds[1]; |
|
73 |
if (result < 0) throw cli::RelpipeCLIException(L"Unable to create a pipe.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions? |
|
74 |
} |
|
75 |
||
76 |
void redirectFD(int oldfd, int newfd) { |
|
77 |
int result = dup2(oldfd, newfd); |
|
78 |
if (result < 0) throw cli::RelpipeCLIException(L"Unable redirect FD.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions? |
|
79 |
} |
|
80 |
||
81 |
void closeOrThrow(int fd) { |
|
82 |
int error = close(fd); |
|
2 | 83 |
if (error) throw cli::RelpipeCLIException(L"Unable to close FD: " + to_wstring(fd) + L" from PID: " + to_wstring(getpid()), cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions? |
84 |
} |
|
85 |
||
86 |
void cleanUp() { |
|
87 |
if (awkInputWriterFD >= 0) { |
|
88 |
closeOrThrow(awkInputWriterFD); |
|
5 | 89 |
// TODO: check exit codes |
2 | 90 |
__pid_t waitResult1 = wait(NULL); |
91 |
__pid_t waitResult2 = wait(NULL); |
|
92 |
awkInputWriterFD = -1; |
|
93 |
} |
|
1 | 94 |
} |
95 |
||
0 | 96 |
public: |
97 |
||
98 |
AwkHandler(writer::RelationalWriter* relationalWriter, Configuration& configuration) : relationalWriter(relationalWriter), configuration(configuration) { |
|
99 |
} |
|
100 |
||
101 |
void startRelation(string_t name, vector<AttributeMetadata> attributes) override { |
|
2 | 102 |
cleanUp(); |
0 | 103 |
|
1 | 104 |
|
105 |
int awkInputReaderFD; |
|
106 |
int awkOutputReaderFD; |
|
107 |
int awkOutputWriterFD; |
|
108 |
||
109 |
createPipe(awkInputReaderFD, awkInputWriterFD); |
|
110 |
createPipe(awkOutputReaderFD, awkOutputWriterFD); |
|
111 |
||
112 |
__pid_t awkPid = fork(); |
|
113 |
||
114 |
if (awkPid < 0) { |
|
115 |
throw cli::RelpipeCLIException(L"Unable to fork AWK process.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions? |
|
116 |
} else if (awkPid == 0) { |
|
117 |
// AWK child process |
|
2 | 118 |
closeOrThrow(awkInputWriterFD); |
119 |
closeOrThrow(awkOutputReaderFD); |
|
1 | 120 |
|
2 | 121 |
redirectFD(awkInputReaderFD, STDIN_FILENO); |
122 |
redirectFD(awkOutputWriterFD, STDOUT_FILENO); |
|
1 | 123 |
|
5 | 124 |
// Runs AWK program found on $PATH → user can plug-in a custom implementation or a wrapper, but this can be also bit dangerous (however AWK itself is dangerous). |
1 | 125 |
execlp("awk", "awk", "{print \"AWK says: line \" NR \" = \" $0;}", nullptr); |
126 |
} else { |
|
127 |
// Parent process |
|
2 | 128 |
closeOrThrow(awkInputReaderFD); |
129 |
closeOrThrow(awkOutputWriterFD); |
|
1 | 130 |
|
131 |
__pid_t writerPid = fork(); |
|
132 |
||
133 |
if (writerPid < 0) { |
|
134 |
throw cli::RelpipeCLIException(L"Unable to fork Writer process.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions? |
|
135 |
} else if (writerPid == 0) { |
|
136 |
// Writer child process |
|
2 | 137 |
closeOrThrow(awkInputWriterFD); |
1 | 138 |
|
3
e086ae6a19c3
generate (some) relational output
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
139 |
__gnu_cxx::stdio_filebuf<wchar_t> awkOutputReaderBuffer(awkOutputReaderFD, std::ios::in); |
e086ae6a19c3
generate (some) relational output
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
140 |
std::wistream awkOutputReader(&awkOutputReaderBuffer); |
1 | 141 |
|
3
e086ae6a19c3
generate (some) relational output
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
142 |
relationalWriter->startRelation(L"writer_debug",{ |
e086ae6a19c3
generate (some) relational output
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
143 |
{L"message", writer::TypeId::STRING}, |
e086ae6a19c3
generate (some) relational output
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
144 |
}, true); |
e086ae6a19c3
generate (some) relational output
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
145 |
|
e086ae6a19c3
generate (some) relational output
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
146 |
for (wchar_t ch = awkOutputReader.get(); awkOutputReader.good(); ch = awkOutputReader.get()) { |
e086ae6a19c3
generate (some) relational output
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
147 |
relationalWriter->writeAttribute(string_t(1, ch)); |
1 | 148 |
} |
149 |
||
2 | 150 |
closeOrThrow(awkOutputReaderFD); |
151 |
exit(0); |
|
1 | 152 |
} else { |
153 |
// Parent process |
|
2 | 154 |
closeOrThrow(awkOutputReaderFD); |
1 | 155 |
} |
156 |
} |
|
157 |
||
0 | 158 |
} |
159 |
||
160 |
void attribute(const string_t& value) override { |
|
2 | 161 |
dprintf(awkInputWriterFD, "attribute!\n"); |
0 | 162 |
} |
163 |
||
164 |
void endOfPipe() { |
|
2 | 165 |
cleanUp(); |
0 | 166 |
} |
167 |
||
168 |
}; |
|
169 |
||
170 |
} |
|
171 |
} |
|
172 |
} |