-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_redirect_handler.cc
More file actions
169 lines (162 loc) · 3.79 KB
/
io_redirect_handler.cc
File metadata and controls
169 lines (162 loc) · 3.79 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "io_redirect_handler.h"
CppIORedirectHandler::CppIORedirectHandler(const config &cfg):cin_buffer(NULL), cout_buffer(NULL), ifs(NULL), ofs(NULL), ok(cfg.server)
{
if (ok)
{
ifs = new std::ifstream(cfg.InPipe.c_str());
if (ifs->fail())
{
std::cerr << "Cannot open in pipe (" << cfg.InPipe << "). Reverting to Stdio." << std::endl;
ok = false;
}
ofs = new std::ofstream(cfg.OutPipe.c_str());
if (ofs->fail())
{
std::cerr << "Cannot open out pipe (" << cfg.OutPipe << "). Reverting to Stdio." << std::endl;
ok = false;
}
if (ok)
{
cin_buffer = std::cin.rdbuf(ifs->rdbuf());
cout_buffer = std::cout.rdbuf(ofs->rdbuf());
}
}
}
CppIORedirectHandler::~CppIORedirectHandler()
{
if (cin_buffer)
std::cin.rdbuf(cin_buffer);
if (cout_buffer)
std::cout.rdbuf(cout_buffer);
delete ifs;
delete ofs;
}
bool CppIORedirectHandler::serverOK()
{
return ok;
}
CppWIORedirectHandler::CppWIORedirectHandler(const config &cfg):cin_buffer(NULL), cout_buffer(NULL), ifs(NULL), ofs(NULL), ok(cfg.server)
{
if (ok)
{
ifs = new std::wifstream(cfg.InPipe.c_str());
if (ifs->fail())
{
std::cerr << "Cannot open in pipe (" << cfg.InPipe << "). Reverting to Stdio." << std::endl;
ok = false;
}
ofs = new std::wofstream(cfg.OutPipe.c_str());
if (ofs->fail())
{
std::cerr << "Cannot open out pipe (" << cfg.OutPipe << "). Reverting to Stdio." << std::endl;
ok = false;
}
if (ok)
{
cin_buffer = std::wcin.rdbuf(ifs->rdbuf());
cout_buffer = std::wcout.rdbuf(ofs->rdbuf());
}
}
}
CppWIORedirectHandler::~CppWIORedirectHandler()
{
if (cin_buffer)
std::wcin.rdbuf(cin_buffer);
if (cout_buffer)
std::wcout.rdbuf(cout_buffer);
delete ifs;
delete ofs;
}
bool CppWIORedirectHandler::serverOK()
{
return ok;
}
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
Fd0WcoutRedirectHandler::Fd0WcoutRedirectHandler(const config &cfg):original_stdin(-1), cout_buffer(NULL), ofs(NULL), ok(cfg.server)
{
int pipe_fd(0);
if (ok)
{
// open input and output pipes
// open pipe in stdin file descriptor
pipe_fd = open(cfg.InPipe.c_str(), 0);
if (-1 == pipe_fd)
{
std::cerr << "Cannot open in pipe (" << cfg.InPipe << "). Reverting to Stdio." << std::endl;
ok = false;
}
ofs = new std::wofstream(cfg.OutPipe.c_str());
if (ofs->fail())
{
std::cerr << "Cannot open out pipe (" << cfg.OutPipe << "). Reverting to Stdio." << std::endl;
ok = false;
}
if (ok)
{
// duplicate stdin
original_stdin = dup(0);
if (original_stdin == -1)
{
perror("dup(0)");
exit(1);
}
// close original stdin fd
if (close(0))
{
perror("close(0)");
exit(1);
}
// dup in pipe as stdin
if (0 != dup(pipe_fd))
{
perror("dup(pipe_fd)");
exit(1);
}
// close pipe's original fd
if (0 != close(pipe_fd))
{
perror("close(pipe_fd)");
exit(1);
}
// redirect wcout
cout_buffer = std::wcout.rdbuf(ofs->rdbuf());
}
}
}
Fd0WcoutRedirectHandler::~Fd0WcoutRedirectHandler()
{
// restore wcout
if (cout_buffer)
std::wcout.rdbuf(cout_buffer);
delete ofs;
if (original_stdin != -1)
{
// close pipe
if (-1 == close(0))
{
perror("~close(0)");
exit(1);
}
// restore stdin
if (0 != dup(original_stdin))
{
perror("~dup(original_stdin)");
exit(1);
}
if (-1 == close(original_stdin))
{
perror("~close(original_stdin)");
exit(1);
}
}
}
bool Fd0WcoutRedirectHandler::serverOK()
{
return ok;
}