filter.c 3.0 KB

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