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>
|
2
|
7 |
#include <fcntl.h>
|
0
|
8 |
|
|
9 |
#include "dictre.h"
|
|
10 |
|
1
|
11 |
enum
|
|
12 |
{
|
3
|
13 |
SIZESTEP=1000
|
1
|
14 |
};
|
|
15 |
|
0
|
16 |
extern struct Def defs[];
|
|
17 |
extern int ndefs;
|
|
18 |
extern int dont_touch[];
|
|
19 |
extern int ndont_touch;
|
|
20 |
|
|
21 |
static void more_memory(void **ptr, int size)
|
|
22 |
{
|
|
23 |
void *new;
|
|
24 |
|
|
25 |
new = realloc(*ptr, size);
|
|
26 |
*ptr = new;
|
|
27 |
}
|
|
28 |
|
|
29 |
static
|
|
30 |
char * manage_filter(const char *def, int deflen, int writeto, int readfrom,
|
|
31 |
int *outlen)
|
|
32 |
{
|
|
33 |
int maxfd;
|
|
34 |
int defptr;
|
|
35 |
int outptr;
|
|
36 |
char *out;
|
|
37 |
int outsize;
|
|
38 |
int outrest;
|
2
|
39 |
int res;
|
0
|
40 |
|
|
41 |
out = 0;
|
1
|
42 |
outsize = SIZESTEP;
|
0
|
43 |
outptr = 0;
|
|
44 |
more_memory((void **) &out, outsize);
|
1
|
45 |
outrest = SIZESTEP;
|
0
|
46 |
|
2
|
47 |
/* We need unblocking write for select() */
|
|
48 |
res = fcntl(writeto, F_SETFL, O_NONBLOCK);
|
|
49 |
if (res == -1)
|
|
50 |
{
|
|
51 |
perror("Error setting nonblock to writeto");
|
|
52 |
exit(-1);
|
|
53 |
}
|
|
54 |
|
0
|
55 |
maxfd = writeto;
|
|
56 |
if (readfrom > maxfd)
|
|
57 |
maxfd = readfrom;
|
|
58 |
|
|
59 |
defptr = 0;
|
|
60 |
do
|
|
61 |
{
|
|
62 |
fd_set writeset, readset;
|
|
63 |
FD_ZERO(&writeset);
|
|
64 |
FD_ZERO(&readset);
|
|
65 |
if (defptr < deflen)
|
|
66 |
FD_SET(writeto, &writeset);
|
|
67 |
FD_SET(readfrom, &readset);
|
|
68 |
|
|
69 |
select(maxfd+1, &readset, &writeset, 0, 0);
|
|
70 |
|
|
71 |
if (FD_ISSET(readfrom, &readset))
|
|
72 |
{
|
|
73 |
int res;
|
|
74 |
res = read(readfrom, out + outptr, outrest);
|
|
75 |
if (res == 0)
|
|
76 |
{
|
|
77 |
close(readfrom);
|
|
78 |
break;
|
|
79 |
}
|
|
80 |
outrest -= res;
|
|
81 |
outptr += res;
|
|
82 |
if (outrest == 0)
|
|
83 |
{
|
1
|
84 |
outrest = SIZESTEP;
|
|
85 |
outsize += SIZESTEP;
|
0
|
86 |
more_memory((void **) &out, outsize);
|
|
87 |
}
|
|
88 |
}
|
|
89 |
|
|
90 |
if (FD_ISSET(writeto, &writeset))
|
|
91 |
{
|
|
92 |
int res;
|
1
|
93 |
res = write(writeto, def+defptr, deflen - defptr);
|
|
94 |
defptr += res;
|
0
|
95 |
if (defptr >= deflen)
|
|
96 |
close(writeto);
|
|
97 |
}
|
|
98 |
} while(1);
|
|
99 |
|
2
|
100 |
/*
|
|
101 |
{
|
|
102 |
int i;
|
|
103 |
printf("In : ");
|
|
104 |
for(i=0; i < deflen; ++i)
|
|
105 |
putchar(def[i]);
|
|
106 |
printf("\nOut: ");
|
|
107 |
for(i=0; i < outptr; ++i)
|
|
108 |
putchar(out[i]);
|
|
109 |
putchar('\n');
|
|
110 |
}
|
|
111 |
*/
|
|
112 |
|
0
|
113 |
if (defptr < deflen)
|
|
114 |
{
|
|
115 |
fprintf(stderr, "Error in filter! not all written.\n");
|
|
116 |
exit(-1);
|
|
117 |
}
|
|
118 |
|
3
|
119 |
/* Give away memory don't needed */
|
|
120 |
more_memory((void **) &out, outptr);
|
|
121 |
|
0
|
122 |
*outlen = outptr;
|
|
123 |
return out;
|
|
124 |
}
|
|
125 |
|
|
126 |
static char * filter(char *def, int deflen, const char *filter_par, int *outlen)
|
|
127 |
{
|
|
128 |
int write_pipe[2];
|
|
129 |
int read_pipe[2];
|
|
130 |
int pid;
|
|
131 |
int res;
|
|
132 |
int status;
|
|
133 |
char *out;
|
|
134 |
|
|
135 |
pipe(write_pipe);
|
|
136 |
pipe(read_pipe);
|
|
137 |
|
|
138 |
|
|
139 |
pid = fork();
|
|
140 |
switch(pid)
|
|
141 |
{
|
|
142 |
case 0: /* child */
|
|
143 |
close(0);
|
|
144 |
dup(write_pipe[0]);
|
|
145 |
close(write_pipe[0]);
|
|
146 |
close(write_pipe[1]);
|
|
147 |
close(1);
|
|
148 |
dup(read_pipe[1]);
|
|
149 |
close(read_pipe[1]);
|
|
150 |
close(read_pipe[0]);
|
2
|
151 |
execl(filter_par, filter_par, 0);
|
0
|
152 |
perror("execlp");
|
|
153 |
exit(-1);
|
|
154 |
break;
|
|
155 |
case -1:
|
|
156 |
perror("fork");
|
|
157 |
exit(-1);
|
|
158 |
break;
|
|
159 |
default: /* parent */
|
|
160 |
close(write_pipe[0]);
|
|
161 |
close(read_pipe[1]);
|
|
162 |
break;
|
|
163 |
}
|
|
164 |
|
|
165 |
/* parent */
|
|
166 |
out = manage_filter(def, deflen, write_pipe[1], read_pipe[0], outlen);
|
|
167 |
|
|
168 |
res = wait(&status);
|
|
169 |
if (res != pid || WEXITSTATUS(status) != 0)
|
|
170 |
{
|
|
171 |
fprintf(stderr, "Error filtering: pid=%i status=%i",
|
|
172 |
pid, WEXITSTATUS(status));
|
|
173 |
exit(-1);
|
|
174 |
}
|
|
175 |
|
|
176 |
return out;
|
|
177 |
}
|
|
178 |
|
|
179 |
static int in_dont_touch(int n)
|
|
180 |
{
|
|
181 |
int i;
|
|
182 |
for(i =0; i < ndont_touch; ++i)
|
|
183 |
{
|
|
184 |
if (n == dont_touch[i])
|
|
185 |
{
|
|
186 |
return 1;
|
|
187 |
}
|
|
188 |
}
|
|
189 |
return 0;
|
|
190 |
}
|
|
191 |
|
|
192 |
void filter_all(const char *filter_par)
|
|
193 |
{
|
|
194 |
int i;
|
2
|
195 |
static int dispndefs = 0;
|
|
196 |
static int filtereddefs = 0;
|
0
|
197 |
|
|
198 |
for(i=0; i < ndefs; ++i)
|
|
199 |
{
|
|
200 |
char *newdef;
|
|
201 |
int newdeflen;
|
|
202 |
if (!in_dont_touch(i))
|
|
203 |
{
|
1
|
204 |
if (defs[i].length > 0)
|
|
205 |
{
|
|
206 |
newdef = filter(defs[i].d, defs[i].length,
|
|
207 |
filter_par, &newdeflen);
|
|
208 |
defs[i].length = newdeflen;
|
|
209 |
free(defs[i].d);
|
|
210 |
defs[i].d = newdef;
|
|
211 |
}
|
2
|
212 |
filtereddefs++; /* Not really all filtered. All but the 00-database* */
|
|
213 |
dispndefs++;
|
3
|
214 |
if (dispndefs >= 1000)
|
2
|
215 |
{
|
|
216 |
dispndefs = 0;
|
3
|
217 |
printf("Filtered: %i/%i (%f%%)\n", filtereddefs, ndefs,
|
4
|
218 |
(float) filtereddefs / (float) ndefs * 100.);
|
2
|
219 |
}
|
0
|
220 |
}
|
|
221 |
}
|
|
222 |
}
|