index fe98e91dd62c7a72e3509a61e07e0ee610ea223a..fa7a8dad7d98a1bb35ca28fb1b896131ee1f13a7 100644 (file)
--- a/src/core/store_lookup.c
+++ b/src/core/store_lookup.c
return 0;
} /* lookup_iter */
+static sdb_store_base_t *
+attr_get(sdb_store_base_t *host, const char *name)
+{
+ sdb_llist_iter_t *iter = NULL;
+ sdb_store_base_t *attr = NULL;
+
+ assert(host->type == SDB_HOST);
+
+ iter = sdb_llist_get_iter(SDB_STORE_OBJ(host)->attributes);
+ while (sdb_llist_iter_has_next(iter)) {
+ sdb_attribute_t *a = SDB_ATTR(sdb_llist_iter_get_next(iter));
+
+ if (strcasecmp(name, SDB_OBJ(a)->name))
+ continue;
+ attr = STORE_BASE(a);
+ break;
+ }
+ sdb_llist_iter_destroy(iter);
+ return attr;
+} /* attr_get */
+
/*
* matcher implementations
*/
static int
-match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj);
-static int
-match_unary(sdb_store_matcher_t *m, sdb_store_base_t *obj);
-
-/* specific matchers */
-
-static int
-match_obj_name(name_matcher_t *m, const char *name)
+match_string(string_matcher_t *m, const char *name)
{
- assert(m);
-
if ((! m->name) && (! m->name_re))
return 1;
/* matches */ 0, NULL, /* flags = */ 0))
return 0;
return 1;
-} /* match_obj_name */
+} /* match_string */
-static char *
-obj_name_tostring(name_matcher_t *m, char *buf, size_t buflen)
+static int
+match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj)
{
- snprintf(buf, buflen, "{ %s%s%s, %p }",
- m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
- m->name_re);
- return buf;
-} /* obj_name_tostring */
+ int status;
-static char *
-logical_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
-{
- char left[buflen + 1], right[buflen + 1];
+ assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
+ assert(OP_M(m)->left && OP_M(m)->right);
- if (! m) {
- /* this should not happen */
- snprintf(buf, buflen, "()");
- return buf;
- }
+ status = sdb_store_matcher_matches(OP_M(m)->left, obj);
+ /* lazy evaluation */
+ if ((! status) && (m->type == MATCHER_AND))
+ return status;
+ else if (status && (m->type == MATCHER_OR))
+ return status;
- assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
- snprintf(buf, buflen, "(%s, %s, %s)",
- m->type == MATCHER_OR ? "OR" : "AND",
- sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
- sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
- return buf;
-} /* logical_tostring */
+ return sdb_store_matcher_matches(OP_M(m)->right, obj);
+} /* match_logical */
-static char *
-unary_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
+static int
+match_unary(sdb_store_matcher_t *m, sdb_store_base_t *obj)
{
- char op[buflen + 1];
-
- if (! m) {
- /* this should not happen */
- snprintf(buf, buflen, "()");
- return buf;
- }
-
assert(m->type == MATCHER_NOT);
- snprintf(buf, buflen, "(NOT, %s)",
- sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
- return buf;
-} /* unary_tostring */
-
-static char *
-name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
-{
- char name[buflen + 1];
- assert(m->type == MATCHER_NAME);
- snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
- SDB_STORE_TYPE_TO_NAME(OBJ_M(m)->obj_type),
- obj_name_tostring(&OBJ_M(m)->name, name, sizeof(name)));
- return buf;
-} /* name_tostring */
-
-static char *
-attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
-{
- char value[buflen + 1];
-
- if (! m) {
- snprintf(buf, buflen, "ATTR{}");
- return buf;
- }
-
- assert(m->type == MATCHER_ATTR);
- snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
- obj_name_tostring(&ATTR_M(m)->value, value, sizeof(value)));
- return buf;
-} /* attr_tostring */
-
-static char *
-host_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
-{
- char name[buflen + 1], attr[buflen + 1];
-
- if (! m) {
- snprintf(buf, buflen, "HOST{}");
- return buf;
- }
+ assert(UOP_M(m)->op);
- assert(m->type == MATCHER_HOST);
- snprintf(buf, buflen, "HOST{ NAME%s, %s }",
- obj_name_tostring(&OBJ_M(m)->name, name, sizeof(name)),
- attr_tostring(SDB_STORE_MATCHER(HOST_M(m)->attr),
- attr, sizeof(attr)));
- return buf;
-} /* host_tostring */
+ return !sdb_store_matcher_matches(UOP_M(m)->op, obj);
+} /* match_unary */
static int
match_name(sdb_store_matcher_t *m, sdb_store_base_t *obj)
sdb_llist_iter_t *iter = NULL;
int status = 0;
- assert(m && obj);
assert(m->type == MATCHER_NAME);
- if (obj->type != SDB_HOST)
- return 0;
-
- switch (OBJ_M(m)->obj_type) {
+ switch (NAME_M(m)->obj_type) {
case SDB_HOST:
- return match_obj_name(&OBJ_M(m)->name, obj->super.name);
+ return match_string(&NAME_M(m)->name, obj->super.name);
break;
case SDB_SERVICE:
iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children);
while (sdb_llist_iter_has_next(iter)) {
sdb_store_base_t *child = STORE_BASE(sdb_llist_iter_get_next(iter));
- if (match_obj_name(&OBJ_M(m)->name, child->super.name)) {
+ if (match_string(&NAME_M(m)->name, child->super.name)) {
status = 1;
break;
}
static int
match_attr(sdb_store_matcher_t *m, sdb_store_base_t *obj)
{
- sdb_llist_iter_t *iter = NULL;
- int status = 0;
+ sdb_attribute_t *attr;
- assert(m && obj);
assert(m->type == MATCHER_ATTR);
+ assert(ATTR_M(m)->name);
- if (obj->type != SDB_HOST)
- return 0;
-
- iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
- while (sdb_llist_iter_has_next(iter)) {
- sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(iter));
+ attr = SDB_ATTR(attr_get(obj, ATTR_M(m)->name));
+ if (attr) {
char buf[sdb_data_strlen(&attr->value) + 1];
-
if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
return 0;
- if (match_obj_name(&ATTR_M(m)->value, buf)) {
- status = 1;
- break;
- }
+ if (match_string(&ATTR_M(m)->value, buf))
+ return 1;
}
- sdb_llist_iter_destroy(iter);
- return status;
+ return 0;
} /* match_attr */
-/* match host specific values;
- * always call this function through match_obj() */
-static int
-match_host(sdb_store_matcher_t *m, sdb_store_base_t *obj)
-{
- int status;
-
- assert(m && obj);
- assert(m->type == MATCHER_HOST);
-
- if (obj->type != SDB_HOST)
- return 0;
-
- status = match_obj_name(&OBJ_M(m)->name, obj->super.name);
- if (! status)
- return status;
-
- if (! HOST_M(m)->attr)
- return 1;
-
- return match_attr(M(HOST_M(m)->attr), obj);
-} /* match_host */
-
-/* generic matchers */
-
typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_base_t *);
/* this array needs to be indexable by the matcher types;
match_unary,
match_name,
match_attr,
- match_host,
};
-typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
-
-static matcher_tostring_cb matchers_tostring[] = {
- logical_tostring,
- logical_tostring,
- unary_tostring,
- name_tostring,
- attr_tostring,
- host_tostring,
-};
-
-static int
-match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj)
-{
- int status;
-
- assert(m && obj);
- assert(OP_M(m)->left && OP_M(m)->right);
-
- status = sdb_store_matcher_matches(OP_M(m)->left, obj);
- /* lazy evaluation */
- if ((! status) && (m->type == MATCHER_AND))
- return status;
- else if (status && (m->type == MATCHER_OR))
- return status;
-
- return sdb_store_matcher_matches(OP_M(m)->right, obj);
-} /* match_logical */
-
-static int
-match_unary(sdb_store_matcher_t *m, sdb_store_base_t *obj)
-{
- assert(m && obj);
- assert(UOP_M(m)->op);
-
- return !sdb_store_matcher_matches(UOP_M(m)->op, obj);
-} /* match_unary */
-
/*
* private matcher types
*/
-/* initializes a name matcher consuming two elements from ap */
+/* initializes a string matcher consuming two elements from ap */
static int
-name_matcher_init(name_matcher_t *m, va_list ap)
+string_matcher_init(string_matcher_t *m, va_list ap)
{
const char *name = va_arg(ap, const char *);
const char *name_re = va_arg(ap, const char *);
return -1;
}
return 0;
-} /* name_matcher_init */
+} /* string_matcher_init */
static void
-name_matcher_destroy(name_matcher_t *m)
+string_matcher_destroy(string_matcher_t *m)
{
if (m->name)
free(m->name);
regfree(m->name_re);
free(m->name_re);
}
-} /* name_matcher_destroy */
+} /* string_matcher_destroy */
+
+static char *
+string_tostring(string_matcher_t *m, char *buf, size_t buflen)
+{
+ snprintf(buf, buflen, "{ %s%s%s, %p }",
+ m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
+ m->name_re);
+ return buf;
+} /* string_tostring */
-/* initializes an object matcher consuming two elements from ap */
+/* initializes a name matcher */
static int
-obj_matcher_init(sdb_object_t *obj, va_list ap)
+name_matcher_init(sdb_object_t *obj, va_list ap)
{
- obj_matcher_t *m = OBJ_M(obj);
+ name_matcher_t *m = NAME_M(obj);
M(obj)->type = MATCHER_NAME;
- return name_matcher_init(&m->name, ap);
-} /* obj_matcher_init */
+ return string_matcher_init(&m->name, ap);
+} /* name_matcher_init */
static void
-obj_matcher_destroy(sdb_object_t *obj)
+name_matcher_destroy(sdb_object_t *obj)
{
- obj_matcher_t *m = OBJ_M(obj);
- name_matcher_destroy(&m->name);
-} /* obj_matcher_destroy */
+ name_matcher_t *m = NAME_M(obj);
+ string_matcher_destroy(&m->name);
+} /* name_matcher_destroy */
+
+static char *
+name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
+{
+ char name[buflen + 1];
+ assert(m->type == MATCHER_NAME);
+ snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
+ SDB_STORE_TYPE_TO_NAME(NAME_M(m)->obj_type),
+ string_tostring(&NAME_M(m)->name, name, sizeof(name)));
+ return buf;
+} /* name_tostring */
static int
attr_matcher_init(sdb_object_t *obj, va_list ap)
if (! attr->name)
return -1;
}
- return name_matcher_init(&attr->value, ap);
+ return string_matcher_init(&attr->value, ap);
} /* attr_matcher_init */
static void
if (attr->name)
free(attr->name);
attr->name = NULL;
- name_matcher_destroy(&attr->value);
+ string_matcher_destroy(&attr->value);
} /* attr_matcher_destroy */
-static int
-host_matcher_init(sdb_object_t *obj, va_list ap)
+static char *
+attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
{
- attr_matcher_t *attr;
- int status;
-
- status = obj_matcher_init(obj, ap);
- if (status)
- return status;
-
- attr = va_arg(ap, attr_matcher_t *);
- sdb_object_ref(SDB_OBJ(attr));
- HOST_M(obj)->attr = attr;
+ char value[buflen + 1];
- M(obj)->type = MATCHER_HOST;
- return 0;
-} /* host_matcher_init */
+ if (! m) {
+ snprintf(buf, buflen, "ATTR{}");
+ return buf;
+ }
-static void
-host_matcher_destroy(sdb_object_t *obj)
-{
- obj_matcher_destroy(obj);
- sdb_object_deref(SDB_OBJ(HOST_M(obj)->attr));
-} /* host_matcher_destroy */
+ assert(m->type == MATCHER_ATTR);
+ snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
+ string_tostring(&ATTR_M(m)->value, value, sizeof(value)));
+ return buf;
+} /* attr_tostring */
static int
op_matcher_init(sdb_object_t *obj, va_list ap)
sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
} /* op_matcher_destroy */
+static char *
+op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
+{
+ char left[buflen + 1], right[buflen + 1];
+
+ if (! m) {
+ /* this should not happen */
+ snprintf(buf, buflen, "()");
+ return buf;
+ }
+
+ assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
+ snprintf(buf, buflen, "(%s, %s, %s)",
+ m->type == MATCHER_OR ? "OR" : "AND",
+ sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
+ sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
+ return buf;
+} /* op_tostring */
+
static int
uop_matcher_init(sdb_object_t *obj, va_list ap)
{
sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
} /* uop_matcher_destroy */
+static char *
+uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
+{
+ char op[buflen + 1];
+
+ if (! m) {
+ /* this should not happen */
+ snprintf(buf, buflen, "()");
+ return buf;
+ }
+
+ assert(m->type == MATCHER_NOT);
+ snprintf(buf, buflen, "(NOT, %s)",
+ sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
+ return buf;
+} /* uop_tostring */
+
static sdb_type_t name_type = {
- /* size = */ sizeof(obj_matcher_t),
- /* init = */ obj_matcher_init,
- /* destroy = */ obj_matcher_destroy,
+ /* size = */ sizeof(name_matcher_t),
+ /* init = */ name_matcher_init,
+ /* destroy = */ name_matcher_destroy,
};
static sdb_type_t attr_type = {
/* destroy = */ attr_matcher_destroy,
};
-static sdb_type_t host_type = {
- /* size = */ sizeof(host_matcher_t),
- /* init = */ host_matcher_init,
- /* destroy = */ host_matcher_destroy,
-};
-
static sdb_type_t op_type = {
/* size = */ sizeof(op_matcher_t),
/* init = */ op_matcher_init,
/* destroy = */ uop_matcher_destroy,
};
+typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
+
+/* this array needs to be indexable by the matcher types;
+ * -> update the enum in store-private.h when updating this */
+static matcher_tostring_cb matchers_tostring[] = {
+ op_tostring,
+ op_tostring,
+ uop_tostring,
+ name_tostring,
+ attr_tostring,
+};
+
/*
* public API
*/
if (! m)
return NULL;
- OBJ_M(m)->obj_type = type;
+ NAME_M(m)->obj_type = type;
return m;
} /* sdb_store_name_matcher */
sdb_store_matcher_t *
sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
{
+ if (! name)
+ return NULL;
+
if (re)
return M(sdb_object_create("attr-matcher", attr_type,
name, NULL, value));
name, value, NULL));
} /* sdb_store_attr_matcher */
-sdb_store_matcher_t *
-sdb_store_host_matcher(const char *host_name, const char *host_name_re,
- sdb_store_matcher_t *attr_matcher)
-{
- return M(sdb_object_create("host-matcher", host_type,
- host_name, host_name_re, attr_matcher));
-} /* sdb_store_host_matcher */
-
sdb_store_matcher_t *
sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
const char *op, const char *value)
int
sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj)
{
+ if (obj->type != SDB_HOST)
+ return 0;
+
/* "NULL" always matches */
if ((! m) || (! obj))
return 1;