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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
/* The IGEN simulator generator for GDB, the GNU Debugger.
Copyright 2002 Free Software Foundation, Inc.
Contributed by Andrew Cagney.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#include <stdio.h>
#include "config.h"
#ifdef HAVE_STRING_H
#include <string.h>
#else
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#endif
#include "misc.h"
#include "lf.h"
#include "filter.h"
struct _filter {
char *member;
filter *next;
};
void
filter_parse (filter **filters,
const char *filt)
{
while (strlen (filt) > 0)
{
filter *new_filter;
filter **last;
/* break out a member of the filter list */
const char *flag = filt;
unsigned /*size_t*/ len;
filt = strchr (filt, ',');
if (filt == NULL)
{
filt = strchr (flag, '\0');
len = strlen (flag);
}
else
{
len = filt - flag;
filt = filt + 1;
}
/* find an insertion point - sorted order */
last = filters;
while (*last != NULL
&& strncmp (flag, (*last)->member, len) > 0)
last = &(*last)->next;
if (*last != NULL
&& strncmp (flag, (*last)->member, len) == 0
&& strlen ((*last)->member) == len)
continue; /* duplicate */
/* create an entry for that member */
new_filter = ZALLOC (filter);
new_filter->member = NZALLOC (char, len + 1);
strncpy (new_filter->member, flag, len);
/* insert it */
new_filter->next = *last;
*last = new_filter;
}
}
void
filter_add (filter **set,
filter *add)
{
while (add != NULL)
{
int cmp;
if (*set == NULL)
cmp = 1; /* set->member > add->member */
else
cmp = strcmp ((*set)->member, add->member);
if (cmp > 0)
{
/* insert it here */
filter *new = ZALLOC (filter);
new->member = NZALLOC (char, strlen (add->member) + 1);
strcpy (new->member, add->member);
new->next = *set;
*set = new;
add = add->next;
}
else if (cmp == 0)
{
/* already in set */
add = add->next;
}
else /* cmp < 0 */
{
/* not reached insertion point */
set = &(*set)->next;
}
}
}
int
filter_is_subset (filter *superset,
filter *subset)
{
while (1)
{
int cmp;
if (subset == NULL)
return 1;
if (superset == NULL)
return 0; /* subset isn't finished */
cmp = strcmp (subset->member, superset->member);
if (cmp < 0)
return 0; /* not found */
else if (cmp == 0)
subset = subset->next; /* found */
else if (cmp > 0)
superset = superset->next; /* later in list? */
}
}
int
filter_is_common (filter *l,
filter *r)
{
while (1)
{
int cmp;
if (l == NULL)
return 0;
if (r == NULL)
return 0;
cmp = strcmp (l->member, r->member);
if (cmp < 0)
l = l->next;
else if (cmp == 0)
return 1; /* common member */
else if (cmp > 0)
r = r->next;
}
}
int
filter_is_member (filter *filt,
const char *flag)
{
int index = 1;
while (filt != NULL)
{
if (strcmp (flag, filt->member) == 0)
return index;
filt = filt->next;
index++;
}
return 0;
}
int
is_filtered_out (filter *filters,
const char *flags)
{
while (strlen(flags) > 0) {
int present;
filter *filt = filters;
/* break the string up */
char *end = strchr(flags, ',');
char *next;
unsigned /*size_t*/ len;
if (end == NULL) {
end = strchr(flags, '\0');
next = end;
}
else {
next = end + 1;
}
len = end - flags;
/* check that it is present */
present = 0;
filt = filters;
while (filt != NULL) {
if (strncmp(flags, filt->member, len) == 0
&& strlen(filt->member) == len) {
present = 1;
break;
}
filt = filt->next;
}
if (!present)
return 1;
flags = next;
}
return 0;
}
#if 0
int
it_is (const char *flag,
const char *flags)
{
int flag_len = strlen(flag);
while (*flags != '\0') {
if (!strncmp(flags, flag, flag_len)
&& (flags[flag_len] == ',' || flags[flag_len] == '\0'))
return 1;
while (*flags != ',') {
if (*flags == '\0')
return 0;
flags++;
}
flags++;
}
return 0;
}
#endif
char *
filter_next (filter *set,
char *member)
{
while (set != NULL)
{
if (strcmp (set->member, member) > 0)
return set->member;
set = set->next;
}
return NULL;
}
void
dump_filter (lf *file,
char *prefix,
filter *set,
char *suffix)
{
char *member;
lf_printf (file, "%s", prefix);
member = filter_next (set, "");
if (member != NULL)
{
while (1)
{
lf_printf (file, "%s", member);
member = filter_next (set, member);
if (member == NULL)
break;
lf_printf (file, ",");
}
}
lf_printf (file, "%s", suffix);
}
#ifdef MAIN
int
main(int argc, char **argv)
{
filter *subset = NULL;
filter *superset = NULL;
lf *l;
int i;
if (argc < 2) {
printf("Usage: filter <subset> <filter> ...\n");
exit (1);
}
/* load the filter up */
filter_parse (&subset, argv[1]);
for (i = 2; i < argc; i++)
filter_parse (&superset, argv[i]);
/* dump various info */
l = lf_open ("-", "stdout", lf_omit_references, lf_is_text, "tmp-filter");
#if 0
if (is_filtered_out (argv[1], superset))
lf_printf (l, "excluded\n");
else
lf_printf (l, "included\n");
#endif
/* subset */
{
dump_filter (l, "{", subset, " }");
if (filter_is_subset (superset, subset))
lf_printf (l, " subset of ");
else
lf_printf (l, " !subset of ");
dump_filter (l, "{", superset, " }");
lf_printf (l, "\n");
}
/* intersection */
{
dump_filter (l, "{", subset, " }");
if (filter_is_common (subset, superset))
lf_printf (l, " intersects ");
else
lf_printf (l, " !intersects ");
dump_filter (l, "{", superset, " }");
lf_printf (l, "\n");
}
/* membership */
{
filter *memb = subset;
while (memb != NULL)
{
lf_printf (l, "%s", memb->member);
if (filter_is_member (superset, memb->member))
lf_printf (l, " in ");
else
lf_printf (l, " !in ");
dump_filter (l, "{", superset, " }");
lf_printf (l, "\n");
memb = memb->next;
}
}
/* addition */
{
filter *add = NULL;
filter_add (&add, superset);
filter_add (&add, subset);
dump_filter (l, "{", add, " }");
lf_printf (l, " = ");
dump_filter (l, "{", subset, " }");
lf_printf (l, " + ");
dump_filter (l, "{", superset, " }");
lf_printf (l, "\n");
}
return 0;
}
#endif
|