Code

a3efb4669c182ee66938ac3554ba79c10d577d84
[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 #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_avltree_iter_t *iter = NULL;
80         sdb_attribute_t *attr = NULL;
82         iter = sdb_avltree_get_iter(host->attributes);
83         while (sdb_avltree_iter_has_next(iter)) {
84                 sdb_attribute_t *a = ATTR(sdb_avltree_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_avltree_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)
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)
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)
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)
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)
165         sdb_avltree_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_avltree_get_iter(host->services);
176                         break;
177                 case SDB_ATTRIBUTE:
178                         iter = sdb_avltree_get_iter(host->attributes);
179                         break;
180         }
182         while (sdb_avltree_iter_has_next(iter)) {
183                 sdb_object_t *child = sdb_avltree_iter_get_next(iter);
184                 if (match_string(&NAME_M(m)->name, child->name)) {
185                         status = 1;
186                         break;
187                 }
188         }
189         sdb_avltree_iter_destroy(iter);
190         return status;
191 } /* match_name */
193 static int
194 match_attr(sdb_store_matcher_t *m, sdb_host_t *host)
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)
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)
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)
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)
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)
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 static int
258 match_isnull(sdb_store_matcher_t *m, sdb_host_t *host)
260         assert(m->type == MATCHER_ISNULL);
261         return attr_get(host, ISNULL_M(m)->attr_name) == NULL;
262 } /* match_isnull */
264 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_host_t *);
266 /* this array needs to be indexable by the matcher types;
267  * -> update the enum in store-private.h when updating this */
268 static matcher_cb
269 matchers[] = {
270         match_logical,
271         match_logical,
272         match_unary,
273         match_name,
274         match_attr,
275         match_lt,
276         match_le,
277         match_eq,
278         match_ge,
279         match_gt,
280         match_isnull,
281 };
283 /*
284  * private conditional types
285  */
287 static int
288 attr_cond_init(sdb_object_t *obj, va_list ap)
290         const char *name = va_arg(ap, const char *);
291         const sdb_data_t *value = va_arg(ap, const sdb_data_t *);
293         if (! name)
294                 return -1;
296         SDB_STORE_COND(obj)->cmp = attr_cmp;
298         ATTR_C(obj)->name = strdup(name);
299         if (! ATTR_C(obj)->name)
300                 return -1;
301         if (sdb_data_copy(&ATTR_C(obj)->value, value))
302                 return -1;
303         return 0;
304 } /* attr_cond_init */
306 static void
307 attr_cond_destroy(sdb_object_t *obj)
309         if (ATTR_C(obj)->name)
310                 free(ATTR_C(obj)->name);
311         sdb_data_free_datum(&ATTR_C(obj)->value);
312 } /* attr_cond_destroy */
314 static sdb_type_t attr_cond_type = {
315         /* size = */ sizeof(attr_cond_t),
316         /* init = */ attr_cond_init,
317         /* destroy = */ attr_cond_destroy,
318 };
320 /*
321  * private matcher types
322  */
324 /* initializes a string matcher consuming two elements from ap */
325 static int
326 string_matcher_init(string_matcher_t *m, va_list ap)
328         const char *name = va_arg(ap, const char *);
329         const char *name_re = va_arg(ap, const char *);
331         if (name) {
332                 m->name = strdup(name);
333                 if (! m->name)
334                         return -1;
335         }
336         if (name_re) {
337                 m->name_re = malloc(sizeof(*m->name_re));
338                 if (! m->name_re)
339                         return -1;
340                 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
341                         return -1;
342         }
343         return 0;
344 } /* string_matcher_init */
346 static void
347 string_matcher_destroy(string_matcher_t *m)
349         if (m->name)
350                 free(m->name);
351         if (m->name_re) {
352                 regfree(m->name_re);
353                 free(m->name_re);
354         }
355 } /* string_matcher_destroy */
357 static char *
358 string_tostring(string_matcher_t *m, char *buf, size_t buflen)
360         snprintf(buf, buflen, "{ %s%s%s, %p }",
361                         m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
362                         m->name_re);
363         return buf;
364 } /* string_tostring */
366 /* initializes a name matcher */
367 static int
368 name_matcher_init(sdb_object_t *obj, va_list ap)
370         name_matcher_t *m = NAME_M(obj);
371         M(obj)->type = MATCHER_NAME;
372         return string_matcher_init(&m->name, ap);
373 } /* name_matcher_init */
375 static void
376 name_matcher_destroy(sdb_object_t *obj)
378         name_matcher_t *m = NAME_M(obj);
379         string_matcher_destroy(&m->name);
380 } /* name_matcher_destroy */
382 static char *
383 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
385         char name[buflen + 1];
386         assert(m->type == MATCHER_NAME);
387         snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
388                         SDB_STORE_TYPE_TO_NAME(NAME_M(m)->obj_type),
389                         string_tostring(&NAME_M(m)->name, name, sizeof(name)));
390         return buf;
391 } /* name_tostring */
393 static int
394 attr_matcher_init(sdb_object_t *obj, va_list ap)
396         attr_matcher_t *attr = ATTR_M(obj);
397         const char *name = va_arg(ap, const char *);
399         M(obj)->type = MATCHER_ATTR;
400         if (name) {
401                 attr->name = strdup(name);
402                 if (! attr->name)
403                         return -1;
404         }
405         return string_matcher_init(&attr->value, ap);
406 } /* attr_matcher_init */
408 static void
409 attr_matcher_destroy(sdb_object_t *obj)
411         attr_matcher_t *attr = ATTR_M(obj);
412         if (attr->name)
413                 free(attr->name);
414         attr->name = NULL;
415         string_matcher_destroy(&attr->value);
416 } /* attr_matcher_destroy */
418 static char *
419 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
421         char value[buflen + 1];
423         if (! m) {
424                 snprintf(buf, buflen, "ATTR{}");
425                 return buf;
426         }
428         assert(m->type == MATCHER_ATTR);
429         snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
430                         string_tostring(&ATTR_M(m)->value, value, sizeof(value)));
431         return buf;
432 } /* attr_tostring */
434 static int
435 cond_matcher_init(sdb_object_t *obj, va_list ap)
437         int type = va_arg(ap, int);
438         sdb_store_cond_t *cond = va_arg(ap, sdb_store_cond_t *);
440         if (! cond)
441                 return -1;
443         sdb_object_ref(SDB_OBJ(cond));
445         M(obj)->type = type;
446         COND_M(obj)->cond = cond;
447         return 0;
448 } /* cond_matcher_init */
450 static void
451 cond_matcher_destroy(sdb_object_t *obj)
453         sdb_object_deref(SDB_OBJ(COND_M(obj)->cond));
454 } /* cond_matcher_destroy */
456 static char *
457 cond_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
459         if (COND_M(m)->cond->cmp == attr_cmp) {
460                 char value[buflen];
461                 if (sdb_data_format(&ATTR_C(COND_M(m)->cond)->value,
462                                         value, sizeof(value), SDB_SINGLE_QUOTED) < 0)
463                         snprintf(value, sizeof(value), "ERR");
464                 snprintf(buf, buflen, "ATTR[%s]{ %s %s }",
465                                 ATTR_C(COND_M(m)->cond)->name, MATCHER_SYM(m->type), value);
466         }
467         return buf;
468 } /* cond_tostring */
470 static int
471 op_matcher_init(sdb_object_t *obj, va_list ap)
473         M(obj)->type = va_arg(ap, int);
474         if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
475                 return -1;
477         OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
478         sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
479         OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
480         sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
482         if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
483                 return -1;
484         return 0;
485 } /* op_matcher_init */
487 static void
488 op_matcher_destroy(sdb_object_t *obj)
490         if (OP_M(obj)->left)
491                 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
492         if (OP_M(obj)->right)
493                 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
494 } /* op_matcher_destroy */
496 static char *
497 op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
499         char left[buflen + 1], right[buflen + 1];
501         if (! m) {
502                 /* this should not happen */
503                 snprintf(buf, buflen, "()");
504                 return buf;
505         }
507         assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
508         snprintf(buf, buflen, "(%s, %s, %s)",
509                         m->type == MATCHER_OR ? "OR" : "AND",
510                         sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
511                         sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
512         return buf;
513 } /* op_tostring */
515 static int
516 uop_matcher_init(sdb_object_t *obj, va_list ap)
518         M(obj)->type = va_arg(ap, int);
519         if (M(obj)->type != MATCHER_NOT)
520                 return -1;
522         UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
523         sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
525         if (! UOP_M(obj)->op)
526                 return -1;
527         return 0;
528 } /* uop_matcher_init */
530 static void
531 uop_matcher_destroy(sdb_object_t *obj)
533         if (UOP_M(obj)->op)
534                 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
535 } /* uop_matcher_destroy */
537 static char *
538 uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
540         char op[buflen + 1];
542         if (! m) {
543                 /* this should not happen */
544                 snprintf(buf, buflen, "()");
545                 return buf;
546         }
548         assert(m->type == MATCHER_NOT);
549         snprintf(buf, buflen, "(NOT, %s)",
550                         sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
551         return buf;
552 } /* uop_tostring */
554 static int
555 isnull_matcher_init(sdb_object_t *obj, va_list ap)
557         const char *name;
559         M(obj)->type = va_arg(ap, int);
560         if (M(obj)->type != MATCHER_ISNULL)
561                 return -1;
563         name = va_arg(ap, const char *);
564         if (! name)
565                 return -1;
566         ISNULL_M(obj)->attr_name = strdup(name);
567         if (! ISNULL_M(obj)->attr_name)
568                 return -1;
569         return 0;
570 } /* isnull_matcher_init */
572 static void
573 isnull_matcher_destroy(sdb_object_t *obj)
575         if (ISNULL_M(obj)->attr_name)
576                 free(ISNULL_M(obj)->attr_name);
577         ISNULL_M(obj)->attr_name = NULL;
578 } /* isnull_matcher_destroy */
580 static char *
581 isnull_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
583         snprintf(buf, buflen, "(IS NULL, ATTR[%s])", ISNULL_M(m)->attr_name);
584         return buf;
585 } /* isnull_tostring */
587 static sdb_type_t name_type = {
588         /* size = */ sizeof(name_matcher_t),
589         /* init = */ name_matcher_init,
590         /* destroy = */ name_matcher_destroy,
591 };
593 static sdb_type_t attr_type = {
594         /* size = */ sizeof(attr_matcher_t),
595         /* init = */ attr_matcher_init,
596         /* destroy = */ attr_matcher_destroy,
597 };
599 static sdb_type_t cond_type = {
600         /* size = */ sizeof(cond_matcher_t),
601         /* init = */ cond_matcher_init,
602         /* destroy = */ cond_matcher_destroy,
603 };
605 static sdb_type_t op_type = {
606         /* size = */ sizeof(op_matcher_t),
607         /* init = */ op_matcher_init,
608         /* destroy = */ op_matcher_destroy,
609 };
611 static sdb_type_t uop_type = {
612         /* size = */ sizeof(uop_matcher_t),
613         /* init = */ uop_matcher_init,
614         /* destroy = */ uop_matcher_destroy,
615 };
617 static sdb_type_t isnull_type = {
618         /* size = */ sizeof(isnull_matcher_t),
619         /* init = */ isnull_matcher_init,
620         /* destroy = */ isnull_matcher_destroy,
621 };
623 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
625 /* this array needs to be indexable by the matcher types;
626  * -> update the enum in store-private.h when updating this */
627 static matcher_tostring_cb
628 matchers_tostring[] = {
629         op_tostring,
630         op_tostring,
631         uop_tostring,
632         name_tostring,
633         attr_tostring,
634         cond_tostring,
635         cond_tostring,
636         cond_tostring,
637         cond_tostring,
638         cond_tostring,
639         isnull_tostring,
640 };
642 /*
643  * public API
644  */
646 sdb_store_cond_t *
647 sdb_store_attr_cond(const char *name, const sdb_data_t *value)
649         return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type,
650                                 name, value));
651 } /* sdb_store_attr_cond */
653 sdb_store_matcher_t *
654 sdb_store_name_matcher(int type, const char *name, _Bool re)
656         sdb_store_matcher_t *m;
658         if (re)
659                 m = M(sdb_object_create("name-matcher", name_type,
660                                         NULL, name));
661         else
662                 m = M(sdb_object_create("name-matcher", name_type,
663                                         name, NULL));
665         if (! m)
666                 return NULL;
668         NAME_M(m)->obj_type = type;
669         return m;
670 } /* sdb_store_name_matcher */
672 sdb_store_matcher_t *
673 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
675         if (! name)
676                 return NULL;
678         if (re)
679                 return M(sdb_object_create("attr-matcher", attr_type,
680                                         name, NULL, value));
681         return M(sdb_object_create("attr-matcher", attr_type,
682                                 name, value, NULL));
683 } /* sdb_store_attr_matcher */
685 sdb_store_matcher_t *
686 sdb_store_lt_matcher(sdb_store_cond_t *cond)
688         return M(sdb_object_create("lt-matcher", cond_type,
689                                 MATCHER_LT, cond));
690 } /* sdb_store_lt_matcher */
692 sdb_store_matcher_t *
693 sdb_store_le_matcher(sdb_store_cond_t *cond)
695         return M(sdb_object_create("le-matcher", cond_type,
696                                 MATCHER_LE, cond));
697 } /* sdb_store_le_matcher */
699 sdb_store_matcher_t *
700 sdb_store_eq_matcher(sdb_store_cond_t *cond)
702         return M(sdb_object_create("eq-matcher", cond_type,
703                                 MATCHER_EQ, cond));
704 } /* sdb_store_eq_matcher */
706 sdb_store_matcher_t *
707 sdb_store_ge_matcher(sdb_store_cond_t *cond)
709         return M(sdb_object_create("ge-matcher", cond_type,
710                                 MATCHER_GE, cond));
711 } /* sdb_store_ge_matcher */
713 sdb_store_matcher_t *
714 sdb_store_gt_matcher(sdb_store_cond_t *cond)
716         return M(sdb_object_create("gt-matcher", cond_type,
717                                 MATCHER_GT, cond));
718 } /* sdb_store_gt_matcher */
720 sdb_store_matcher_t *
721 sdb_store_isnull_matcher(const char *attr_name)
723         return M(sdb_object_create("isnull-matcher", isnull_type,
724                                 MATCHER_ISNULL, attr_name));
725 } /* sdb_store_isnull_matcher */
727 static sdb_store_matcher_t *
728 parse_attr_cmp(const char *attr, const char *op, const sdb_data_t *value)
730         sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
731         sdb_store_matcher_t *m;
732         sdb_store_cond_t *cond;
733         _Bool inv = 0;
735         if (! attr)
736                 return NULL;
738         if (! strcasecmp(op, "IS")) {
739                 if (! value)
740                         return sdb_store_isnull_matcher(attr);
741                 else
742                         return NULL;
743         }
744         else if (! value)
745                 return NULL;
746         else if (! strcasecmp(op, "<"))
747                 matcher = sdb_store_lt_matcher;
748         else if (! strcasecmp(op, "<="))
749                 matcher = sdb_store_le_matcher;
750         else if (! strcasecmp(op, "="))
751                 matcher = sdb_store_eq_matcher;
752         else if (! strcasecmp(op, ">="))
753                 matcher = sdb_store_ge_matcher;
754         else if (! strcasecmp(op, ">"))
755                 matcher = sdb_store_gt_matcher;
756         else if (! strcasecmp(op, "!=")) {
757                 matcher = sdb_store_eq_matcher;
758                 inv = 1;
759         }
760         else
761                 return NULL;
763         cond = sdb_store_attr_cond(attr, value);
764         if (! cond)
765                 return NULL;
767         m = matcher(cond);
768         /* pass ownership to 'm' or destroy in case of an error */
769         sdb_object_deref(SDB_OBJ(cond));
770         if (! m)
771                 return NULL;
773         if (inv) {
774                 sdb_store_matcher_t *tmp;
775                 tmp = sdb_store_inv_matcher(m);
776                 /* pass ownership to the inverse matcher */
777                 sdb_object_deref(SDB_OBJ(m));
778                 m = tmp;
779         }
780         return m;
781 } /* parse_attr_cmp */
783 sdb_store_matcher_t *
784 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
785                 const char *op, const sdb_data_t *value)
787         int type = -1;
788         _Bool inv = 0;
789         _Bool re = 0;
791         sdb_store_matcher_t *m = NULL;
793         if (! strcasecmp(obj_type, "host"))
794                 type = SDB_HOST;
795         else if (! strcasecmp(obj_type, "service"))
796                 type = SDB_SERVICE;
797         else if (! strcasecmp(obj_type, "attribute"))
798                 type = SDB_ATTRIBUTE;
799         else
800                 return NULL;
802         /* XXX: this code sucks! */
803         if (! strcasecmp(op, "=")) {
804                 /* nothing to do */
805         }
806         else if (! strcasecmp(op, "!=")) {
807                 inv = 1;
808         }
809         else if (! strcasecmp(op, "=~")) {
810                 re = 1;
811         }
812         else if (! strcasecmp(op, "!~")) {
813                 inv = 1;
814                 re = 1;
815         }
816         else if (type == SDB_ATTRIBUTE)
817                 return parse_attr_cmp(attr, op, value);
818         else
819                 return NULL;
821         if (value->type != SDB_TYPE_STRING) {
822                 if (type == SDB_ATTRIBUTE)
823                         return parse_attr_cmp(attr, op, value);
824                 return NULL;
825         }
827         if (! attr)
828                 m = sdb_store_name_matcher(type, value->data.string, re);
829         else if (type == SDB_ATTRIBUTE)
830                 m = sdb_store_attr_matcher(attr, value->data.string, re);
832         if (! m)
833                 return NULL;
835         if (inv) {
836                 sdb_store_matcher_t *tmp;
837                 tmp = sdb_store_inv_matcher(m);
838                 /* pass ownership to the inverse matcher */
839                 sdb_object_deref(SDB_OBJ(m));
840                 m = tmp;
841         }
842         return m;
843 } /* sdb_store_matcher_parse_cmp */
845 sdb_store_matcher_t *
846 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
848         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
849                                 left, right));
850 } /* sdb_store_dis_matcher */
852 sdb_store_matcher_t *
853 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
855         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
856                                 left, right));
857 } /* sdb_store_con_matcher */
859 sdb_store_matcher_t *
860 sdb_store_inv_matcher(sdb_store_matcher_t *m)
862         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
863 } /* sdb_store_inv_matcher */
865 int
866 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj)
868         if (obj->type != SDB_HOST)
869                 return 0;
871         /* "NULL" always matches */
872         if ((! m) || (! obj))
873                 return 1;
875         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
876                 return 0;
878         return matchers[m->type](m, HOST(obj));
879 } /* sdb_store_matcher_matches */
881 char *
882 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
884         if (! m)
885                 return NULL;
887         if ((m->type < 0)
888                         || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
889                 return NULL;
890         return matchers_tostring[m->type](m, buf, buflen);
891 } /* sdb_store_matcher_tostring */
893 int
894 sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
895                 void *user_data)
897         lookup_iter_data_t data = { m, cb, user_data };
899         if (! cb)
900                 return -1;
901         return sdb_store_iterate(lookup_iter, &data);
902 } /* sdb_store_lookup */
904 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */