Code

store_lookup: Added a matcher matching by object name.
[sysdb.git] / src / core / store_lookup.c
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 /*
51  * private data types
52  */
54 typedef struct {
55         sdb_store_matcher_t *m;
56         sdb_store_lookup_cb  cb;
57         void *user_data;
58 } lookup_iter_data_t;
60 /*
61  * private helper functions
62  */
64 static int
65 lookup_iter(sdb_store_base_t *obj, void *user_data)
66 {
67         lookup_iter_data_t *d = user_data;
69         if (sdb_store_matcher_matches(d->m, obj))
70                 return d->cb(obj, d->user_data);
71         return 0;
72 } /* lookup_iter */
74 /*
75  * matcher implementations
76  */
78 static int
79 match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj);
80 static int
81 match_unary(sdb_store_matcher_t *m, sdb_store_base_t *obj);
82 static int
83 match_obj(sdb_store_matcher_t *m, sdb_store_base_t *obj);
85 /* specific matchers */
87 static int
88 match_obj_name(name_matcher_t *m, const char *name)
89 {
90         assert(m);
92         if ((! m->name) && (! m->name_re))
93                 return 1;
95         if (! name)
96                 name = "";
98         if (m->name && strcasecmp(m->name, name))
99                 return 0;
100         if (m->name_re && regexec(m->name_re, name,
101                                         /* matches */ 0, NULL, /* flags = */ 0))
102                 return 0;
103         return 1;
104 } /* match_obj_name */
106 static char *
107 obj_name_tostring(name_matcher_t *m, char *buf, size_t buflen)
109         snprintf(buf, buflen, "{ %s%s%s, %p }",
110                         m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
111                         m->name_re);
112         return buf;
113 } /* obj_name_tostring */
115 static char *
116 logical_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
118         char left[buflen + 1], right[buflen + 1];
120         if (! m) {
121                 /* this should not happen */
122                 snprintf(buf, buflen, "()");
123                 return buf;
124         }
126         assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
127         snprintf(buf, buflen, "(%s, %s, %s)",
128                         m->type == MATCHER_OR ? "OR" : "AND",
129                         sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
130                         sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
131         return buf;
132 } /* logical_tostring */
134 static char *
135 unary_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
137         char op[buflen + 1];
139         if (! m) {
140                 /* this should not happen */
141                 snprintf(buf, buflen, "()");
142                 return buf;
143         }
145         assert(m->type == MATCHER_NOT);
146         snprintf(buf, buflen, "(NOT, %s)",
147                         sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
148         return buf;
149 } /* unary_tostring */
151 static char *
152 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
154         char name[buflen + 1];
155         assert(m->type == MATCHER_NAME);
156         snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
157                         SDB_STORE_TYPE_TO_NAME(OBJ_M(m)->obj_type),
158                         obj_name_tostring(&OBJ_M(m)->name, name, sizeof(name)));
159         return buf;
160 } /* name_tostring */
162 static char *
163 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
165         char name[buflen + 1], value[buflen + 1];
167         if (! m) {
168                 snprintf(buf, buflen, "ATTR{}");
169                 return buf;
170         }
172         assert(m->type == MATCHER_ATTR);
173         snprintf(buf, buflen, "ATTR{ NAME%s, VALUE%s }",
174                         obj_name_tostring(&OBJ_M(m)->name, name, sizeof(name)),
175                         obj_name_tostring(&ATTR_M(m)->value, value, sizeof(value)));
176         return buf;
177 } /* attr_tostring */
179 static char *
180 service_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
182         char name[buflen + 1], attr[buflen + 1];
184         if (! m) {
185                 snprintf(buf, buflen, "SERVICE{}");
186                 return buf;
187         }
189         assert(m->type == MATCHER_SERVICE);
190         snprintf(buf, buflen, "SERVICE{ NAME%s, %s }",
191                         obj_name_tostring(&OBJ_M(m)->name, name, sizeof(name)),
192                         attr_tostring(SDB_STORE_MATCHER(SERVICE_M(m)->attr),
193                                 attr, sizeof(attr)));
194         return buf;
195 } /* service_tostring */
197 static char *
198 host_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
200         char name[buflen + 1], service[buflen + 1], attr[buflen + 1];
202         if (! m) {
203                 snprintf(buf, buflen, "HOST{}");
204                 return buf;
205         }
207         assert(m->type == MATCHER_HOST);
208         snprintf(buf, buflen, "HOST{ NAME%s, %s, %s }",
209                         obj_name_tostring(&OBJ_M(m)->name, name, sizeof(name)),
210                         service_tostring(SDB_STORE_MATCHER(HOST_M(m)->service),
211                                 service, sizeof(service)),
212                         attr_tostring(SDB_STORE_MATCHER(HOST_M(m)->attr),
213                                 attr, sizeof(attr)));
214         return buf;
215 } /* host_tostring */
217 static int
218 match_name(sdb_store_matcher_t *m, sdb_store_base_t *obj)
220         sdb_llist_iter_t *iter = NULL;
221         int status = 0;
223         assert(m && obj);
225         if (obj->type != SDB_HOST)
226                 return 0;
228         switch (OBJ_M(m)->obj_type) {
229                 case SDB_HOST:
230                         return match_obj_name(&OBJ_M(m)->name, obj->super.name);
231                         break;
232                 case SDB_SERVICE:
233                         iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children);
234                         break;
235                 case SDB_ATTRIBUTE:
236                         iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
237                         break;
238         }
240         while (sdb_llist_iter_has_next(iter)) {
241                 sdb_store_base_t *child = STORE_BASE(sdb_llist_iter_get_next(iter));
242                 if (match_obj_name(&OBJ_M(m)->name, child->super.name)) {
243                         status = 1;
244                         break;
245                 }
246         }
247         sdb_llist_iter_destroy(iter);
248         return status;
249 } /* match_name */
251 /* match attribute specific values;
252  * always call this function through match_obj() */
253 static int
254 match_attr(attr_matcher_t *m, sdb_store_base_t *obj)
256         assert(m && obj);
258         if (obj->type != SDB_ATTRIBUTE)
259                 return 0;
261         {
262                 sdb_attribute_t *attr = SDB_ATTR(obj);
263                 char buf[sdb_data_strlen(&attr->value) + 1];
265                 if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
266                         return 0;
267                 return match_obj_name(&m->value, buf);
268         }
269 } /* match_attr */
271 /* match service specific values;
272  * always call this function through match_obj() */
273 static int
274 match_service(service_matcher_t *m, sdb_store_base_t *obj)
276         sdb_llist_iter_t *iter;
278         assert(m && obj);
280         if (obj->type != SDB_SERVICE)
281                 return 0;
283         if (! m->attr)
284                 return 1;
286         iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
287         while (sdb_llist_iter_has_next(iter)) {
288                 sdb_store_base_t *attr = STORE_BASE(sdb_llist_iter_get_next(iter));
290                 /* if any of the attributes matches we found a matching service */
291                 if (match_obj(M(m->attr), attr)) {
292                         sdb_llist_iter_destroy(iter);
293                         return 1;
294                 }
295         }
296         sdb_llist_iter_destroy(iter);
297         return 0;
298 } /* match_service */
300 /* match host specific values;
301  * always call this function through match_obj() */
302 static int
303 match_host(host_matcher_t *m, sdb_store_base_t *obj)
305         sdb_llist_iter_t *iter;
306         int status;
308         assert(m && obj);
310         if (obj->type != SDB_HOST)
311                 return 0;
313         if (m->service) {
314                 iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children);
315                 status = 0;
316         }
317         else {
318                 iter = NULL;
319                 status = 1;
320         }
321         while (sdb_llist_iter_has_next(iter)) {
322                 sdb_store_base_t *service = STORE_BASE(sdb_llist_iter_get_next(iter));
324                 /* found a matching service */
325                 if (match_obj(M(m->service), service)) {
326                         status = 1;
327                         break;
328                 }
329         }
330         sdb_llist_iter_destroy(iter);
332         if (! status)
333                 return status;
334         else if (! m->attr)
335                 return 1;
337         iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
338         while (sdb_llist_iter_has_next(iter)) {
339                 sdb_store_base_t *attr = STORE_BASE(sdb_llist_iter_get_next(iter));
341                 /* if any attribute matches, we found a matching host */
342                 if (match_obj(M(m->attr), attr)) {
343                         sdb_llist_iter_destroy(iter);
344                         return 1;
345                 }
346         }
347         sdb_llist_iter_destroy(iter);
348         return 0;
349 } /* match_host */
351 /* generic matchers */
353 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_base_t *);
355 /* this array needs to be indexable by the matcher types;
356  * -> update the enum in store-private.h when updating this */
357 static matcher_cb matchers[] = {
358         match_logical,
359         match_logical,
360         match_unary,
361         match_name,
362         match_obj,
363         match_obj,
364         match_obj,
365 };
367 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
369 static matcher_tostring_cb matchers_tostring[] = {
370         logical_tostring,
371         logical_tostring,
372         unary_tostring,
373         name_tostring,
374         attr_tostring,
375         service_tostring,
376         host_tostring,
377 };
379 static int
380 match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj)
382         int status;
384         assert(m && obj);
385         assert(OP_M(m)->left && OP_M(m)->right);
387         status = sdb_store_matcher_matches(OP_M(m)->left, obj);
388         /* lazy evaluation */
389         if ((! status) && (m->type == MATCHER_AND))
390                 return status;
391         else if (status && (m->type == MATCHER_OR))
392                 return status;
394         return sdb_store_matcher_matches(OP_M(m)->right, obj);
395 } /* match_logical */
397 static int
398 match_unary(sdb_store_matcher_t *m, sdb_store_base_t *obj)
400         assert(m && obj);
401         assert(UOP_M(m)->op);
403         return !sdb_store_matcher_matches(UOP_M(m)->op, obj);
404 } /* match_unary */
406 static int
407 match_obj(sdb_store_matcher_t *m, sdb_store_base_t *obj)
409         int status;
411         assert(m && obj);
413         status = match_obj_name(&OBJ_M(m)->name, obj->super.name);
414         if (! status)
415                 return status;
417         switch (m->type) {
418                 case MATCHER_ATTR:
419                         return match_attr(ATTR_M(m), obj);
420                         break;
421                 case MATCHER_SERVICE:
422                         return match_service(SERVICE_M(m), obj);
423                         break;
424                 case MATCHER_HOST:
425                         return match_host(HOST_M(m), obj);
426                         break;
427                 default:
428                         assert(m->type != m->type);
429                         break;
430         }
431         return 0;
432 } /* match_obj */
434 /*
435  * private matcher types
436  */
438 /* initializes a name matcher consuming two elements from ap */
439 static int
440 name_matcher_init(name_matcher_t *m, va_list ap)
442         const char *name = va_arg(ap, const char *);
443         const char *name_re = va_arg(ap, const char *);
445         if (name) {
446                 m->name = strdup(name);
447                 if (! m->name)
448                         return -1;
449         }
450         if (name_re) {
451                 m->name_re = malloc(sizeof(*m->name_re));
452                 if (! m->name_re)
453                         return -1;
454                 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
455                         return -1;
456         }
457         return 0;
458 } /* name_matcher_init */
460 static void
461 name_matcher_destroy(name_matcher_t *m)
463         if (m->name)
464                 free(m->name);
465         if (m->name_re) {
466                 regfree(m->name_re);
467                 free(m->name_re);
468         }
469 } /* name_matcher_destroy */
471 /* initializes an object matcher consuming two elements from ap */
472 static int
473 obj_matcher_init(sdb_object_t *obj, va_list ap)
475         obj_matcher_t *m = OBJ_M(obj);
476         M(obj)->type = MATCHER_NAME;
477         return name_matcher_init(&m->name, ap);
478 } /* obj_matcher_init */
480 static void
481 obj_matcher_destroy(sdb_object_t *obj)
483         obj_matcher_t *m = OBJ_M(obj);
484         name_matcher_destroy(&m->name);
485 } /* obj_matcher_destroy */
487 static int
488 attr_matcher_init(sdb_object_t *obj, va_list ap)
490         attr_matcher_t *attr = ATTR_M(obj);
491         int status;
493         status = obj_matcher_init(obj, ap);
494         if (! status)
495                 status = name_matcher_init(&attr->value, ap);
497         M(obj)->type = MATCHER_ATTR;
498         return status;
499 } /* attr_matcher_init */
501 static void
502 attr_matcher_destroy(sdb_object_t *obj)
504         attr_matcher_t *attr = ATTR_M(obj);
506         obj_matcher_destroy(obj);
507         name_matcher_destroy(&attr->value);
508 } /* attr_matcher_destroy */
510 static int
511 service_matcher_init(sdb_object_t *obj, va_list ap)
513         attr_matcher_t *attr;
514         int status;
516         status = obj_matcher_init(obj, ap);
517         if (status)
518                 return status;
520         attr = va_arg(ap, attr_matcher_t *);
522         sdb_object_ref(SDB_OBJ(attr));
523         SERVICE_M(obj)->attr = attr;
525         M(obj)->type = MATCHER_SERVICE;
526         return 0;
527 } /* service_matcher_init */
529 static void
530 service_matcher_destroy(sdb_object_t *obj)
532         obj_matcher_destroy(obj);
533         sdb_object_deref(SDB_OBJ(SERVICE_M(obj)->attr));
534 } /* service_matcher_destroy */
536 static int
537 host_matcher_init(sdb_object_t *obj, va_list ap)
539         service_matcher_t *service;
540         attr_matcher_t *attr;
541         int status;
543         status = obj_matcher_init(obj, ap);
544         if (status)
545                 return status;
547         service = va_arg(ap, service_matcher_t *);
548         attr = va_arg(ap, attr_matcher_t *);
550         sdb_object_ref(SDB_OBJ(service));
551         HOST_M(obj)->service = service;
552         sdb_object_ref(SDB_OBJ(attr));
553         HOST_M(obj)->attr = attr;
555         M(obj)->type = MATCHER_HOST;
556         return 0;
557 } /* host_matcher_init */
559 static void
560 host_matcher_destroy(sdb_object_t *obj)
562         obj_matcher_destroy(obj);
563         sdb_object_deref(SDB_OBJ(HOST_M(obj)->service));
564         sdb_object_deref(SDB_OBJ(HOST_M(obj)->attr));
565 } /* host_matcher_destroy */
567 static int
568 op_matcher_init(sdb_object_t *obj, va_list ap)
570         M(obj)->type = va_arg(ap, int);
571         if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
572                 return -1;
574         OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
575         sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
576         OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
577         sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
579         if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
580                 return -1;
581         return 0;
582 } /* op_matcher_init */
584 static void
585 op_matcher_destroy(sdb_object_t *obj)
587         if (OP_M(obj)->left)
588                 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
589         if (OP_M(obj)->right)
590                 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
591 } /* op_matcher_destroy */
593 static int
594 uop_matcher_init(sdb_object_t *obj, va_list ap)
596         M(obj)->type = va_arg(ap, int);
597         if (M(obj)->type != MATCHER_NOT)
598                 return -1;
600         UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
601         sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
603         if (! UOP_M(obj)->op)
604                 return -1;
605         return 0;
606 } /* uop_matcher_init */
608 static void
609 uop_matcher_destroy(sdb_object_t *obj)
611         if (UOP_M(obj)->op)
612                 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
613 } /* uop_matcher_destroy */
615 static sdb_type_t name_type = {
616         /* size = */ sizeof(obj_matcher_t),
617         /* init = */ obj_matcher_init,
618         /* destroy = */ obj_matcher_destroy,
619 };
621 static sdb_type_t attr_type = {
622         /* size = */ sizeof(attr_matcher_t),
623         /* init = */ attr_matcher_init,
624         /* destroy = */ attr_matcher_destroy,
625 };
627 static sdb_type_t service_type = {
628         /* size = */ sizeof(service_matcher_t),
629         /* init = */ service_matcher_init,
630         /* destroy = */ service_matcher_destroy,
631 };
633 static sdb_type_t host_type = {
634         /* size = */ sizeof(host_matcher_t),
635         /* init = */ host_matcher_init,
636         /* destroy = */ host_matcher_destroy,
637 };
639 static sdb_type_t op_type = {
640         /* size = */ sizeof(op_matcher_t),
641         /* init = */ op_matcher_init,
642         /* destroy = */ op_matcher_destroy,
643 };
645 static sdb_type_t uop_type = {
646         /* size = */ sizeof(uop_matcher_t),
647         /* init = */ uop_matcher_init,
648         /* destroy = */ uop_matcher_destroy,
649 };
651 /*
652  * public API
653  */
655 sdb_store_matcher_t *
656 sdb_store_name_matcher(int type, const char *name, _Bool re)
658         sdb_store_matcher_t *m;
660         if (re)
661                 m = M(sdb_object_create("name-matcher", name_type,
662                                         NULL, name));
663         else
664                 m = M(sdb_object_create("name-matcher", name_type,
665                                         name, NULL));
667         if (! m)
668                 return NULL;
670         OBJ_M(m)->obj_type = type;
671         return m;
672 } /* sdb_store_name_matcher */
674 sdb_store_matcher_t *
675 sdb_store_attr_matcher(const char *attr_name, const char *attr_name_re,
676                 const char *attr_value, const char *attr_value_re)
678         return M(sdb_object_create("attr-matcher", attr_type,
679                                 attr_name, attr_name_re, attr_value, attr_value_re));
680 } /* sdb_store_attr_matcher */
682 sdb_store_matcher_t *
683 sdb_store_service_matcher(const char *service_name, const char *service_name_re,
684                 sdb_store_matcher_t *attr_matcher)
686         return M(sdb_object_create("service-matcher", service_type,
687                                 service_name, service_name_re, attr_matcher));
688 } /* sdb_store_service_matcher */
690 sdb_store_matcher_t *
691 sdb_store_host_matcher(const char *host_name, const char *host_name_re,
692                 sdb_store_matcher_t *service_matcher,
693                 sdb_store_matcher_t *attr_matcher)
695         return M(sdb_object_create("host-matcher", host_type,
696                                 host_name, host_name_re, service_matcher, attr_matcher));
697 } /* sdb_store_host_matcher */
699 sdb_store_matcher_t *
700 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
701                 const char *op, const char *value)
703         int typ = -1;
704         int inv = 0;
706         sdb_store_matcher_t *m = NULL;
708         const char *matcher = NULL;
709         const char *matcher_re = NULL;
711         if (! strcasecmp(obj_type, "host"))
712                 typ = SDB_HOST;
713         else if (! strcasecmp(obj_type, "service"))
714                 typ = SDB_SERVICE;
715         else if (! strcasecmp(obj_type, "attribute"))
716                 typ = SDB_ATTRIBUTE;
718         /* TODO: support other operators */
719         if (! strcasecmp(op, "=")) {
720                 matcher = value;
721         }
722         else if (! strcasecmp(op, "!=")) {
723                 matcher = value;
724                 inv = 1;
725         }
726         else if (! strcasecmp(op, "=~")) {
727                 matcher_re = value;
728         }
729         else if (! strcasecmp(op, "!~")) {
730                 matcher_re = value;
731                 inv = 1;
732         }
733         else
734                 return NULL;
736         if (! strcasecmp(attr, "name")) {
737                 /* accept */
738         }
739         else if (typ == SDB_ATTRIBUTE)
740                 m = sdb_store_host_matcher(/* name = */ NULL, NULL,
741                                 /* service = */ NULL,
742                                 sdb_store_attr_matcher(attr, NULL, matcher, matcher_re));
743         else
744                 return NULL;
746         if (m) {
747                 /* accept the attribute value matcher */
748         }
749         else if (typ == SDB_HOST)
750                 m = sdb_store_host_matcher(matcher, matcher_re, NULL, NULL);
751         else if (typ == SDB_SERVICE)
752                 m = sdb_store_host_matcher(/* name = */ NULL, NULL,
753                                 sdb_store_service_matcher(matcher, matcher_re, NULL),
754                                 /* attr = */ NULL);
755         else if (typ == SDB_ATTRIBUTE)
756                 m = sdb_store_host_matcher(/* name = */ NULL, NULL,
757                                 /* service = */ NULL,
758                                 sdb_store_attr_matcher(matcher, matcher_re, NULL, NULL));
760         if (! m)
761                 return NULL;
763         /* pass ownership to the host matcher */
764         sdb_object_deref(SDB_OBJ(HOST_M(m)->service));
765         sdb_object_deref(SDB_OBJ(HOST_M(m)->attr));
767         if (inv) {
768                 sdb_store_matcher_t *tmp;
769                 tmp = sdb_store_inv_matcher(m);
770                 /* pass ownership to the inverse matcher */
771                 sdb_object_deref(SDB_OBJ(m));
772                 m = tmp;
773         }
774         return m;
775 } /* sdb_store_matcher_parse_cmp */
777 sdb_store_matcher_t *
778 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
780         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
781                                 left, right));
782 } /* sdb_store_dis_matcher */
784 sdb_store_matcher_t *
785 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
787         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
788                                 left, right));
789 } /* sdb_store_con_matcher */
791 sdb_store_matcher_t *
792 sdb_store_inv_matcher(sdb_store_matcher_t *m)
794         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
795 } /* sdb_store_inv_matcher */
797 int
798 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj)
800         /* "NULL" always matches */
801         if ((! m) || (! obj))
802                 return 1;
804         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
805                 return 0;
807         return matchers[m->type](m, obj);
808 } /* sdb_store_matcher_matches */
810 char *
811 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
813         if (! m)
814                 return NULL;
816         if ((m->type < 0)
817                         || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
818                 return NULL;
819         return matchers_tostring[m->type](m, buf, buflen);
820 } /* sdb_store_matcher_tostring */
822 int
823 sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
824                 void *user_data)
826         lookup_iter_data_t data = { m, cb, user_data };
828         if (! cb)
829                 return -1;
830         return sdb_store_iterate(lookup_iter, &data);
831 } /* sdb_store_lookup */
833 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */