Code

store: Make matchers more generic and applicable to non-hosts as well.
[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_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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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         return buf;
512 } /* cond_tostring */
514 static int
515 op_matcher_init(sdb_object_t *obj, va_list ap)
517         M(obj)->type = va_arg(ap, int);
518         if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
519                 return -1;
521         OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
522         sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
523         OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
524         sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
526         if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
527                 return -1;
528         return 0;
529 } /* op_matcher_init */
531 static void
532 op_matcher_destroy(sdb_object_t *obj)
534         if (OP_M(obj)->left)
535                 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
536         if (OP_M(obj)->right)
537                 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
538 } /* op_matcher_destroy */
540 static char *
541 op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
543         char left[buflen + 1], right[buflen + 1];
545         if (! m) {
546                 /* this should not happen */
547                 snprintf(buf, buflen, "()");
548                 return buf;
549         }
551         assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
552         snprintf(buf, buflen, "(%s, %s, %s)",
553                         m->type == MATCHER_OR ? "OR" : "AND",
554                         sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
555                         sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
556         return buf;
557 } /* op_tostring */
559 static int
560 uop_matcher_init(sdb_object_t *obj, va_list ap)
562         M(obj)->type = va_arg(ap, int);
563         if (M(obj)->type != MATCHER_NOT)
564                 return -1;
566         UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
567         sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
569         if (! UOP_M(obj)->op)
570                 return -1;
571         return 0;
572 } /* uop_matcher_init */
574 static void
575 uop_matcher_destroy(sdb_object_t *obj)
577         if (UOP_M(obj)->op)
578                 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
579 } /* uop_matcher_destroy */
581 static char *
582 uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
584         char op[buflen + 1];
586         if (! m) {
587                 /* this should not happen */
588                 snprintf(buf, buflen, "()");
589                 return buf;
590         }
592         assert(m->type == MATCHER_NOT);
593         snprintf(buf, buflen, "(NOT, %s)",
594                         sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
595         return buf;
596 } /* uop_tostring */
598 static int
599 isnull_matcher_init(sdb_object_t *obj, va_list ap)
601         const char *name;
603         M(obj)->type = va_arg(ap, int);
604         if (M(obj)->type != MATCHER_ISNULL)
605                 return -1;
607         name = va_arg(ap, const char *);
608         if (! name)
609                 return -1;
610         ISNULL_M(obj)->attr_name = strdup(name);
611         if (! ISNULL_M(obj)->attr_name)
612                 return -1;
613         return 0;
614 } /* isnull_matcher_init */
616 static void
617 isnull_matcher_destroy(sdb_object_t *obj)
619         if (ISNULL_M(obj)->attr_name)
620                 free(ISNULL_M(obj)->attr_name);
621         ISNULL_M(obj)->attr_name = NULL;
622 } /* isnull_matcher_destroy */
624 static char *
625 isnull_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
627         snprintf(buf, buflen, "(IS NULL, ATTR[%s])", ISNULL_M(m)->attr_name);
628         return buf;
629 } /* isnull_tostring */
631 static sdb_type_t name_type = {
632         /* size = */ sizeof(name_matcher_t),
633         /* init = */ name_matcher_init,
634         /* destroy = */ name_matcher_destroy,
635 };
637 static sdb_type_t attr_type = {
638         /* size = */ sizeof(attr_matcher_t),
639         /* init = */ attr_matcher_init,
640         /* destroy = */ attr_matcher_destroy,
641 };
643 static sdb_type_t cond_type = {
644         /* size = */ sizeof(cond_matcher_t),
645         /* init = */ cond_matcher_init,
646         /* destroy = */ cond_matcher_destroy,
647 };
649 static sdb_type_t op_type = {
650         /* size = */ sizeof(op_matcher_t),
651         /* init = */ op_matcher_init,
652         /* destroy = */ op_matcher_destroy,
653 };
655 static sdb_type_t uop_type = {
656         /* size = */ sizeof(uop_matcher_t),
657         /* init = */ uop_matcher_init,
658         /* destroy = */ uop_matcher_destroy,
659 };
661 static sdb_type_t isnull_type = {
662         /* size = */ sizeof(isnull_matcher_t),
663         /* init = */ isnull_matcher_init,
664         /* destroy = */ isnull_matcher_destroy,
665 };
667 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
669 /* this array needs to be indexable by the matcher types;
670  * -> update the enum in store-private.h when updating this */
671 static matcher_tostring_cb
672 matchers_tostring[] = {
673         op_tostring,
674         op_tostring,
675         uop_tostring,
676         name_tostring,
677         attr_tostring,
678         cond_tostring,
679         cond_tostring,
680         cond_tostring,
681         cond_tostring,
682         cond_tostring,
683         isnull_tostring,
684 };
686 /*
687  * public API
688  */
690 sdb_store_cond_t *
691 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr)
693         return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type,
694                                 name, expr));
695 } /* sdb_store_attr_cond */
697 sdb_store_matcher_t *
698 sdb_store_name_matcher(int type, const char *name, _Bool re)
700         sdb_store_matcher_t *m;
702         if (re)
703                 m = M(sdb_object_create("name-matcher", name_type, NULL, name));
704         else
705                 m = M(sdb_object_create("name-matcher", name_type, name, NULL));
707         if (! m)
708                 return NULL;
710         NAME_M(m)->obj_type = type;
711         return m;
712 } /* sdb_store_name_matcher */
714 sdb_store_matcher_t *
715 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
717         sdb_store_matcher_t *m;
719         if (! name)
720                 return NULL;
722         if (re)
723                 m = M(sdb_object_create("attr-matcher", attr_type,
724                                         name, NULL, value));
725         else
726                 m = M(sdb_object_create("attr-matcher", attr_type,
727                                         name, value, NULL));
728         return m;
729 } /* sdb_store_attr_matcher */
731 sdb_store_matcher_t *
732 sdb_store_lt_matcher(sdb_store_cond_t *cond)
734         return M(sdb_object_create("lt-matcher", cond_type,
735                                 MATCHER_LT, cond));
736 } /* sdb_store_lt_matcher */
738 sdb_store_matcher_t *
739 sdb_store_le_matcher(sdb_store_cond_t *cond)
741         return M(sdb_object_create("le-matcher", cond_type,
742                                 MATCHER_LE, cond));
743 } /* sdb_store_le_matcher */
745 sdb_store_matcher_t *
746 sdb_store_eq_matcher(sdb_store_cond_t *cond)
748         return M(sdb_object_create("eq-matcher", cond_type,
749                                 MATCHER_EQ, cond));
750 } /* sdb_store_eq_matcher */
752 sdb_store_matcher_t *
753 sdb_store_ge_matcher(sdb_store_cond_t *cond)
755         return M(sdb_object_create("ge-matcher", cond_type,
756                                 MATCHER_GE, cond));
757 } /* sdb_store_ge_matcher */
759 sdb_store_matcher_t *
760 sdb_store_gt_matcher(sdb_store_cond_t *cond)
762         return M(sdb_object_create("gt-matcher", cond_type,
763                                 MATCHER_GT, cond));
764 } /* sdb_store_gt_matcher */
766 sdb_store_matcher_t *
767 sdb_store_isnull_matcher(const char *attr_name)
769         return M(sdb_object_create("isnull-matcher", isnull_type,
770                                 MATCHER_ISNULL, attr_name));
771 } /* sdb_store_isnull_matcher */
773 static sdb_store_matcher_t *
774 parse_attr_cmp(const char *attr, const char *op, sdb_store_expr_t *expr)
776         sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
777         sdb_store_matcher_t *m;
778         sdb_store_cond_t *cond;
779         _Bool inv = 0;
781         if (! attr)
782                 return NULL;
784         if (! strcasecmp(op, "IS")) {
785                 if (! expr)
786                         return sdb_store_isnull_matcher(attr);
787                 else
788                         return NULL;
789         }
790         else if (! expr)
791                 return NULL;
792         else if (! strcasecmp(op, "<"))
793                 matcher = sdb_store_lt_matcher;
794         else if (! strcasecmp(op, "<="))
795                 matcher = sdb_store_le_matcher;
796         else if (! strcasecmp(op, "="))
797                 matcher = sdb_store_eq_matcher;
798         else if (! strcasecmp(op, ">="))
799                 matcher = sdb_store_ge_matcher;
800         else if (! strcasecmp(op, ">"))
801                 matcher = sdb_store_gt_matcher;
802         else if (! strcasecmp(op, "!=")) {
803                 matcher = sdb_store_eq_matcher;
804                 inv = 1;
805         }
806         else
807                 return NULL;
809         cond = sdb_store_attr_cond(attr, expr);
810         if (! cond)
811                 return NULL;
813         m = matcher(cond);
814         /* pass ownership to 'm' or destroy in case of an error */
815         sdb_object_deref(SDB_OBJ(cond));
816         if (! m)
817                 return NULL;
819         if (inv) {
820                 sdb_store_matcher_t *tmp;
821                 tmp = sdb_store_inv_matcher(m);
822                 /* pass ownership to the inverse matcher */
823                 sdb_object_deref(SDB_OBJ(m));
824                 m = tmp;
825         }
826         return m;
827 } /* parse_attr_cmp */
829 sdb_store_matcher_t *
830 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
831                 const char *op, sdb_store_expr_t *expr)
833         int type = -1;
834         _Bool inv = 0;
835         _Bool re = 0;
837         sdb_data_t value = SDB_DATA_INIT;
838         sdb_store_matcher_t *m = NULL;
840         if (! strcasecmp(obj_type, "host"))
841                 type = SDB_HOST;
842         else if (! strcasecmp(obj_type, "service"))
843                 type = SDB_SERVICE;
844         else if (! strcasecmp(obj_type, "attribute"))
845                 type = SDB_ATTRIBUTE;
846         else
847                 return NULL;
849         /* XXX: this code sucks! */
850         if (! strcasecmp(op, "=")) {
851                 /* nothing to do */
852         }
853         else if (! strcasecmp(op, "!=")) {
854                 inv = 1;
855         }
856         else if (! strcasecmp(op, "=~")) {
857                 re = 1;
858         }
859         else if (! strcasecmp(op, "!~")) {
860                 inv = 1;
861                 re = 1;
862         }
863         else if (type == SDB_ATTRIBUTE)
864                 return parse_attr_cmp(attr, op, expr);
865         else
866                 return NULL;
868         if (! expr)
869                 return NULL;
871         if (sdb_store_expr_eval(expr, &value))
872                 return NULL;
873         if (value.type != SDB_TYPE_STRING) {
874                 sdb_data_free_datum(&value);
875                 return parse_attr_cmp(attr, op, expr);
876         }
878         if (! attr)
879                 m = sdb_store_name_matcher(type, value.data.string, re);
880         else if (type == SDB_ATTRIBUTE)
881                 m = sdb_store_attr_matcher(attr, value.data.string, re);
883         sdb_data_free_datum(&value);
885         if (! m)
886                 return NULL;
888         if (inv) {
889                 sdb_store_matcher_t *tmp;
890                 tmp = sdb_store_inv_matcher(m);
891                 /* pass ownership to the inverse matcher */
892                 sdb_object_deref(SDB_OBJ(m));
893                 m = tmp;
894         }
895         return m;
896 } /* sdb_store_matcher_parse_cmp */
898 sdb_store_matcher_t *
899 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
901         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
902                                 left, right));
903 } /* sdb_store_dis_matcher */
905 sdb_store_matcher_t *
906 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
908         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
909                                 left, right));
910 } /* sdb_store_con_matcher */
912 sdb_store_matcher_t *
913 sdb_store_inv_matcher(sdb_store_matcher_t *m)
915         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
916 } /* sdb_store_inv_matcher */
918 int
919 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
920                 sdb_store_matcher_t *filter)
922         if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
923                 return 0;
925         /* "NULL" always matches */
926         if ((! m) || (! obj))
927                 return 1;
929         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
930                 return 0;
932         return matchers[m->type](m, obj, filter);
933 } /* sdb_store_matcher_matches */
935 char *
936 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
938         if (! m)
939                 return NULL;
941         if ((m->type < 0)
942                         || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
943                 return NULL;
944         return matchers_tostring[m->type](m, buf, buflen);
945 } /* sdb_store_matcher_tostring */
947 int
948 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
949                 sdb_store_lookup_cb cb, void *user_data)
951         scan_iter_data_t data = { m, filter, cb, user_data };
953         if (! cb)
954                 return -1;
955         return sdb_store_iterate(scan_iter, &data);
956 } /* sdb_store_scan */
958 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */