-
Notifications
You must be signed in to change notification settings - Fork 0
/
piper.h
140 lines (126 loc) · 2.77 KB
/
piper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <cstring>
#include <iostream>
#include <cstdarg>
#include <vector>
#include <signal.h>
#include <thread>
#include <chrono>
#include <sys/wait.h>
class piper {
protected:
int _in[2];
int _out[2];
pid_t _pid;
public:
piper()
{
if(pipe(_in) == -1)
{
exit(-1);
}
if(pipe(_out) == -1)
{
exit(-1);
}
}
~piper()
{
}
bool run(const char* cmd, std::vector<const char*> args)
{
if(args.size() < 1) return false;
args.push_back(nullptr);
_pid = fork();
if (_pid == (pid_t) 0)
{
close(_in[1]);
close(_out[0]);
dup2(_in[0], STDIN_FILENO);
dup2(_out[1], STDOUT_FILENO);
dup2(_out[1], STDERR_FILENO);
execv(cmd, (char* const*)args.data());
exit(0);
}
else if (_pid < (pid_t) 0)
{
std::cerr << "Fork failed" << std::endl;
}
else
{
close(_out[1]);
close(_in[0]);
}
return false;
}
void block(size_t ms = 100) const {
int status = 0;
while (wait(&status) != _pid);
}
void block(std::ostream& ss, size_t ms = 100)
{
block(ms);
const auto MX = 1024;
char c[MX+1];
do {
auto r = read(_out[0], c, 1024);
if(r <= 0) return;
c[r] = '\0';
ss << c;
if(r < MX) return;
} while(true);
}
void send(const char* cmd)
{
write(_in[1], cmd, strlen(cmd));
fsync(_in[1]);
}
bool readchar(std::ostream& ss)
{
char c;
int r = read(_out[0], &c, 1);
if(!r){
return false;
}
else if(c != '\n'){
ss << c;
return true;
}
else {
c = 0;
return false;
}
}
void readline(std::ostream& ss)
{
while(true){
if(!readchar(ss)) break;
}
ss << "\n";
}
void readchunk(std::ostream& ss, size_t timeout = 1000)
{
timeval tv;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(_out[0], &rfds);
while(true){
while(true){
tv.tv_sec = timeout / 1000;
tv.tv_usec = timeout % 1000;
int dataflag = select(_out[0] + 1, &rfds, NULL, NULL, &tv);
if(dataflag && dataflag != -1) {
if(!readchar(ss)) break;
}
else
{
return;
}
}
ss << "\n";
}
}
};