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 2 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, write to the Free Software
17    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19    */
20
21
22#include <stdio.h>
23
24#include "config.h"
25
26#ifdef HAVE_STRING_H
27#include <string.h>
28#else
29#ifdef HAVE_STRINGS_H
30#include <strings.h>
31#endif
32#endif
33
34#include "misc.h"
35#include "filter.h"
36
37struct _filter {
38  char *flag;
39  filter *next;
40};
41
42
43filter *
44new_filter(const char *filt,
45	   filter *filters)
46{
47  while (strlen(filt) > 0) {
48    filter *new_filter;
49    /* break up the filt list */
50    char *end = strchr(filt, ',');
51    char *next;
52    int len;
53    if (end == NULL) {
54      end = strchr(filt, '\0');
55      next = end;
56    }
57    else {
58      next = end + 1;
59    }
60    len = end - filt;
61    /* add to filter list */
62    new_filter = ZALLOC(filter);
63    new_filter->flag = (char*)zalloc(len + 1);
64    strncpy(new_filter->flag, filt, len);
65    new_filter->next = filters;
66    filters = new_filter;
67    filt = next;
68  }
69  return filters;
70}
71
72
73int
74is_filtered_out(const char *flags,
75		filter *filters)
76{
77  while (strlen(flags) > 0) {
78    int present;
79    filter *filt = filters;
80    /* break the string up */
81    char *end = strchr(flags, ',');
82    char *next;
83    int len;
84    if (end == NULL) {
85      end = strchr(flags, '\0');
86      next = end;
87    }
88    else {
89      next = end + 1;
90    }
91    len = end - flags;
92    /* check that it is present */
93    present = 0;
94    filt = filters;
95    while (filt != NULL) {
96      if (strncmp(flags, filt->flag, len) == 0
97	  && strlen(filt->flag) == len) {
98	present = 1;
99	break;
100      }
101      filt = filt->next;
102    }
103    if (!present)
104      return 1;
105    flags = next;
106  }
107  return 0;
108}
109
110
111int
112it_is(const char *flag,
113      const char *flags)
114{
115  int flag_len = strlen(flag);
116  while (*flags != '\0') {
117    if (!strncmp(flags, flag, flag_len)
118	&& (flags[flag_len] == ',' || flags[flag_len] == '\0'))
119      return 1;
120    while (*flags != ',') {
121      if (*flags == '\0')
122	return 0;
123      flags++;
124    }
125    flags++;
126  }
127  return 0;
128}
129
130
131#ifdef MAIN
132int
133main(int argc, char **argv)
134{
135  filter *filters = NULL;
136  int i;
137  if (argc < 2) {
138    printf("Usage: filter <flags> <filter> ...\n");
139    exit (1);
140  }
141  /* load the filter up */
142  for (i = 2; i < argc; i++)
143    filters = new_filter(argv[i], filters);
144  if (is_filtered_out(argv[1], filters))
145    printf("fail\n");
146  else
147    printf("pass\n");
148  return 0;
149}
150#endif
151