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_lookup_cb cb;
59 void *user_data;
60 } lookup_iter_data_t;
62 /*
63 * private helper functions
64 */
66 static int
67 lookup_iter(sdb_store_obj_t *obj, void *user_data)
68 {
69 lookup_iter_data_t *d = user_data;
71 if (sdb_store_matcher_matches(d->m, obj))
72 return d->cb(obj, d->user_data);
73 return 0;
74 } /* lookup_iter */
76 static sdb_attribute_t *
77 attr_get(sdb_host_t *host, const char *name)
78 {
79 sdb_llist_iter_t *iter = NULL;
80 sdb_attribute_t *attr = NULL;
82 iter = sdb_llist_get_iter(host->attributes);
83 while (sdb_llist_iter_has_next(iter)) {
84 sdb_attribute_t *a = ATTR(sdb_llist_iter_get_next(iter));
86 if (strcasecmp(name, SDB_OBJ(a)->name))
87 continue;
89 assert(STORE_OBJ(a)->type == SDB_ATTRIBUTE);
90 attr = a;
91 break;
92 }
93 sdb_llist_iter_destroy(iter);
94 return attr;
95 } /* attr_get */
97 /*
98 * conditional implementations
99 */
101 static int
102 attr_cmp(sdb_host_t *host, sdb_store_cond_t *cond)
103 {
104 sdb_attribute_t *attr;
106 attr = attr_get(host, ATTR_C(cond)->name);
107 if (! attr)
108 return INT_MAX;
109 if (attr->value.type != ATTR_C(cond)->value.type)
110 return INT_MAX;
111 return sdb_data_cmp(&attr->value, &ATTR_C(cond)->value);
112 } /* attr_cmp */
114 /*
115 * matcher implementations
116 */
118 static int
119 match_string(string_matcher_t *m, const char *name)
120 {
121 if ((! m->name) && (! m->name_re))
122 return 1;
124 if (! name)
125 name = "";
127 if (m->name && strcasecmp(m->name, name))
128 return 0;
129 if (m->name_re && regexec(m->name_re, name,
130 /* matches */ 0, NULL, /* flags = */ 0))
131 return 0;
132 return 1;
133 } /* match_string */
135 static int
136 match_logical(sdb_store_matcher_t *m, sdb_host_t *host)
137 {
138 int status;
140 assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
141 assert(OP_M(m)->left && OP_M(m)->right);
143 status = sdb_store_matcher_matches(OP_M(m)->left, STORE_OBJ(host));
144 /* lazy evaluation */
145 if ((! status) && (m->type == MATCHER_AND))
146 return status;
147 else if (status && (m->type == MATCHER_OR))
148 return status;
150 return sdb_store_matcher_matches(OP_M(m)->right, STORE_OBJ(host));
151 } /* match_logical */
153 static int
154 match_unary(sdb_store_matcher_t *m, sdb_host_t *host)
155 {
156 assert(m->type == MATCHER_NOT);
157 assert(UOP_M(m)->op);
159 return !sdb_store_matcher_matches(UOP_M(m)->op, STORE_OBJ(host));
160 } /* match_unary */
162 static int
163 match_name(sdb_store_matcher_t *m, sdb_host_t *host)
164 {
165 sdb_llist_iter_t *iter = NULL;
166 int status = 0;
168 assert(m->type == MATCHER_NAME);
170 switch (NAME_M(m)->obj_type) {
171 case SDB_HOST:
172 return match_string(&NAME_M(m)->name, SDB_OBJ(host)->name);
173 break;
174 case SDB_SERVICE:
175 iter = sdb_llist_get_iter(host->services);
176 break;
177 case SDB_ATTRIBUTE:
178 iter = sdb_llist_get_iter(host->attributes);
179 break;
180 }
182 while (sdb_llist_iter_has_next(iter)) {
183 sdb_object_t *child = sdb_llist_iter_get_next(iter);
184 if (match_string(&NAME_M(m)->name, child->name)) {
185 status = 1;
186 break;
187 }
188 }
189 sdb_llist_iter_destroy(iter);
190 return status;
191 } /* match_name */
193 static int
194 match_attr(sdb_store_matcher_t *m, sdb_host_t *host)
195 {
196 sdb_attribute_t *attr;
198 assert(m->type == MATCHER_ATTR);
199 assert(ATTR_M(m)->name);
201 attr = attr_get(host, ATTR_M(m)->name);
202 if (attr) {
203 char buf[sdb_data_strlen(&attr->value) + 1];
204 if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
205 return 0;
206 if (match_string(&ATTR_M(m)->value, buf))
207 return 1;
208 }
209 return 0;
210 } /* match_attr */
212 static int
213 match_lt(sdb_store_matcher_t *m, sdb_host_t *host)
214 {
215 int status;
216 assert(m->type == MATCHER_LT);
217 status = COND_M(m)->cond->cmp(host, COND_M(m)->cond);
218 return (status != INT_MAX) && (status < 0);
219 } /* match_lt */
221 static int
222 match_le(sdb_store_matcher_t *m, sdb_host_t *host)
223 {
224 int status;
225 assert(m->type == MATCHER_LE);
226 status = COND_M(m)->cond->cmp(host, COND_M(m)->cond);
227 return (status != INT_MAX) && (status <= 0);
228 } /* match_le */
230 static int
231 match_eq(sdb_store_matcher_t *m, sdb_host_t *host)
232 {
233 int status;
234 assert(m->type == MATCHER_EQ);
235 status = COND_M(m)->cond->cmp(host, COND_M(m)->cond);
236 return (status != INT_MAX) && (! status);
237 } /* match_eq */
239 static int
240 match_ge(sdb_store_matcher_t *m, sdb_host_t *host)
241 {
242 int status;
243 assert(m->type == MATCHER_GE);
244 status = COND_M(m)->cond->cmp(host, COND_M(m)->cond);
245 return (status != INT_MAX) && (status >= 0);
246 } /* match_ge */
248 static int
249 match_gt(sdb_store_matcher_t *m, sdb_host_t *host)
250 {
251 int status;
252 assert(m->type == MATCHER_GT);
253 status = COND_M(m)->cond->cmp(host, COND_M(m)->cond);
254 return (status != INT_MAX) && (status > 0);
255 } /* match_gt */
257 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_host_t *);
259 /* this array needs to be indexable by the matcher types;
260 * -> update the enum in store-private.h when updating this */
261 static matcher_cb matchers[] = {
262 match_logical,
263 match_logical,
264 match_unary,
265 match_name,
266 match_attr,
267 match_lt,
268 match_le,
269 match_eq,
270 match_ge,
271 match_gt,
272 };
274 /*
275 * private conditional types
276 */
278 static int
279 attr_cond_init(sdb_object_t *obj, va_list ap)
280 {
281 const char *name = va_arg(ap, const char *);
282 const sdb_data_t *value = va_arg(ap, const sdb_data_t *);
284 if (! name)
285 return -1;
287 SDB_STORE_COND(obj)->cmp = attr_cmp;
289 ATTR_C(obj)->name = strdup(name);
290 if (! ATTR_C(obj)->name)
291 return -1;
292 if (sdb_data_copy(&ATTR_C(obj)->value, value))
293 return -1;
294 return 0;
295 } /* attr_cond_init */
297 static void
298 attr_cond_destroy(sdb_object_t *obj)
299 {
300 if (ATTR_C(obj)->name)
301 free(ATTR_C(obj)->name);
302 sdb_data_free_datum(&ATTR_C(obj)->value);
303 } /* attr_cond_destroy */
305 static sdb_type_t attr_cond_type = {
306 /* size = */ sizeof(attr_cond_t),
307 /* init = */ attr_cond_init,
308 /* destroy = */ attr_cond_destroy,
309 };
311 /*
312 * private matcher types
313 */
315 /* initializes a string matcher consuming two elements from ap */
316 static int
317 string_matcher_init(string_matcher_t *m, va_list ap)
318 {
319 const char *name = va_arg(ap, const char *);
320 const char *name_re = va_arg(ap, const char *);
322 if (name) {
323 m->name = strdup(name);
324 if (! m->name)
325 return -1;
326 }
327 if (name_re) {
328 m->name_re = malloc(sizeof(*m->name_re));
329 if (! m->name_re)
330 return -1;
331 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
332 return -1;
333 }
334 return 0;
335 } /* string_matcher_init */
337 static void
338 string_matcher_destroy(string_matcher_t *m)
339 {
340 if (m->name)
341 free(m->name);
342 if (m->name_re) {
343 regfree(m->name_re);
344 free(m->name_re);
345 }
346 } /* string_matcher_destroy */
348 static char *
349 string_tostring(string_matcher_t *m, char *buf, size_t buflen)
350 {
351 snprintf(buf, buflen, "{ %s%s%s, %p }",
352 m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
353 m->name_re);
354 return buf;
355 } /* string_tostring */
357 /* initializes a name matcher */
358 static int
359 name_matcher_init(sdb_object_t *obj, va_list ap)
360 {
361 name_matcher_t *m = NAME_M(obj);
362 M(obj)->type = MATCHER_NAME;
363 return string_matcher_init(&m->name, ap);
364 } /* name_matcher_init */
366 static void
367 name_matcher_destroy(sdb_object_t *obj)
368 {
369 name_matcher_t *m = NAME_M(obj);
370 string_matcher_destroy(&m->name);
371 } /* name_matcher_destroy */
373 static char *
374 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
375 {
376 char name[buflen + 1];
377 assert(m->type == MATCHER_NAME);
378 snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
379 SDB_STORE_TYPE_TO_NAME(NAME_M(m)->obj_type),
380 string_tostring(&NAME_M(m)->name, name, sizeof(name)));
381 return buf;
382 } /* name_tostring */
384 static int
385 attr_matcher_init(sdb_object_t *obj, va_list ap)
386 {
387 attr_matcher_t *attr = ATTR_M(obj);
388 const char *name = va_arg(ap, const char *);
390 M(obj)->type = MATCHER_ATTR;
391 if (name) {
392 attr->name = strdup(name);
393 if (! attr->name)
394 return -1;
395 }
396 return string_matcher_init(&attr->value, ap);
397 } /* attr_matcher_init */
399 static void
400 attr_matcher_destroy(sdb_object_t *obj)
401 {
402 attr_matcher_t *attr = ATTR_M(obj);
403 if (attr->name)
404 free(attr->name);
405 attr->name = NULL;
406 string_matcher_destroy(&attr->value);
407 } /* attr_matcher_destroy */
409 static char *
410 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
411 {
412 char value[buflen + 1];
414 if (! m) {
415 snprintf(buf, buflen, "ATTR{}");
416 return buf;
417 }
419 assert(m->type == MATCHER_ATTR);
420 snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
421 string_tostring(&ATTR_M(m)->value, value, sizeof(value)));
422 return buf;
423 } /* attr_tostring */
425 static int
426 cond_matcher_init(sdb_object_t *obj, va_list ap)
427 {
428 int type = va_arg(ap, int);
429 sdb_store_cond_t *cond = va_arg(ap, sdb_store_cond_t *);
431 if (! cond)
432 return -1;
434 sdb_object_ref(SDB_OBJ(cond));
436 M(obj)->type = type;
437 COND_M(obj)->cond = cond;
438 return 0;
439 } /* cond_matcher_init */
441 static void
442 cond_matcher_destroy(sdb_object_t *obj)
443 {
444 sdb_object_deref(SDB_OBJ(COND_M(obj)->cond));
445 } /* cond_matcher_destroy */
447 static char *
448 cond_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
449 {
450 if (COND_M(m)->cond->cmp == attr_cmp) {
451 char value[buflen];
452 if (sdb_data_format(&ATTR_C(COND_M(m)->cond)->value,
453 value, sizeof(value), SDB_SINGLE_QUOTED) < 0)
454 snprintf(value, sizeof(value), "ERR");
455 snprintf(buf, buflen, "ATTR[%s]{ %s %s }",
456 ATTR_C(COND_M(m)->cond)->name, MATCHER_SYM(m->type), value);
457 }
458 return buf;
459 } /* cond_tostring */
461 static int
462 op_matcher_init(sdb_object_t *obj, va_list ap)
463 {
464 M(obj)->type = va_arg(ap, int);
465 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
466 return -1;
468 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
469 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
470 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
471 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
473 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
474 return -1;
475 return 0;
476 } /* op_matcher_init */
478 static void
479 op_matcher_destroy(sdb_object_t *obj)
480 {
481 if (OP_M(obj)->left)
482 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
483 if (OP_M(obj)->right)
484 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
485 } /* op_matcher_destroy */
487 static char *
488 op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
489 {
490 char left[buflen + 1], right[buflen + 1];
492 if (! m) {
493 /* this should not happen */
494 snprintf(buf, buflen, "()");
495 return buf;
496 }
498 assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
499 snprintf(buf, buflen, "(%s, %s, %s)",
500 m->type == MATCHER_OR ? "OR" : "AND",
501 sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
502 sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
503 return buf;
504 } /* op_tostring */
506 static int
507 uop_matcher_init(sdb_object_t *obj, va_list ap)
508 {
509 M(obj)->type = va_arg(ap, int);
510 if (M(obj)->type != MATCHER_NOT)
511 return -1;
513 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
514 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
516 if (! UOP_M(obj)->op)
517 return -1;
518 return 0;
519 } /* uop_matcher_init */
521 static void
522 uop_matcher_destroy(sdb_object_t *obj)
523 {
524 if (UOP_M(obj)->op)
525 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
526 } /* uop_matcher_destroy */
528 static char *
529 uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
530 {
531 char op[buflen + 1];
533 if (! m) {
534 /* this should not happen */
535 snprintf(buf, buflen, "()");
536 return buf;
537 }
539 assert(m->type == MATCHER_NOT);
540 snprintf(buf, buflen, "(NOT, %s)",
541 sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
542 return buf;
543 } /* uop_tostring */
545 static sdb_type_t name_type = {
546 /* size = */ sizeof(name_matcher_t),
547 /* init = */ name_matcher_init,
548 /* destroy = */ name_matcher_destroy,
549 };
551 static sdb_type_t attr_type = {
552 /* size = */ sizeof(attr_matcher_t),
553 /* init = */ attr_matcher_init,
554 /* destroy = */ attr_matcher_destroy,
555 };
557 static sdb_type_t cond_type = {
558 /* size = */ sizeof(cond_matcher_t),
559 /* init = */ cond_matcher_init,
560 /* destroy = */ cond_matcher_destroy,
561 };
563 static sdb_type_t op_type = {
564 /* size = */ sizeof(op_matcher_t),
565 /* init = */ op_matcher_init,
566 /* destroy = */ op_matcher_destroy,
567 };
569 static sdb_type_t uop_type = {
570 /* size = */ sizeof(uop_matcher_t),
571 /* init = */ uop_matcher_init,
572 /* destroy = */ uop_matcher_destroy,
573 };
575 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
577 /* this array needs to be indexable by the matcher types;
578 * -> update the enum in store-private.h when updating this */
579 static matcher_tostring_cb matchers_tostring[] = {
580 op_tostring,
581 op_tostring,
582 uop_tostring,
583 name_tostring,
584 attr_tostring,
585 cond_tostring,
586 cond_tostring,
587 cond_tostring,
588 cond_tostring,
589 cond_tostring,
590 };
592 /*
593 * public API
594 */
596 sdb_store_cond_t *
597 sdb_store_attr_cond(const char *name, const sdb_data_t *value)
598 {
599 return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type,
600 name, value));
601 } /* sdb_store_attr_cond */
603 sdb_store_matcher_t *
604 sdb_store_name_matcher(int type, const char *name, _Bool re)
605 {
606 sdb_store_matcher_t *m;
608 if (re)
609 m = M(sdb_object_create("name-matcher", name_type,
610 NULL, name));
611 else
612 m = M(sdb_object_create("name-matcher", name_type,
613 name, NULL));
615 if (! m)
616 return NULL;
618 NAME_M(m)->obj_type = type;
619 return m;
620 } /* sdb_store_name_matcher */
622 sdb_store_matcher_t *
623 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
624 {
625 if (! name)
626 return NULL;
628 if (re)
629 return M(sdb_object_create("attr-matcher", attr_type,
630 name, NULL, value));
631 return M(sdb_object_create("attr-matcher", attr_type,
632 name, value, NULL));
633 } /* sdb_store_attr_matcher */
635 sdb_store_matcher_t *
636 sdb_store_lt_matcher(sdb_store_cond_t *cond)
637 {
638 return M(sdb_object_create("lt-matcher", cond_type,
639 MATCHER_LT, cond));
640 } /* sdb_store_lt_matcher */
642 sdb_store_matcher_t *
643 sdb_store_le_matcher(sdb_store_cond_t *cond)
644 {
645 return M(sdb_object_create("le-matcher", cond_type,
646 MATCHER_LE, cond));
647 } /* sdb_store_le_matcher */
649 sdb_store_matcher_t *
650 sdb_store_eq_matcher(sdb_store_cond_t *cond)
651 {
652 return M(sdb_object_create("eq-matcher", cond_type,
653 MATCHER_EQ, cond));
654 } /* sdb_store_eq_matcher */
656 sdb_store_matcher_t *
657 sdb_store_ge_matcher(sdb_store_cond_t *cond)
658 {
659 return M(sdb_object_create("ge-matcher", cond_type,
660 MATCHER_GE, cond));
661 } /* sdb_store_ge_matcher */
663 sdb_store_matcher_t *
664 sdb_store_gt_matcher(sdb_store_cond_t *cond)
665 {
666 return M(sdb_object_create("gt-matcher", cond_type,
667 MATCHER_GT, cond));
668 } /* sdb_store_gt_matcher */
670 static sdb_store_matcher_t *
671 parse_attr_cmp(const char *attr, const char *op, const sdb_data_t *value)
672 {
673 sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
674 sdb_store_matcher_t *m;
675 sdb_store_cond_t *cond;
676 _Bool inv = 0;
678 /* TODO: this will reject any attributes called "name";
679 * use a different syntax for querying objects by name */
680 if (! strcasecmp(attr, "name"))
681 return NULL;
683 if (! strcasecmp(op, "<"))
684 matcher = sdb_store_lt_matcher;
685 else if (! strcasecmp(op, "<="))
686 matcher = sdb_store_le_matcher;
687 else if (! strcasecmp(op, "="))
688 matcher = sdb_store_eq_matcher;
689 else if (! strcasecmp(op, ">="))
690 matcher = sdb_store_ge_matcher;
691 else if (! strcasecmp(op, ">"))
692 matcher = sdb_store_gt_matcher;
693 else if (! strcasecmp(op, "!=")) {
694 matcher = sdb_store_eq_matcher;
695 inv = 1;
696 }
697 else
698 return NULL;
700 cond = sdb_store_attr_cond(attr, value);
701 if (! cond)
702 return NULL;
704 m = matcher(cond);
705 /* pass ownership to 'm' or destroy in case of an error */
706 sdb_object_deref(SDB_OBJ(cond));
707 if (! m)
708 return NULL;
710 if (inv) {
711 sdb_store_matcher_t *tmp;
712 tmp = sdb_store_inv_matcher(m);
713 /* pass ownership to the inverse matcher */
714 sdb_object_deref(SDB_OBJ(m));
715 m = tmp;
716 }
717 return m;
718 } /* parse_attr_cmp */
720 sdb_store_matcher_t *
721 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
722 const char *op, const sdb_data_t *value)
723 {
724 int type = -1;
725 _Bool inv = 0;
726 _Bool re = 0;
728 sdb_store_matcher_t *m = NULL;
730 if (! strcasecmp(obj_type, "host"))
731 type = SDB_HOST;
732 else if (! strcasecmp(obj_type, "service"))
733 type = SDB_SERVICE;
734 else if (! strcasecmp(obj_type, "attribute"))
735 type = SDB_ATTRIBUTE;
736 else
737 return NULL;
739 /* XXX: this code sucks! */
740 if (! strcasecmp(op, "=")) {
741 /* nothing to do */
742 }
743 else if (! strcasecmp(op, "!=")) {
744 inv = 1;
745 }
746 else if (! strcasecmp(op, "=~")) {
747 re = 1;
748 }
749 else if (! strcasecmp(op, "!~")) {
750 inv = 1;
751 re = 1;
752 }
753 else if (type == SDB_ATTRIBUTE)
754 return parse_attr_cmp(attr, op, value);
755 else
756 return NULL;
758 if (value->type != SDB_TYPE_STRING) {
759 if (type == SDB_ATTRIBUTE)
760 return parse_attr_cmp(attr, op, value);
761 return NULL;
762 }
764 if (! strcasecmp(attr, "name"))
765 m = sdb_store_name_matcher(type, value->data.string, re);
766 else if (type == SDB_ATTRIBUTE)
767 m = sdb_store_attr_matcher(attr, value->data.string, re);
769 if (! m)
770 return NULL;
772 if (inv) {
773 sdb_store_matcher_t *tmp;
774 tmp = sdb_store_inv_matcher(m);
775 /* pass ownership to the inverse matcher */
776 sdb_object_deref(SDB_OBJ(m));
777 m = tmp;
778 }
779 return m;
780 } /* sdb_store_matcher_parse_cmp */
782 sdb_store_matcher_t *
783 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
784 {
785 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
786 left, right));
787 } /* sdb_store_dis_matcher */
789 sdb_store_matcher_t *
790 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
791 {
792 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
793 left, right));
794 } /* sdb_store_con_matcher */
796 sdb_store_matcher_t *
797 sdb_store_inv_matcher(sdb_store_matcher_t *m)
798 {
799 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
800 } /* sdb_store_inv_matcher */
802 int
803 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj)
804 {
805 if (obj->type != SDB_HOST)
806 return 0;
808 /* "NULL" always matches */
809 if ((! m) || (! obj))
810 return 1;
812 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
813 return 0;
815 return matchers[m->type](m, HOST(obj));
816 } /* sdb_store_matcher_matches */
818 char *
819 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
820 {
821 if (! m)
822 return NULL;
824 if ((m->type < 0)
825 || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
826 return NULL;
827 return matchers_tostring[m->type](m, buf, buflen);
828 } /* sdb_store_matcher_tostring */
830 int
831 sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
832 void *user_data)
833 {
834 lookup_iter_data_t data = { m, cb, user_data };
836 if (! cb)
837 return -1;
838 return sdb_store_iterate(lookup_iter, &data);
839 } /* sdb_store_lookup */
841 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */