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 |
|
|
53 |
class AwkHandler : public RelationalReaderStringHandler {
|
|
54 |
private:
|
|
55 |
Configuration configuration;
|
|
56 |
writer::RelationalWriter* relationalWriter;
|
|
57 |
|
2
|
58 |
int awkInputWriterFD = -1;
|
|
59 |
|
1
|
60 |
void createPipe(int& readerFD, int& writerFD) {
|
|
61 |
int fds[2];
|
|
62 |
int result = pipe(fds);
|
|
63 |
readerFD = fds[0];
|
|
64 |
writerFD = fds[1];
|
|
65 |
if (result < 0) throw cli::RelpipeCLIException(L"Unable to create a pipe.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
|
|
66 |
}
|
|
67 |
|
|
68 |
void redirectFD(int oldfd, int newfd) {
|
|
69 |
int result = dup2(oldfd, newfd);
|
|
70 |
if (result < 0) throw cli::RelpipeCLIException(L"Unable redirect FD.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
|
|
71 |
}
|
|
72 |
|
|
73 |
void closeOrThrow(int fd) {
|
|
74 |
int error = close(fd);
|
2
|
75 |
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?
|
|
76 |
}
|
|
77 |
|
|
78 |
void cleanUp() {
|
|
79 |
if (awkInputWriterFD >= 0) {
|
|
80 |
closeOrThrow(awkInputWriterFD);
|
|
81 |
fwprintf(stderr, L"writing done and closed\n");
|
|
82 |
__pid_t waitResult1 = wait(NULL);
|
|
83 |
fwprintf(stderr, L"wait 1 done: %d\n", waitResult1);
|
|
84 |
__pid_t waitResult2 = wait(NULL);
|
|
85 |
fwprintf(stderr, L"wait 2 done: %d\n", waitResult2);
|
|
86 |
awkInputWriterFD = -1;
|
|
87 |
}
|
1
|
88 |
}
|
|
89 |
|
0
|
90 |
public:
|
|
91 |
|
|
92 |
AwkHandler(writer::RelationalWriter* relationalWriter, Configuration& configuration) : relationalWriter(relationalWriter), configuration(configuration) {
|
|
93 |
}
|
|
94 |
|
|
95 |
void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
|
2
|
96 |
cleanUp();
|
0
|
97 |
|
1
|
98 |
|
|
99 |
int awkInputReaderFD;
|
|
100 |
int awkOutputReaderFD;
|
|
101 |
int awkOutputWriterFD;
|
|
102 |
|
|
103 |
createPipe(awkInputReaderFD, awkInputWriterFD);
|
|
104 |
createPipe(awkOutputReaderFD, awkOutputWriterFD);
|
|
105 |
|
|
106 |
__pid_t awkPid = fork();
|
|
107 |
|
|
108 |
if (awkPid < 0) {
|
|
109 |
throw cli::RelpipeCLIException(L"Unable to fork AWK process.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
|
|
110 |
} else if (awkPid == 0) {
|
|
111 |
// AWK child process
|
2
|
112 |
closeOrThrow(awkInputWriterFD);
|
|
113 |
closeOrThrow(awkOutputReaderFD);
|
1
|
114 |
|
2
|
115 |
redirectFD(awkInputReaderFD, STDIN_FILENO);
|
|
116 |
redirectFD(awkOutputWriterFD, STDOUT_FILENO);
|
1
|
117 |
|
|
118 |
|
|
119 |
fwprintf(stderr, L"I am child AWK with PID: %d\n", getpid());
|
|
120 |
execlp("awk", "awk", "{print \"AWK says: line \" NR \" = \" $0;}", nullptr);
|
|
121 |
|
|
122 |
} else {
|
|
123 |
// Parent process
|
2
|
124 |
closeOrThrow(awkInputReaderFD);
|
|
125 |
closeOrThrow(awkOutputWriterFD);
|
1
|
126 |
fwprintf(stderr, L"Forked AWK has PID: %d\n", awkPid);
|
|
127 |
|
|
128 |
__pid_t writerPid = fork();
|
|
129 |
|
|
130 |
if (writerPid < 0) {
|
|
131 |
throw cli::RelpipeCLIException(L"Unable to fork Writer process.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
|
|
132 |
} else if (writerPid == 0) {
|
|
133 |
// Writer child process
|
2
|
134 |
closeOrThrow(awkInputWriterFD);
|
1
|
135 |
fwprintf(stderr, L"I am child Writer with PID: %d\n", getpid());
|
|
136 |
|
|
137 |
__gnu_cxx::stdio_filebuf<char> awkOutputReaderBuffer(awkOutputReaderFD, std::ios::in);
|
|
138 |
std::istream awkOutputReader(&awkOutputReaderBuffer);
|
|
139 |
|
|
140 |
std::cout << "--- PIPE start --------" << std::endl;
|
|
141 |
for (char ch = awkOutputReader.get(); awkOutputReader.good(); ch = awkOutputReader.get()) {
|
|
142 |
std::cout << ch;
|
|
143 |
}
|
|
144 |
std::cout << "--- PIPE end ----------" << std::endl;
|
|
145 |
|
2
|
146 |
closeOrThrow(awkOutputReaderFD);
|
|
147 |
exit(0);
|
1
|
148 |
} else {
|
|
149 |
// Parent process
|
2
|
150 |
closeOrThrow(awkOutputReaderFD);
|
1
|
151 |
fwprintf(stderr, L"Forked Writer has PID: %d\n", writerPid);
|
|
152 |
|
|
153 |
dprintf(awkInputWriterFD, "hello world :-)\n");
|
2
|
154 |
//closeOrThrow(awkInputWriterFD);
|
1
|
155 |
|
|
156 |
/*
|
|
157 |
__gnu_cxx::stdio_filebuf<char> awkInputWriterBuffer(awkInputWriterFD, std::ios::out);
|
|
158 |
std::ostream awkInputWriter(&awkInputWriterBuffer);
|
|
159 |
awkInputWriter << "hello world :-)" << std::endl;
|
|
160 |
awkInputWriter.flush();
|
2
|
161 |
closeOrThrow(awkInputWriterFD);
|
1
|
162 |
*/
|
|
163 |
|
|
164 |
}
|
|
165 |
}
|
|
166 |
|
0
|
167 |
}
|
|
168 |
|
|
169 |
void attribute(const string_t& value) override {
|
2
|
170 |
dprintf(awkInputWriterFD, "attribute!\n");
|
0
|
171 |
}
|
|
172 |
|
|
173 |
void endOfPipe() {
|
2
|
174 |
cleanUp();
|
0
|
175 |
}
|
|
176 |
|
|
177 |
};
|
|
178 |
|
|
179 |
}
|
|
180 |
}
|
|
181 |
}
|