0
|
1 |
#include <stdio.h>
|
|
2 |
#include <stdlib.h>
|
|
3 |
#include <sys/select.h>
|
|
4 |
#include <sys/types.h>
|
|
5 |
#include <sys/wait.h>
|
|
6 |
#include <unistd.h>
|
|
7 |
|
|
8 |
#include "dictre.h"
|
|
9 |
|
1
|
10 |
enum
|
|
11 |
{
|
|
12 |
SIZESTEP=100
|
|
13 |
};
|
|
14 |
|
0
|
15 |
extern struct Def defs[];
|
|
16 |
extern int ndefs;
|
|
17 |
extern int dont_touch[];
|
|
18 |
extern int ndont_touch;
|
|
19 |
|
|
20 |
static void more_memory(void **ptr, int size)
|
|
21 |
{
|
|
22 |
void *new;
|
|
23 |
|
|
24 |
new = realloc(*ptr, size);
|
|
25 |
*ptr = new;
|
|
26 |
}
|
|
27 |
|
|
28 |
static
|
|
29 |
char * manage_filter(const char *def, int deflen, int writeto, int readfrom,
|
|
30 |
int *outlen)
|
|
31 |
{
|
|
32 |
int maxfd;
|
|
33 |
int defptr;
|
|
34 |
int outptr;
|
|
35 |
char *out;
|
|
36 |
int outsize;
|
|
37 |
int outrest;
|
|
38 |
|
|
39 |
out = 0;
|
1
|
40 |
outsize = SIZESTEP;
|
0
|
41 |
outptr = 0;
|
|
42 |
more_memory((void **) &out, outsize);
|
1
|
43 |
outrest = SIZESTEP;
|
0
|
44 |
|
|
45 |
maxfd = writeto;
|
|
46 |
if (readfrom > maxfd)
|
|
47 |
maxfd = readfrom;
|
|
48 |
|
|
49 |
defptr = 0;
|
|
50 |
do
|
|
51 |
{
|
|
52 |
fd_set writeset, readset;
|
|
53 |
FD_ZERO(&writeset);
|
|
54 |
FD_ZERO(&readset);
|
|
55 |
if (defptr < deflen)
|
|
56 |
FD_SET(writeto, &writeset);
|
|
57 |
FD_SET(readfrom, &readset);
|
|
58 |
|
|
59 |
select(maxfd+1, &readset, &writeset, 0, 0);
|
|
60 |
|
|
61 |
if (FD_ISSET(readfrom, &readset))
|
|
62 |
{
|
|
63 |
int res;
|
|
64 |
res = read(readfrom, out + outptr, outrest);
|
|
65 |
if (res == 0)
|
|
66 |
{
|
|
67 |
close(readfrom);
|
|
68 |
break;
|
|
69 |
}
|
|
70 |
outrest -= res;
|
|
71 |
outptr += res;
|
|
72 |
if (outrest == 0)
|
|
73 |
{
|
1
|
74 |
outrest = SIZESTEP;
|
|
75 |
outsize += SIZESTEP;
|
0
|
76 |
more_memory((void **) &out, outsize);
|
|
77 |
}
|
|
78 |
}
|
|
79 |
|
|
80 |
if (FD_ISSET(writeto, &writeset))
|
|
81 |
{
|
|
82 |
int res;
|
1
|
83 |
res = write(writeto, def+defptr, deflen - defptr);
|
|
84 |
defptr += res;
|
0
|
85 |
if (defptr >= deflen)
|
|
86 |
close(writeto);
|
|
87 |
}
|
|
88 |
} while(1);
|
|
89 |
|
|
90 |
if (defptr < deflen)
|
|
91 |
{
|
|
92 |
fprintf(stderr, "Error in filter! not all written.\n");
|
|
93 |
exit(-1);
|
|
94 |
}
|
|
95 |
|
|
96 |
*outlen = outptr;
|
|
97 |
return out;
|
|
98 |
}
|
|
99 |
|
|
100 |
static char * filter(char *def, int deflen, const char *filter_par, int *outlen)
|
|
101 |
{
|
|
102 |
int write_pipe[2];
|
|
103 |
int read_pipe[2];
|
|
104 |
int pid;
|
|
105 |
int res;
|
|
106 |
int status;
|
|
107 |
char *out;
|
|
108 |
|
|
109 |
pipe(write_pipe);
|
|
110 |
pipe(read_pipe);
|
|
111 |
|
|
112 |
|
|
113 |
pid = fork();
|
|
114 |
switch(pid)
|
|
115 |
{
|
|
116 |
case 0: /* child */
|
|
117 |
close(0);
|
|
118 |
dup(write_pipe[0]);
|
|
119 |
close(write_pipe[0]);
|
|
120 |
close(write_pipe[1]);
|
|
121 |
close(1);
|
|
122 |
dup(read_pipe[1]);
|
|
123 |
close(read_pipe[1]);
|
|
124 |
close(read_pipe[0]);
|
|
125 |
execlp("bash", "bash", "-c", filter_par, 0);
|
|
126 |
perror("execlp");
|
|
127 |
exit(-1);
|
|
128 |
break;
|
|
129 |
case -1:
|
|
130 |
perror("fork");
|
|
131 |
exit(-1);
|
|
132 |
break;
|
|
133 |
default: /* parent */
|
|
134 |
close(write_pipe[0]);
|
|
135 |
close(read_pipe[1]);
|
|
136 |
break;
|
|
137 |
}
|
|
138 |
|
|
139 |
/* parent */
|
|
140 |
out = manage_filter(def, deflen, write_pipe[1], read_pipe[0], outlen);
|
|
141 |
|
|
142 |
res = wait(&status);
|
|
143 |
if (res != pid || WEXITSTATUS(status) != 0)
|
|
144 |
{
|
|
145 |
fprintf(stderr, "Error filtering: pid=%i status=%i",
|
|
146 |
pid, WEXITSTATUS(status));
|
|
147 |
exit(-1);
|
|
148 |
}
|
|
149 |
|
|
150 |
return out;
|
|
151 |
}
|
|
152 |
|
|
153 |
static int in_dont_touch(int n)
|
|
154 |
{
|
|
155 |
int i;
|
|
156 |
for(i =0; i < ndont_touch; ++i)
|
|
157 |
{
|
|
158 |
if (n == dont_touch[i])
|
|
159 |
{
|
|
160 |
return 1;
|
|
161 |
}
|
|
162 |
}
|
|
163 |
return 0;
|
|
164 |
}
|
|
165 |
|
|
166 |
void filter_all(const char *filter_par)
|
|
167 |
{
|
|
168 |
int i;
|
|
169 |
|
|
170 |
for(i=0; i < ndefs; ++i)
|
|
171 |
{
|
|
172 |
char *newdef;
|
|
173 |
int newdeflen;
|
|
174 |
if (!in_dont_touch(i))
|
|
175 |
{
|
1
|
176 |
if (defs[i].length > 0)
|
|
177 |
{
|
|
178 |
newdef = filter(defs[i].d, defs[i].length,
|
|
179 |
filter_par, &newdeflen);
|
|
180 |
defs[i].length = newdeflen;
|
|
181 |
free(defs[i].d);
|
|
182 |
defs[i].d = newdef;
|
|
183 |
}
|
0
|
184 |
}
|
|
185 |
}
|
|
186 |
}
|