-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPipe.cc
More file actions
111 lines (89 loc) · 2.35 KB
/
Pipe.cc
File metadata and controls
111 lines (89 loc) · 2.35 KB
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
#include "Pipe.h"
#include "misc.h"
#include "Interpreter.h"
#include "OutputWindow.h"
#include <unistd.h>
#include <limits.h>
#include <sys/poll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <cstring>
Pipe::Pipe(int fd1, int fd2) {
// socketpair has apparently greater 'capacity' than pipe
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0)
error ("Pipe::Pipe socketpair: %m");
if (fd1 != -1) {
if (dup2(fds[Read], fd1) < 0)
error ("Pipe::Pipe dup2 (%d->%d) %m", fds[Read], fd1);
close(fds[Read]);
fds[Read] = fd1;
}
if (fd2 != -1) {
if (dup2(fds[Write], fd2) < 0)
error ("Pipe::Pipe dup2 (%d->%d) %m", fds[Write], fd2);
close(fds[Write]);
fds[Write] = fd2;
}
}
Pipe::~Pipe() {
close(fds[0]);
close(fds[1]);
}
int Pipe::read (char *buf, int count) {
count = ::read(fds[Read], buf, count);
return count;
}
int Pipe::write(const char *buf, int count) {
count = ::write(fds[Write], buf, count);
return count;
}
int Pipe::init_fdset(fd_set *set, fd_set*) {
FD_SET(fds[Read], set);
return fds[Read];
}
void Pipe::check_fdset(fd_set *set, fd_set*) {
if (FD_ISSET(fds[Read], set)) {
//FIXME cout << "Activity on a pipe...\n";
inputReady();
}
}
InterpreterPipe::InterpreterPipe() : Pipe(), pos(0) {
}
void InterpreterPipe::inputReady() {
int res;
char *s;
res = read(line_buf+pos, sizeof(line_buf)-pos);
if (res <= 0)
error ("inputReady::read:%m");
pos += res;
while ((s = (char*)memchr(line_buf, '\n', pos))) {
char buf[PIPE_BUF];
int len = s-line_buf;
memcpy(buf, line_buf, len);
buf[len] = NUL;
memmove(line_buf, s+1, pos - len);
pos -= len+1;
interpreter.add(buf);
}
}
bool InterpreterPipe::have_data() {
pollfd myfd;
myfd.fd = fds[0]; // My read fd
myfd.events = 0;
myfd.events |= POLLIN;
poll(&myfd, 1, 0);
return(myfd.revents & POLLIN);
}
OutputPipe::OutputPipe() : Pipe(-1, STDOUT_FILENO) {
}
void OutputPipe::inputReady() {
char buf[PIPE_BUF];
int count = read(buf, sizeof(buf));
if (count < 0)
error ("OutputPipe::inputReady:%m");
buf[count] = NUL;
outputWindow->printf("%s", buf);
}
int Pipe::getFile(End e) {
return (fds[e]);
}