476e0e717d9fb5c788651722d5e2a4948dcd696e
1 /*
2 * SysDB - src/core/store_lookup.c
3 * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 /*
29 * This module implements operators which may be used to select contents of
30 * the store by matching various attributes of the stored objects. For now, a
31 * simple full table scan is supported only.
32 */
34 #if HAVE_CONFIG_H
35 # include "config.h"
36 #endif /* HAVE_CONFIG_H */
38 #include "sysdb.h"
39 #include "core/store-private.h"
40 #include "core/object.h"
42 #include <assert.h>
44 #include <sys/types.h>
45 #include <regex.h>
47 #include <stdlib.h>
48 #include <string.h>
50 #include <limits.h>
52 /*
53 * private data types
54 */
56 typedef struct {
57 sdb_store_matcher_t *m;
58 sdb_store_matcher_t *filter;
59 sdb_store_lookup_cb cb;
60 void *user_data;
61 } scan_iter_data_t;
63 /*
64 * private helper functions
65 */
67 static int
68 scan_iter(sdb_store_obj_t *obj, void *user_data)
69 {
70 scan_iter_data_t *d = user_data;
72 if (sdb_store_matcher_matches(d->m, obj, d->filter))
73 return d->cb(obj, d->user_data);
74 return 0;
75 } /* scan_iter */
77 static sdb_attribute_t *
78 attr_get(sdb_host_t *host, const char *name, sdb_store_matcher_t *filter)
79 {
80 sdb_avltree_iter_t *iter = NULL;
81 sdb_attribute_t *attr = NULL;
83 iter = sdb_avltree_get_iter(host->attributes);
84 while (sdb_avltree_iter_has_next(iter)) {
85 sdb_attribute_t *a = ATTR(sdb_avltree_iter_get_next(iter));
87 if (strcasecmp(name, SDB_OBJ(a)->name))
88 continue;
90 assert(STORE_OBJ(a)->type == SDB_ATTRIBUTE);
91 attr = a;
92 break;
93 }
94 sdb_avltree_iter_destroy(iter);
96 if (filter && (! sdb_store_matcher_matches(filter, STORE_OBJ(attr),
97 NULL)))
98 return NULL;
99 return attr;
100 } /* attr_get */
102 /*
103 * conditional implementations
104 */
106 static int
107 attr_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond,
108 sdb_store_matcher_t *filter)
109 {
110 sdb_attribute_t *attr;
111 sdb_data_t value = SDB_DATA_INIT;
112 int status;
114 if (obj->type != SDB_HOST)
115 return INT_MAX;
117 if (sdb_store_expr_eval(ATTR_C(cond)->expr, &value))
118 return INT_MAX;
120 attr = attr_get(HOST(obj), ATTR_C(cond)->name, filter);
121 if (! attr)
122 status = INT_MAX;
123 else if (attr->value.type != value.type)
124 status = INT_MAX;
125 else
126 status = sdb_data_cmp(&attr->value, &value);
127 sdb_data_free_datum(&value);
128 return status;
129 } /* attr_cmp */
131 /*
132 * matcher implementations
133 */
135 static int
136 match_string(string_matcher_t *m, const char *name)
137 {
138 if ((! m->name) && (! m->name_re))
139 return 1;
141 if (! name)
142 name = "";
144 if (m->name && strcasecmp(m->name, name))
145 return 0;
146 if (m->name_re && regexec(m->name_re, name,
147 /* matches */ 0, NULL, /* flags = */ 0))
148 return 0;
149 return 1;
150 } /* match_string */
152 static int
153 match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
154 sdb_store_matcher_t *filter)
155 {
156 int status;
158 assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
159 assert(OP_M(m)->left && OP_M(m)->right);
161 status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter);
163 /* lazy evaluation */
164 if ((! status) && (m->type == MATCHER_AND))
165 return status;
166 else if (status && (m->type == MATCHER_OR))
167 return status;
169 return sdb_store_matcher_matches(OP_M(m)->right, obj, filter);
170 } /* match_logical */
172 static int
173 match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
174 sdb_store_matcher_t *filter)
175 {
176 assert(m->type == MATCHER_NOT);
177 assert(UOP_M(m)->op);
179 return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter);
180 } /* match_unary */
182 static int
183 match_name(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
184 sdb_store_matcher_t *filter)
185 {
186 sdb_avltree_iter_t *iter = NULL;
187 int status = 0;
189 assert(m->type == MATCHER_NAME);
191 if (obj->type == NAME_M(m)->obj_type)
192 return match_string(&NAME_M(m)->name, SDB_OBJ(obj)->name);
193 else if (obj->type != SDB_HOST)
194 return 0;
196 switch (NAME_M(m)->obj_type) {
197 case SDB_SERVICE:
198 iter = sdb_avltree_get_iter(HOST(obj)->services);
199 break;
200 case SDB_ATTRIBUTE:
201 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
202 break;
203 }
205 while (sdb_avltree_iter_has_next(iter)) {
206 sdb_object_t *child = sdb_avltree_iter_get_next(iter);
207 if (filter && (! sdb_store_matcher_matches(filter, STORE_OBJ(child),
208 NULL)))
209 continue;
210 if (match_string(&NAME_M(m)->name, child->name)) {
211 status = 1;
212 break;
213 }
214 }
215 sdb_avltree_iter_destroy(iter);
216 return status;
217 } /* match_name */
219 static int
220 match_attr(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
221 sdb_store_matcher_t *filter)
222 {
223 sdb_attribute_t *attr;
225 assert(m->type == MATCHER_ATTR);
226 assert(ATTR_M(m)->name);
228 if (obj->type != SDB_HOST)
229 return 0;
231 attr = attr_get(HOST(obj), ATTR_M(m)->name, filter);
232 if (attr) {
233 char buf[sdb_data_strlen(&attr->value) + 1];
234 if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
235 return 0;
236 if (match_string(&ATTR_M(m)->value, buf))
237 return 1;
238 }
239 return 0;
240 } /* match_attr */
242 static int
243 match_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
244 sdb_store_matcher_t *filter)
245 {
246 int status;
247 assert(m->type == MATCHER_LT);
248 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
249 return (status != INT_MAX) && (status < 0);
250 } /* match_lt */
252 static int
253 match_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
254 sdb_store_matcher_t *filter)
255 {
256 int status;
257 assert(m->type == MATCHER_LE);
258 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
259 return (status != INT_MAX) && (status <= 0);
260 } /* match_le */
262 static int
263 match_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
264 sdb_store_matcher_t *filter)
265 {
266 int status;
267 assert(m->type == MATCHER_EQ);
268 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
269 return (status != INT_MAX) && (! status);
270 } /* match_eq */
272 static int
273 match_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
274 sdb_store_matcher_t *filter)
275 {
276 int status;
277 assert(m->type == MATCHER_GE);
278 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
279 return (status != INT_MAX) && (status >= 0);
280 } /* match_ge */
282 static int
283 match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
284 sdb_store_matcher_t *filter)
285 {
286 int status;
287 assert(m->type == MATCHER_GT);
288 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
289 return (status != INT_MAX) && (status > 0);
290 } /* match_gt */
292 static int
293 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
294 sdb_store_matcher_t *filter)
295 {
296 assert(m->type == MATCHER_ISNULL);
297 if (obj->type != SDB_HOST)
298 return 0;
299 return attr_get(HOST(obj), ISNULL_M(m)->attr_name, filter) == NULL;
300 } /* match_isnull */
302 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
303 sdb_store_matcher_t *);
305 /* this array needs to be indexable by the matcher types;
306 * -> update the enum in store-private.h when updating this */
307 static matcher_cb
308 matchers[] = {
309 match_logical,
310 match_logical,
311 match_unary,
312 match_name,
313 match_attr,
314 match_lt,
315 match_le,
316 match_eq,
317 match_ge,
318 match_gt,
319 match_isnull,
320 };
322 /*
323 * private conditional types
324 */
326 static int
327 attr_cond_init(sdb_object_t *obj, va_list ap)
328 {
329 const char *name = va_arg(ap, const char *);
330 sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
332 if (! name)
333 return -1;
335 SDB_STORE_COND(obj)->cmp = attr_cmp;
337 ATTR_C(obj)->name = strdup(name);
338 if (! ATTR_C(obj)->name)
339 return -1;
340 ATTR_C(obj)->expr = expr;
341 sdb_object_ref(SDB_OBJ(expr));
342 return 0;
343 } /* attr_cond_init */
345 static void
346 attr_cond_destroy(sdb_object_t *obj)
347 {
348 if (ATTR_C(obj)->name)
349 free(ATTR_C(obj)->name);
350 sdb_object_deref(SDB_OBJ(ATTR_C(obj)->expr));
351 } /* attr_cond_destroy */
353 static sdb_type_t attr_cond_type = {
354 /* size = */ sizeof(attr_cond_t),
355 /* init = */ attr_cond_init,
356 /* destroy = */ attr_cond_destroy,
357 };
359 /*
360 * private matcher types
361 */
363 /* initializes a string matcher consuming two elements from ap */
364 static int
365 string_matcher_init(string_matcher_t *m, va_list ap)
366 {
367 const char *name = va_arg(ap, const char *);
368 const char *name_re = va_arg(ap, const char *);
370 if (name) {
371 m->name = strdup(name);
372 if (! m->name)
373 return -1;
374 }
375 if (name_re) {
376 m->name_re = malloc(sizeof(*m->name_re));
377 if (! m->name_re)
378 return -1;
379 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
380 return -1;
381 }
382 return 0;
383 } /* string_matcher_init */
385 static void
386 string_matcher_destroy(string_matcher_t *m)
387 {
388 if (m->name)
389 free(m->name);
390 if (m->name_re) {
391 regfree(m->name_re);
392 free(m->name_re);
393 }
394 } /* string_matcher_destroy */
396 static char *
397 string_tostring(string_matcher_t *m, char *buf, size_t buflen)
398 {
399 snprintf(buf, buflen, "{ %s%s%s, %p }",
400 m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
401 m->name_re);
402 return buf;
403 } /* string_tostring */
405 /* initializes a name matcher */
406 static int
407 name_matcher_init(sdb_object_t *obj, va_list ap)
408 {
409 name_matcher_t *m = NAME_M(obj);
410 M(obj)->type = MATCHER_NAME;
411 return string_matcher_init(&m->name, ap);
412 } /* name_matcher_init */
414 static void
415 name_matcher_destroy(sdb_object_t *obj)
416 {
417 name_matcher_t *m = NAME_M(obj);
418 string_matcher_destroy(&m->name);
419 } /* name_matcher_destroy */
421 static char *
422 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
423 {
424 char name[buflen + 1];
425 assert(m->type == MATCHER_NAME);
426 snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
427 SDB_STORE_TYPE_TO_NAME(NAME_M(m)->obj_type),
428 string_tostring(&NAME_M(m)->name, name, sizeof(name)));
429 return buf;
430 } /* name_tostring */
432 static int
433 attr_matcher_init(sdb_object_t *obj, va_list ap)
434 {
435 attr_matcher_t *attr = ATTR_M(obj);
436 const char *name = va_arg(ap, const char *);
438 M(obj)->type = MATCHER_ATTR;
439 if (name) {
440 attr->name = strdup(name);
441 if (! attr->name)
442 return -1;
443 }
444 return string_matcher_init(&attr->value, ap);
445 } /* attr_matcher_init */
447 static void
448 attr_matcher_destroy(sdb_object_t *obj)
449 {
450 attr_matcher_t *attr = ATTR_M(obj);
451 if (attr->name)
452 free(attr->name);
453 attr->name = NULL;
454 string_matcher_destroy(&attr->value);
455 } /* attr_matcher_destroy */
457 static char *
458 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
459 {
460 char value[buflen + 1];
462 if (! m) {
463 snprintf(buf, buflen, "ATTR{}");
464 return buf;
465 }
467 assert(m->type == MATCHER_ATTR);
468 snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
469 string_tostring(&ATTR_M(m)->value, value, sizeof(value)));
470 return buf;
471 } /* attr_tostring */
473 static int
474 cond_matcher_init(sdb_object_t *obj, va_list ap)
475 {
476 int type = va_arg(ap, int);
477 sdb_store_cond_t *cond = va_arg(ap, sdb_store_cond_t *);
479 if (! cond)
480 return -1;
482 sdb_object_ref(SDB_OBJ(cond));
484 M(obj)->type = type;
485 COND_M(obj)->cond = cond;
486 return 0;
487 } /* cond_matcher_init */
489 static void
490 cond_matcher_destroy(sdb_object_t *obj)
491 {
492 sdb_object_deref(SDB_OBJ(COND_M(obj)->cond));
493 } /* cond_matcher_destroy */
495 static char *
496 cond_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
497 {
498 if (COND_M(m)->cond->cmp == attr_cmp) {
499 sdb_data_t value = SDB_DATA_INIT;
500 char value_str[buflen];
501 if (sdb_store_expr_eval(ATTR_C(COND_M(m)->cond)->expr, &value))
502 snprintf(value_str, sizeof(value_str), "ERR");
503 else if (sdb_data_format(&value, value_str, sizeof(value_str),
504 SDB_SINGLE_QUOTED) < 0)
505 snprintf(value_str, sizeof(value_str), "ERR");
506 snprintf(buf, buflen, "ATTR[%s]{ %s %s }",
507 ATTR_C(COND_M(m)->cond)->name, MATCHER_SYM(m->type),
508 value_str);
509 sdb_data_free_datum(&value);
510 }
511 else
512 snprintf(buf, buflen, "<unknown>");
513 return buf;
514 } /* cond_tostring */
516 static int
517 op_matcher_init(sdb_object_t *obj, va_list ap)
518 {
519 M(obj)->type = va_arg(ap, int);
520 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
521 return -1;
523 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
524 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
525 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
526 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
528 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
529 return -1;
530 return 0;
531 } /* op_matcher_init */
533 static void
534 op_matcher_destroy(sdb_object_t *obj)
535 {
536 if (OP_M(obj)->left)
537 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
538 if (OP_M(obj)->right)
539 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
540 } /* op_matcher_destroy */
542 static char *
543 op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
544 {
545 char left[buflen + 1], right[buflen + 1];
547 if (! m) {
548 /* this should not happen */
549 snprintf(buf, buflen, "()");
550 return buf;
551 }
553 assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
554 snprintf(buf, buflen, "(%s, %s, %s)",
555 m->type == MATCHER_OR ? "OR" : "AND",
556 sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
557 sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
558 return buf;
559 } /* op_tostring */
561 static int
562 uop_matcher_init(sdb_object_t *obj, va_list ap)
563 {
564 M(obj)->type = va_arg(ap, int);
565 if (M(obj)->type != MATCHER_NOT)
566 return -1;
568 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
569 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
571 if (! UOP_M(obj)->op)
572 return -1;
573 return 0;
574 } /* uop_matcher_init */
576 static void
577 uop_matcher_destroy(sdb_object_t *obj)
578 {
579 if (UOP_M(obj)->op)
580 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
581 } /* uop_matcher_destroy */
583 static char *
584 uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
585 {
586 char op[buflen + 1];
588 if (! m) {
589 /* this should not happen */
590 snprintf(buf, buflen, "()");
591 return buf;
592 }
594 assert(m->type == MATCHER_NOT);
595 snprintf(buf, buflen, "(NOT, %s)",
596 sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
597 return buf;
598 } /* uop_tostring */
600 static int
601 isnull_matcher_init(sdb_object_t *obj, va_list ap)
602 {
603 const char *name;
605 M(obj)->type = va_arg(ap, int);
606 if (M(obj)->type != MATCHER_ISNULL)
607 return -1;
609 name = va_arg(ap, const char *);
610 if (! name)
611 return -1;
612 ISNULL_M(obj)->attr_name = strdup(name);
613 if (! ISNULL_M(obj)->attr_name)
614 return -1;
615 return 0;
616 } /* isnull_matcher_init */
618 static void
619 isnull_matcher_destroy(sdb_object_t *obj)
620 {
621 if (ISNULL_M(obj)->attr_name)
622 free(ISNULL_M(obj)->attr_name);
623 ISNULL_M(obj)->attr_name = NULL;
624 } /* isnull_matcher_destroy */
626 static char *
627 isnull_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
628 {
629 snprintf(buf, buflen, "(IS NULL, ATTR[%s])", ISNULL_M(m)->attr_name);
630 return buf;
631 } /* isnull_tostring */
633 static sdb_type_t name_type = {
634 /* size = */ sizeof(name_matcher_t),
635 /* init = */ name_matcher_init,
636 /* destroy = */ name_matcher_destroy,
637 };
639 static sdb_type_t attr_type = {
640 /* size = */ sizeof(attr_matcher_t),
641 /* init = */ attr_matcher_init,
642 /* destroy = */ attr_matcher_destroy,
643 };
645 static sdb_type_t cond_type = {
646 /* size = */ sizeof(cond_matcher_t),
647 /* init = */ cond_matcher_init,
648 /* destroy = */ cond_matcher_destroy,
649 };
651 static sdb_type_t op_type = {
652 /* size = */ sizeof(op_matcher_t),
653 /* init = */ op_matcher_init,
654 /* destroy = */ op_matcher_destroy,
655 };
657 static sdb_type_t uop_type = {
658 /* size = */ sizeof(uop_matcher_t),
659 /* init = */ uop_matcher_init,
660 /* destroy = */ uop_matcher_destroy,
661 };
663 static sdb_type_t isnull_type = {
664 /* size = */ sizeof(isnull_matcher_t),
665 /* init = */ isnull_matcher_init,
666 /* destroy = */ isnull_matcher_destroy,
667 };
669 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
671 /* this array needs to be indexable by the matcher types;
672 * -> update the enum in store-private.h when updating this */
673 static matcher_tostring_cb
674 matchers_tostring[] = {
675 op_tostring,
676 op_tostring,
677 uop_tostring,
678 name_tostring,
679 attr_tostring,
680 cond_tostring,
681 cond_tostring,
682 cond_tostring,
683 cond_tostring,
684 cond_tostring,
685 isnull_tostring,
686 };
688 /*
689 * public API
690 */
692 sdb_store_cond_t *
693 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr)
694 {
695 return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type,
696 name, expr));
697 } /* sdb_store_attr_cond */
699 sdb_store_matcher_t *
700 sdb_store_name_matcher(int type, const char *name, _Bool re)
701 {
702 sdb_store_matcher_t *m;
704 if (re)
705 m = M(sdb_object_create("name-matcher", name_type, NULL, name));
706 else
707 m = M(sdb_object_create("name-matcher", name_type, name, NULL));
709 if (! m)
710 return NULL;
712 NAME_M(m)->obj_type = type;
713 return m;
714 } /* sdb_store_name_matcher */
716 sdb_store_matcher_t *
717 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
718 {
719 sdb_store_matcher_t *m;
721 if (! name)
722 return NULL;
724 if (re)
725 m = M(sdb_object_create("attr-matcher", attr_type,
726 name, NULL, value));
727 else
728 m = M(sdb_object_create("attr-matcher", attr_type,
729 name, value, NULL));
730 return m;
731 } /* sdb_store_attr_matcher */
733 sdb_store_matcher_t *
734 sdb_store_lt_matcher(sdb_store_cond_t *cond)
735 {
736 return M(sdb_object_create("lt-matcher", cond_type,
737 MATCHER_LT, cond));
738 } /* sdb_store_lt_matcher */
740 sdb_store_matcher_t *
741 sdb_store_le_matcher(sdb_store_cond_t *cond)
742 {
743 return M(sdb_object_create("le-matcher", cond_type,
744 MATCHER_LE, cond));
745 } /* sdb_store_le_matcher */
747 sdb_store_matcher_t *
748 sdb_store_eq_matcher(sdb_store_cond_t *cond)
749 {
750 return M(sdb_object_create("eq-matcher", cond_type,
751 MATCHER_EQ, cond));
752 } /* sdb_store_eq_matcher */
754 sdb_store_matcher_t *
755 sdb_store_ge_matcher(sdb_store_cond_t *cond)
756 {
757 return M(sdb_object_create("ge-matcher", cond_type,
758 MATCHER_GE, cond));
759 } /* sdb_store_ge_matcher */
761 sdb_store_matcher_t *
762 sdb_store_gt_matcher(sdb_store_cond_t *cond)
763 {
764 return M(sdb_object_create("gt-matcher", cond_type,
765 MATCHER_GT, cond));
766 } /* sdb_store_gt_matcher */
768 sdb_store_matcher_t *
769 sdb_store_isnull_matcher(const char *attr_name)
770 {
771 return M(sdb_object_create("isnull-matcher", isnull_type,
772 MATCHER_ISNULL, attr_name));
773 } /* sdb_store_isnull_matcher */
775 static sdb_store_matcher_t *
776 parse_attr_cmp(const char *attr, const char *op, sdb_store_expr_t *expr)
777 {
778 sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
779 sdb_store_matcher_t *m;
780 sdb_store_cond_t *cond;
781 _Bool inv = 0;
783 if (! attr)
784 return NULL;
786 if (! strcasecmp(op, "IS")) {
787 if (! expr)
788 return sdb_store_isnull_matcher(attr);
789 else
790 return NULL;
791 }
792 else if (! expr)
793 return NULL;
794 else if (! strcasecmp(op, "<"))
795 matcher = sdb_store_lt_matcher;
796 else if (! strcasecmp(op, "<="))
797 matcher = sdb_store_le_matcher;
798 else if (! strcasecmp(op, "="))
799 matcher = sdb_store_eq_matcher;
800 else if (! strcasecmp(op, ">="))
801 matcher = sdb_store_ge_matcher;
802 else if (! strcasecmp(op, ">"))
803 matcher = sdb_store_gt_matcher;
804 else if (! strcasecmp(op, "!=")) {
805 matcher = sdb_store_eq_matcher;
806 inv = 1;
807 }
808 else
809 return NULL;
811 cond = sdb_store_attr_cond(attr, expr);
812 if (! cond)
813 return NULL;
815 m = matcher(cond);
816 /* pass ownership to 'm' or destroy in case of an error */
817 sdb_object_deref(SDB_OBJ(cond));
818 if (! m)
819 return NULL;
821 if (inv) {
822 sdb_store_matcher_t *tmp;
823 tmp = sdb_store_inv_matcher(m);
824 /* pass ownership to the inverse matcher */
825 sdb_object_deref(SDB_OBJ(m));
826 m = tmp;
827 }
828 return m;
829 } /* parse_attr_cmp */
831 sdb_store_matcher_t *
832 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
833 const char *op, sdb_store_expr_t *expr)
834 {
835 int type = -1;
836 _Bool inv = 0;
837 _Bool re = 0;
839 sdb_data_t value = SDB_DATA_INIT;
840 sdb_store_matcher_t *m = NULL;
842 if (! strcasecmp(obj_type, "host"))
843 type = SDB_HOST;
844 else if (! strcasecmp(obj_type, "service"))
845 type = SDB_SERVICE;
846 else if (! strcasecmp(obj_type, "attribute"))
847 type = SDB_ATTRIBUTE;
848 else
849 return NULL;
851 /* XXX: this code sucks! */
852 if (! strcasecmp(op, "=")) {
853 /* nothing to do */
854 }
855 else if (! strcasecmp(op, "!=")) {
856 inv = 1;
857 }
858 else if (! strcasecmp(op, "=~")) {
859 re = 1;
860 }
861 else if (! strcasecmp(op, "!~")) {
862 inv = 1;
863 re = 1;
864 }
865 else if (type == SDB_ATTRIBUTE)
866 return parse_attr_cmp(attr, op, expr);
867 else
868 return NULL;
870 if (! expr)
871 return NULL;
873 if (sdb_store_expr_eval(expr, &value))
874 return NULL;
875 if (value.type != SDB_TYPE_STRING) {
876 sdb_data_free_datum(&value);
877 return parse_attr_cmp(attr, op, expr);
878 }
880 if (! attr)
881 m = sdb_store_name_matcher(type, value.data.string, re);
882 else if (type == SDB_ATTRIBUTE)
883 m = sdb_store_attr_matcher(attr, value.data.string, re);
885 sdb_data_free_datum(&value);
887 if (! m)
888 return NULL;
890 if (inv) {
891 sdb_store_matcher_t *tmp;
892 tmp = sdb_store_inv_matcher(m);
893 /* pass ownership to the inverse matcher */
894 sdb_object_deref(SDB_OBJ(m));
895 m = tmp;
896 }
897 return m;
898 } /* sdb_store_matcher_parse_cmp */
900 sdb_store_matcher_t *
901 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
902 {
903 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
904 left, right));
905 } /* sdb_store_dis_matcher */
907 sdb_store_matcher_t *
908 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
909 {
910 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
911 left, right));
912 } /* sdb_store_con_matcher */
914 sdb_store_matcher_t *
915 sdb_store_inv_matcher(sdb_store_matcher_t *m)
916 {
917 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
918 } /* sdb_store_inv_matcher */
920 int
921 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
922 sdb_store_matcher_t *filter)
923 {
924 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
925 return 0;
927 /* "NULL" always matches */
928 if ((! m) || (! obj))
929 return 1;
931 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
932 return 0;
934 return matchers[m->type](m, obj, filter);
935 } /* sdb_store_matcher_matches */
937 char *
938 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
939 {
940 if (! m)
941 return NULL;
943 if ((m->type < 0)
944 || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
945 return NULL;
946 return matchers_tostring[m->type](m, buf, buflen);
947 } /* sdb_store_matcher_tostring */
949 int
950 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
951 sdb_store_lookup_cb cb, void *user_data)
952 {
953 scan_iter_data_t data = { m, filter, cb, user_data };
955 if (! cb)
956 return -1;
957 return sdb_store_iterate(scan_iter, &data);
958 } /* sdb_store_scan */
960 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */