strfilter.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "util.h"
  2. #include "string.h"
  3. #include "strfilter.h"
  4. /* Operators */
  5. static const char *OP_and = "&"; /* Logical AND */
  6. static const char *OP_or = "|"; /* Logical OR */
  7. static const char *OP_not = "!"; /* Logical NOT */
  8. #define is_operator(c) ((c) == '|' || (c) == '&' || (c) == '!')
  9. #define is_separator(c) (is_operator(c) || (c) == '(' || (c) == ')')
  10. static void strfilter_node__delete(struct strfilter_node *self)
  11. {
  12. if (self) {
  13. if (self->p && !is_operator(*self->p))
  14. free((char *)self->p);
  15. strfilter_node__delete(self->l);
  16. strfilter_node__delete(self->r);
  17. free(self);
  18. }
  19. }
  20. void strfilter__delete(struct strfilter *self)
  21. {
  22. if (self) {
  23. strfilter_node__delete(self->root);
  24. free(self);
  25. }
  26. }
  27. static const char *get_token(const char *s, const char **e)
  28. {
  29. const char *p;
  30. while (isspace(*s)) /* Skip spaces */
  31. s++;
  32. if (*s == '\0') {
  33. p = s;
  34. goto end;
  35. }
  36. p = s + 1;
  37. if (!is_separator(*s)) {
  38. /* End search */
  39. retry:
  40. while (*p && !is_separator(*p) && !isspace(*p))
  41. p++;
  42. /* Escape and special case: '!' is also used in glob pattern */
  43. if (*(p - 1) == '\\' || (*p == '!' && *(p - 1) == '[')) {
  44. p++;
  45. goto retry;
  46. }
  47. }
  48. end:
  49. *e = p;
  50. return s;
  51. }
  52. static struct strfilter_node *strfilter_node__alloc(const char *op,
  53. struct strfilter_node *l,
  54. struct strfilter_node *r)
  55. {
  56. struct strfilter_node *ret = zalloc(sizeof(struct strfilter_node));
  57. if (ret) {
  58. ret->p = op;
  59. ret->l = l;
  60. ret->r = r;
  61. }
  62. return ret;
  63. }
  64. static struct strfilter_node *strfilter_node__new(const char *s,
  65. const char **ep)
  66. {
  67. struct strfilter_node root, *cur, *last_op;
  68. const char *e;
  69. if (!s)
  70. return NULL;
  71. memset(&root, 0, sizeof(root));
  72. last_op = cur = &root;
  73. s = get_token(s, &e);
  74. while (*s != '\0' && *s != ')') {
  75. switch (*s) {
  76. case '&': /* Exchg last OP->r with AND */
  77. if (!cur->r || !last_op->r)
  78. goto error;
  79. cur = strfilter_node__alloc(OP_and, last_op->r, NULL);
  80. if (!cur)
  81. goto nomem;
  82. last_op->r = cur;
  83. last_op = cur;
  84. break;
  85. case '|': /* Exchg the root with OR */
  86. if (!cur->r || !root.r)
  87. goto error;
  88. cur = strfilter_node__alloc(OP_or, root.r, NULL);
  89. if (!cur)
  90. goto nomem;
  91. root.r = cur;
  92. last_op = cur;
  93. break;
  94. case '!': /* Add NOT as a leaf node */
  95. if (cur->r)
  96. goto error;
  97. cur->r = strfilter_node__alloc(OP_not, NULL, NULL);
  98. if (!cur->r)
  99. goto nomem;
  100. cur = cur->r;
  101. break;
  102. case '(': /* Recursively parses inside the parenthesis */
  103. if (cur->r)
  104. goto error;
  105. cur->r = strfilter_node__new(s + 1, &s);
  106. if (!s)
  107. goto nomem;
  108. if (!cur->r || *s != ')')
  109. goto error;
  110. e = s + 1;
  111. break;
  112. default:
  113. if (cur->r)
  114. goto error;
  115. cur->r = strfilter_node__alloc(NULL, NULL, NULL);
  116. if (!cur->r)
  117. goto nomem;
  118. cur->r->p = strndup(s, e - s);
  119. if (!cur->r->p)
  120. goto nomem;
  121. }
  122. s = get_token(e, &e);
  123. }
  124. if (!cur->r)
  125. goto error;
  126. *ep = s;
  127. return root.r;
  128. nomem:
  129. s = NULL;
  130. error:
  131. *ep = s;
  132. strfilter_node__delete(root.r);
  133. return NULL;
  134. }
  135. /*
  136. * Parse filter rule and return new strfilter.
  137. * Return NULL if fail, and *ep == NULL if memory allocation failed.
  138. */
  139. struct strfilter *strfilter__new(const char *rules, const char **err)
  140. {
  141. struct strfilter *ret = zalloc(sizeof(struct strfilter));
  142. const char *ep = NULL;
  143. if (ret)
  144. ret->root = strfilter_node__new(rules, &ep);
  145. if (!ret || !ret->root || *ep != '\0') {
  146. if (err)
  147. *err = ep;
  148. strfilter__delete(ret);
  149. ret = NULL;
  150. }
  151. return ret;
  152. }
  153. static bool strfilter_node__compare(struct strfilter_node *self,
  154. const char *str)
  155. {
  156. if (!self || !self->p)
  157. return false;
  158. switch (*self->p) {
  159. case '|': /* OR */
  160. return strfilter_node__compare(self->l, str) ||
  161. strfilter_node__compare(self->r, str);
  162. case '&': /* AND */
  163. return strfilter_node__compare(self->l, str) &&
  164. strfilter_node__compare(self->r, str);
  165. case '!': /* NOT */
  166. return !strfilter_node__compare(self->r, str);
  167. default:
  168. return strglobmatch(str, self->p);
  169. }
  170. }
  171. /* Return true if STR matches the filter rules */
  172. bool strfilter__compare(struct strfilter *self, const char *str)
  173. {
  174. if (!self)
  175. return false;
  176. return strfilter_node__compare(self->root, str);
  177. }