strfilter.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 *node)
  11. {
  12. if (node) {
  13. if (node->p && !is_operator(*node->p))
  14. zfree((char **)&node->p);
  15. strfilter_node__delete(node->l);
  16. strfilter_node__delete(node->r);
  17. free(node);
  18. }
  19. }
  20. void strfilter__delete(struct strfilter *filter)
  21. {
  22. if (filter) {
  23. strfilter_node__delete(filter->root);
  24. free(filter);
  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 *node = zalloc(sizeof(*node));
  57. if (node) {
  58. node->p = op;
  59. node->l = l;
  60. node->r = r;
  61. }
  62. return node;
  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 *filter = zalloc(sizeof(*filter));
  142. const char *ep = NULL;
  143. if (filter)
  144. filter->root = strfilter_node__new(rules, &ep);
  145. if (!filter || !filter->root || *ep != '\0') {
  146. if (err)
  147. *err = ep;
  148. strfilter__delete(filter);
  149. filter = NULL;
  150. }
  151. return filter;
  152. }
  153. static int strfilter__append(struct strfilter *filter, bool _or,
  154. const char *rules, const char **err)
  155. {
  156. struct strfilter_node *right, *root;
  157. const char *ep = NULL;
  158. if (!filter || !rules)
  159. return -EINVAL;
  160. right = strfilter_node__new(rules, &ep);
  161. if (!right || *ep != '\0') {
  162. if (err)
  163. *err = ep;
  164. goto error;
  165. }
  166. root = strfilter_node__alloc(_or ? OP_or : OP_and, filter->root, right);
  167. if (!root) {
  168. ep = NULL;
  169. goto error;
  170. }
  171. filter->root = root;
  172. return 0;
  173. error:
  174. strfilter_node__delete(right);
  175. return ep ? -EINVAL : -ENOMEM;
  176. }
  177. int strfilter__or(struct strfilter *filter, const char *rules, const char **err)
  178. {
  179. return strfilter__append(filter, true, rules, err);
  180. }
  181. int strfilter__and(struct strfilter *filter, const char *rules,
  182. const char **err)
  183. {
  184. return strfilter__append(filter, false, rules, err);
  185. }
  186. static bool strfilter_node__compare(struct strfilter_node *node,
  187. const char *str)
  188. {
  189. if (!node || !node->p)
  190. return false;
  191. switch (*node->p) {
  192. case '|': /* OR */
  193. return strfilter_node__compare(node->l, str) ||
  194. strfilter_node__compare(node->r, str);
  195. case '&': /* AND */
  196. return strfilter_node__compare(node->l, str) &&
  197. strfilter_node__compare(node->r, str);
  198. case '!': /* NOT */
  199. return !strfilter_node__compare(node->r, str);
  200. default:
  201. return strglobmatch(str, node->p);
  202. }
  203. }
  204. /* Return true if STR matches the filter rules */
  205. bool strfilter__compare(struct strfilter *filter, const char *str)
  206. {
  207. if (!filter)
  208. return false;
  209. return strfilter_node__compare(filter->root, str);
  210. }
  211. static int strfilter_node__sprint(struct strfilter_node *node, char *buf);
  212. /* sprint node in parenthesis if needed */
  213. static int strfilter_node__sprint_pt(struct strfilter_node *node, char *buf)
  214. {
  215. int len;
  216. int pt = node->r ? 2 : 0; /* don't need to check node->l */
  217. if (buf && pt)
  218. *buf++ = '(';
  219. len = strfilter_node__sprint(node, buf);
  220. if (len < 0)
  221. return len;
  222. if (buf && pt)
  223. *(buf + len) = ')';
  224. return len + pt;
  225. }
  226. static int strfilter_node__sprint(struct strfilter_node *node, char *buf)
  227. {
  228. int len = 0, rlen;
  229. if (!node || !node->p)
  230. return -EINVAL;
  231. switch (*node->p) {
  232. case '|':
  233. case '&':
  234. len = strfilter_node__sprint_pt(node->l, buf);
  235. if (len < 0)
  236. return len;
  237. __fallthrough;
  238. case '!':
  239. if (buf) {
  240. *(buf + len++) = *node->p;
  241. buf += len;
  242. } else
  243. len++;
  244. rlen = strfilter_node__sprint_pt(node->r, buf);
  245. if (rlen < 0)
  246. return rlen;
  247. len += rlen;
  248. break;
  249. default:
  250. len = strlen(node->p);
  251. if (buf)
  252. strcpy(buf, node->p);
  253. }
  254. return len;
  255. }
  256. char *strfilter__string(struct strfilter *filter)
  257. {
  258. int len;
  259. char *ret = NULL;
  260. len = strfilter_node__sprint(filter->root, NULL);
  261. if (len < 0)
  262. return NULL;
  263. ret = malloc(len + 1);
  264. if (ret)
  265. strfilter_node__sprint(filter->root, ret);
  266. return ret;
  267. }