Commit | Line | Data |
---|---|---|
feaee4bd AC |
1 | /* The IGEN simulator generator for GDB, the GNU Debugger. |
2 | ||
88b9d363 | 3 | Copyright 2002-2022 Free Software Foundation, Inc. |
feaee4bd AC |
4 | |
5 | Contributed by Andrew Cagney. | |
6 | ||
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
4744ac1b | 11 | the Free Software Foundation; either version 3 of the License, or |
feaee4bd AC |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
4744ac1b | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
21 | |
22 | ||
23 | #include <stdio.h> | |
c906108c | 24 | #include <string.h> |
c906108c SS |
25 | |
26 | #include "misc.h" | |
27 | #include "lf.h" | |
28 | #include "filter.h" | |
29 | ||
4e0bf4c4 AC |
30 | struct _filter |
31 | { | |
c906108c SS |
32 | char *member; |
33 | filter *next; | |
34 | }; | |
35 | ||
36 | ||
37 | void | |
4e0bf4c4 | 38 | filter_parse (filter **filters, const char *filt) |
c906108c SS |
39 | { |
40 | while (strlen (filt) > 0) | |
41 | { | |
42 | filter *new_filter; | |
43 | filter **last; | |
44 | /* break out a member of the filter list */ | |
45 | const char *flag = filt; | |
4e0bf4c4 | 46 | unsigned /*size_t */ len; |
c906108c SS |
47 | filt = strchr (filt, ','); |
48 | if (filt == NULL) | |
49 | { | |
50 | filt = strchr (flag, '\0'); | |
51 | len = strlen (flag); | |
52 | } | |
53 | else | |
54 | { | |
55 | len = filt - flag; | |
56 | filt = filt + 1; | |
57 | } | |
58 | /* find an insertion point - sorted order */ | |
59 | last = filters; | |
4e0bf4c4 | 60 | while (*last != NULL && strncmp (flag, (*last)->member, len) > 0) |
c906108c SS |
61 | last = &(*last)->next; |
62 | if (*last != NULL | |
63 | && strncmp (flag, (*last)->member, len) == 0 | |
64 | && strlen ((*last)->member) == len) | |
4e0bf4c4 | 65 | continue; /* duplicate */ |
c906108c SS |
66 | /* create an entry for that member */ |
67 | new_filter = ZALLOC (filter); | |
68 | new_filter->member = NZALLOC (char, len + 1); | |
69 | strncpy (new_filter->member, flag, len); | |
70 | /* insert it */ | |
71 | new_filter->next = *last; | |
72 | *last = new_filter; | |
73 | } | |
74 | } | |
75 | ||
76 | ||
77 | void | |
4e0bf4c4 | 78 | filter_add (filter **set, filter *add) |
c906108c SS |
79 | { |
80 | while (add != NULL) | |
81 | { | |
82 | int cmp; | |
83 | if (*set == NULL) | |
4e0bf4c4 | 84 | cmp = 1; /* set->member > add->member */ |
c906108c SS |
85 | else |
86 | cmp = strcmp ((*set)->member, add->member); | |
87 | if (cmp > 0) | |
88 | { | |
89 | /* insert it here */ | |
90 | filter *new = ZALLOC (filter); | |
91 | new->member = NZALLOC (char, strlen (add->member) + 1); | |
92 | strcpy (new->member, add->member); | |
93 | new->next = *set; | |
94 | *set = new; | |
95 | add = add->next; | |
96 | } | |
97 | else if (cmp == 0) | |
98 | { | |
99 | /* already in set */ | |
100 | add = add->next; | |
101 | } | |
4e0bf4c4 | 102 | else /* cmp < 0 */ |
c906108c SS |
103 | { |
104 | /* not reached insertion point */ | |
105 | set = &(*set)->next; | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
110 | ||
111 | int | |
4e0bf4c4 | 112 | filter_is_subset (filter *superset, filter *subset) |
c906108c SS |
113 | { |
114 | while (1) | |
115 | { | |
116 | int cmp; | |
117 | if (subset == NULL) | |
118 | return 1; | |
119 | if (superset == NULL) | |
4e0bf4c4 | 120 | return 0; /* subset isn't finished */ |
c906108c SS |
121 | cmp = strcmp (subset->member, superset->member); |
122 | if (cmp < 0) | |
4e0bf4c4 | 123 | return 0; /* not found */ |
c906108c | 124 | else if (cmp == 0) |
4e0bf4c4 | 125 | subset = subset->next; /* found */ |
c906108c | 126 | else if (cmp > 0) |
4e0bf4c4 | 127 | superset = superset->next; /* later in list? */ |
c906108c SS |
128 | } |
129 | } | |
130 | ||
131 | ||
132 | int | |
4e0bf4c4 | 133 | filter_is_common (filter *l, filter *r) |
c906108c SS |
134 | { |
135 | while (1) | |
136 | { | |
137 | int cmp; | |
138 | if (l == NULL) | |
139 | return 0; | |
140 | if (r == NULL) | |
141 | return 0; | |
142 | cmp = strcmp (l->member, r->member); | |
143 | if (cmp < 0) | |
144 | l = l->next; | |
145 | else if (cmp == 0) | |
4e0bf4c4 | 146 | return 1; /* common member */ |
c906108c SS |
147 | else if (cmp > 0) |
148 | r = r->next; | |
149 | } | |
150 | } | |
151 | ||
152 | ||
153 | int | |
4e0bf4c4 | 154 | filter_is_member (filter *filt, const char *flag) |
c906108c SS |
155 | { |
156 | int index = 1; | |
157 | while (filt != NULL) | |
158 | { | |
159 | if (strcmp (flag, filt->member) == 0) | |
160 | return index; | |
161 | filt = filt->next; | |
162 | index++; | |
163 | } | |
164 | return 0; | |
165 | } | |
166 | ||
167 | ||
168 | int | |
4e0bf4c4 | 169 | is_filtered_out (filter *filters, const char *flags) |
c906108c | 170 | { |
4e0bf4c4 AC |
171 | while (strlen (flags) > 0) |
172 | { | |
173 | int present; | |
174 | filter *filt = filters; | |
175 | /* break the string up */ | |
176 | char *end = strchr (flags, ','); | |
177 | char *next; | |
178 | unsigned /*size_t */ len; | |
179 | if (end == NULL) | |
180 | { | |
181 | end = strchr (flags, '\0'); | |
182 | next = end; | |
183 | } | |
184 | else | |
185 | { | |
186 | next = end + 1; | |
187 | } | |
188 | len = end - flags; | |
189 | /* check that it is present */ | |
190 | present = 0; | |
191 | filt = filters; | |
192 | while (filt != NULL) | |
193 | { | |
194 | if (strncmp (flags, filt->member, len) == 0 | |
195 | && strlen (filt->member) == len) | |
196 | { | |
197 | present = 1; | |
198 | break; | |
199 | } | |
200 | filt = filt->next; | |
201 | } | |
202 | if (!present) | |
203 | return 1; | |
204 | flags = next; | |
c906108c | 205 | } |
c906108c SS |
206 | return 0; |
207 | } | |
208 | ||
209 | ||
c906108c | 210 | char * |
4e0bf4c4 | 211 | filter_next (filter *set, char *member) |
c906108c SS |
212 | { |
213 | while (set != NULL) | |
214 | { | |
215 | if (strcmp (set->member, member) > 0) | |
216 | return set->member; | |
217 | set = set->next; | |
218 | } | |
219 | return NULL; | |
220 | } | |
221 | ||
222 | ||
223 | void | |
4e0bf4c4 | 224 | dump_filter (lf *file, char *prefix, filter *set, char *suffix) |
c906108c SS |
225 | { |
226 | char *member; | |
227 | lf_printf (file, "%s", prefix); | |
228 | member = filter_next (set, ""); | |
229 | if (member != NULL) | |
230 | { | |
231 | while (1) | |
232 | { | |
233 | lf_printf (file, "%s", member); | |
234 | member = filter_next (set, member); | |
235 | if (member == NULL) | |
236 | break; | |
237 | lf_printf (file, ","); | |
238 | } | |
239 | } | |
240 | lf_printf (file, "%s", suffix); | |
241 | } | |
242 | ||
243 | ||
244 | #ifdef MAIN | |
245 | int | |
4e0bf4c4 | 246 | main (int argc, char **argv) |
c906108c SS |
247 | { |
248 | filter *subset = NULL; | |
249 | filter *superset = NULL; | |
250 | lf *l; | |
251 | int i; | |
4e0bf4c4 AC |
252 | if (argc < 2) |
253 | { | |
254 | printf ("Usage: filter <subset> <filter> ...\n"); | |
255 | exit (1); | |
256 | } | |
c906108c SS |
257 | |
258 | /* load the filter up */ | |
259 | filter_parse (&subset, argv[1]); | |
4e0bf4c4 | 260 | for (i = 2; i < argc; i++) |
c906108c SS |
261 | filter_parse (&superset, argv[i]); |
262 | ||
263 | /* dump various info */ | |
264 | l = lf_open ("-", "stdout", lf_omit_references, lf_is_text, "tmp-filter"); | |
7e60d301 | 265 | |
c906108c SS |
266 | /* subset */ |
267 | { | |
268 | dump_filter (l, "{", subset, " }"); | |
269 | if (filter_is_subset (superset, subset)) | |
270 | lf_printf (l, " subset of "); | |
271 | else | |
272 | lf_printf (l, " !subset of "); | |
273 | dump_filter (l, "{", superset, " }"); | |
274 | lf_printf (l, "\n"); | |
275 | } | |
276 | /* intersection */ | |
277 | { | |
278 | dump_filter (l, "{", subset, " }"); | |
279 | if (filter_is_common (subset, superset)) | |
280 | lf_printf (l, " intersects "); | |
281 | else | |
282 | lf_printf (l, " !intersects "); | |
283 | dump_filter (l, "{", superset, " }"); | |
284 | lf_printf (l, "\n"); | |
285 | } | |
286 | /* membership */ | |
287 | { | |
288 | filter *memb = subset; | |
289 | while (memb != NULL) | |
290 | { | |
291 | lf_printf (l, "%s", memb->member); | |
292 | if (filter_is_member (superset, memb->member)) | |
293 | lf_printf (l, " in "); | |
294 | else | |
295 | lf_printf (l, " !in "); | |
296 | dump_filter (l, "{", superset, " }"); | |
297 | lf_printf (l, "\n"); | |
298 | memb = memb->next; | |
299 | } | |
300 | } | |
301 | /* addition */ | |
302 | { | |
303 | filter *add = NULL; | |
304 | filter_add (&add, superset); | |
305 | filter_add (&add, subset); | |
306 | dump_filter (l, "{", add, " }"); | |
307 | lf_printf (l, " = "); | |
308 | dump_filter (l, "{", subset, " }"); | |
309 | lf_printf (l, " + "); | |
310 | dump_filter (l, "{", superset, " }"); | |
311 | lf_printf (l, "\n"); | |
312 | } | |
4e0bf4c4 | 313 | |
c906108c SS |
314 | return 0; |
315 | } | |
316 | #endif |