This repository has been archived by the owner on Jan 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
rewrite.cc
153 lines (126 loc) · 3 KB
/
rewrite.cc
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
/*
* rewrite - string rewriting functions through coproc. - implementation
* Copyright(c) 2004-2008 of wave++ (Yuri D'Elia)
* Distributed under GNU LGPL without ANY warranty.
*/
// interface
#include "rewrite.hh"
using std::string;
#include <stdexcept>
using std::runtime_error;
// c system headers
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <termios.h>
#include <errno.h>
#include <fcntl.h>
// pseudo-master device
#if (!defined(_POSIX_VERSION) || (_POSIX_VERSION < 200112L))
#include <paths.h>
#define PATH_PTMX _PATH_DEV "ptmx"
#endif
// streams
#if !defined(BSD) && !defined(__linux__)
#define HAS_STREAMS
#include <sys/ioctl.h>
#include <stropts.h>
#endif
// avoid inclusion of pty.h under linux (to avoid redeclaration and libutil)
struct winsize;
// partial replacement to satisfy our needs (forkpty isn't supported enough)
pid_t
forkpty(int* amaster, char*, const termios*, const winsize*)
{
pid_t pid;
int masterFd;
// open the master device and fork
#ifndef PATH_PTMX
if((masterFd = posix_openpt(O_RDWR)) < 0)
return -1;
#else
if((masterFd = open(PATH_PTMX, O_RDWR)) < 0)
return -1;
#endif
if(grantpt(masterFd) < 0 || unlockpt(masterFd) < 0 || (pid = fork()) < 0)
{
close(masterFd);
return -1;
}
if(pid)
*amaster = masterFd;
else
{
// establish as a session leader
setsid();
const char* slavePath;
int slaveFd;
if(!(slavePath = ptsname(masterFd)) ||
(slaveFd = open(slavePath, O_RDWR)) < 0)
{
close(masterFd);
return -1;
}
close(masterFd);
#ifdef HAS_STREAMS
if(ioctl(slaveFd, I_PUSH, "ptem") < 0 ||
ioctl(slaveFd, I_PUSH, "ldterm") < 0)
{
close(slaveFd);
return -1;
}
// not all systems have ttcompat
ioctl(slaveFd, I_PUSH, "ttcompat");
#endif
dup2(slaveFd, STDIN_FILENO);
dup2(slaveFd, STDOUT_FILENO);
dup2(slaveFd, STDERR_FILENO);
close(slaveFd);
}
return pid;
}
Rewrite::Rewrite(const char* arg, const char* coproc, const arg_t type)
{
pid = forkpty(&fd, NULL, NULL, NULL);
if(pid < 0)
throw runtime_error(string("cannot initialize slave pty: ")
+ strerror(errno));
if(!pid)
{
// setup the terminal environment
termios t;
tcgetattr(STDIN_FILENO, &t);
cfmakeraw(&t);
t.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW | TCSAFLUSH, &t);
// execute the coprocess
execlp(coproc, coproc, (type == expr? "-e": "-f"), arg, NULL);
throw runtime_error(string("cannot execute ") + coproc);
}
}
Rewrite::~Rewrite() throw()
{
// close and wait
close(fd);
waitpid(pid, NULL, 0);
}
void
Rewrite::operator()(string& buf)
{
// write to the coprocess
write(fd, buf.data(), buf.size());
if(write(fd, "\n", 1) < 0)
throw runtime_error("cannot write to the coprocess");
// read from the coprocess
buf.clear();
char c;
for(;; buf += c)
{
if(read(fd, &c, 1) < 0)
throw runtime_error("cannot read from the coprocess");
if(c == '\n')
break;
}
}