Fix thinko in the placement of the .gnu.build.attributes section.
[external/binutils.git] / sim / ppc / filter.c
1 /*  This file is part of the program psim.
2
3     Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 3 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14  
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, see <http://www.gnu.org/licenses/>.
17  
18     */
19
20
21 #include <stdio.h>
22
23 #include "build-config.h"
24
25 #ifdef HAVE_STRING_H
26 #include <string.h>
27 #else
28 #ifdef HAVE_STRINGS_H
29 #include <strings.h>
30 #endif
31 #endif
32
33 #include "misc.h"
34 #include "filter.h"
35
36 struct _filter {
37   char *flag;
38   filter *next;
39 };
40
41
42 filter *
43 new_filter(const char *filt,
44            filter *filters)
45 {
46   while (strlen(filt) > 0) {
47     filter *new_filter;
48     /* break up the filt list */
49     char *end = strchr(filt, ',');
50     char *next;
51     int len;
52     if (end == NULL) {
53       end = strchr(filt, '\0');
54       next = end;
55     }
56     else {
57       next = end + 1;
58     }
59     len = end - filt;
60     /* add to filter list */
61     new_filter = ZALLOC(filter);
62     new_filter->flag = (char*)zalloc(len + 1);
63     strncpy(new_filter->flag, filt, len);
64     new_filter->next = filters;
65     filters = new_filter;
66     filt = next;
67   }
68   return filters;
69 }
70
71
72 int
73 is_filtered_out(const char *flags,
74                 filter *filters)
75 {
76   while (strlen(flags) > 0) {
77     int present;
78     filter *filt = filters;
79     /* break the string up */
80     char *end = strchr(flags, ',');
81     char *next;
82     int len;
83     if (end == NULL) {
84       end = strchr(flags, '\0');
85       next = end;
86     }
87     else {
88       next = end + 1;
89     }
90     len = end - flags;
91     /* check that it is present */
92     present = 0;
93     filt = filters;
94     while (filt != NULL) {
95       if (strncmp(flags, filt->flag, len) == 0
96           && strlen(filt->flag) == len) {
97         present = 1;
98         break;
99       }
100       filt = filt->next;
101     }
102     if (!present)
103       return 1;
104     flags = next;
105   }
106   return 0;
107 }
108
109
110 int
111 it_is(const char *flag,
112       const char *flags)
113 {
114   int flag_len = strlen(flag);
115   while (*flags != '\0') {
116     if (!strncmp(flags, flag, flag_len)
117         && (flags[flag_len] == ',' || flags[flag_len] == '\0'))
118       return 1;
119     while (*flags != ',') {
120       if (*flags == '\0')
121         return 0;
122       flags++;
123     }
124     flags++;
125   }
126   return 0;
127 }
128
129
130 #ifdef MAIN
131 int
132 main(int argc, char **argv)
133 {
134   filter *filters = NULL;
135   int i;
136   if (argc < 2) {
137     printf("Usage: filter <flags> <filter> ...\n");
138     exit (1);
139   }
140   /* load the filter up */
141   for (i = 2; i < argc; i++) 
142     filters = new_filter(argv[i], filters);
143   if (is_filtered_out(argv[1], filters))
144     printf("fail\n");
145   else
146     printf("pass\n");
147   return 0;
148 }
149 #endif