index bb32157402d8c047a20326aab81e069a4073e5f1..b5c936aae355419eadf52dfd7e1c99110e38701f 100644 (file)
--- a/src/core/store_lookup.c
+++ b/src/core/store_lookup.c
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);
-static int
-match_obj(sdb_store_matcher_t *m, sdb_store_base_t *obj);
/* specific matchers */
static int
-match_name(name_matcher_t *m, const char *name)
+match_string(string_matcher_t *m, const char *name)
{
assert(m);
/* matches */ 0, NULL, /* flags = */ 0))
return 0;
return 1;
-} /* match_name */
+} /* match_string */
static char *
-name_tostring(name_matcher_t *m, char *buf, size_t buflen)
+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;
-} /* name_tostring */
+} /* string_tostring */
static char *
logical_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
} /* unary_tostring */
static char *
-attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
+name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
{
- char name[buflen + 1], value[buflen + 1];
-
- if (! m) {
- snprintf(buf, buflen, "ATTR{}");
- return buf;
- }
-
- assert(m->type == MATCHER_ATTR);
- snprintf(buf, buflen, "ATTR{ NAME%s, VALUE%s }",
- name_tostring(&OBJ_M(m)->name, name, sizeof(name)),
- name_tostring(&ATTR_M(m)->value, value, sizeof(value)));
- return buf;
-} /* attr_tostring */
-
-static char *
-service_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
-{
- char name[buflen + 1], attr[buflen + 1];
-
- if (! m) {
- snprintf(buf, buflen, "SERVICE{}");
- return buf;
- }
-
- assert(m->type == MATCHER_SERVICE);
- snprintf(buf, buflen, "SERVICE{ NAME%s, %s }",
- name_tostring(&OBJ_M(m)->name, name, sizeof(name)),
- attr_tostring(SDB_STORE_MATCHER(SERVICE_M(m)->attr),
- attr, sizeof(attr)));
+ 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;
-} /* service_tostring */
+} /* name_tostring */
static char *
-host_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
+attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
{
- char name[buflen + 1], service[buflen + 1], attr[buflen + 1];
+ char value[buflen + 1];
if (! m) {
- snprintf(buf, buflen, "HOST{}");
+ snprintf(buf, buflen, "ATTR{}");
return buf;
}
- assert(m->type == MATCHER_HOST);
- snprintf(buf, buflen, "HOST{ NAME%s, %s, %s }",
- name_tostring(&OBJ_M(m)->name, name, sizeof(name)),
- service_tostring(SDB_STORE_MATCHER(HOST_M(m)->service),
- service, sizeof(service)),
- attr_tostring(SDB_STORE_MATCHER(HOST_M(m)->attr),
- attr, sizeof(attr)));
+ 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;
-} /* host_tostring */
-
-/* match attribute specific values;
- * always call this function through match_obj() */
-static int
-match_attr(attr_matcher_t *m, sdb_store_base_t *obj)
-{
- assert(m && obj);
-
- if (obj->type != SDB_ATTRIBUTE)
- return 0;
-
- {
- sdb_attribute_t *attr = SDB_ATTR(obj);
- char buf[sdb_data_strlen(&attr->value) + 1];
-
- if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
- return 0;
- return match_name(&m->value, buf);
- }
-} /* match_attr */
+} /* attr_tostring */
-/* match service specific values;
- * always call this function through match_obj() */
static int
-match_service(service_matcher_t *m, sdb_store_base_t *obj)
+match_name(sdb_store_matcher_t *m, sdb_store_base_t *obj)
{
- sdb_llist_iter_t *iter;
+ sdb_llist_iter_t *iter = NULL;
+ int status = 0;
assert(m && obj);
+ assert(m->type == MATCHER_NAME);
- if (obj->type != SDB_SERVICE)
+ if (obj->type != SDB_HOST)
return 0;
- if (! m->attr)
- return 1;
+ switch (NAME_M(m)->obj_type) {
+ case SDB_HOST:
+ return match_string(&NAME_M(m)->name, obj->super.name);
+ break;
+ case SDB_SERVICE:
+ iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children);
+ break;
+ case SDB_ATTRIBUTE:
+ iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
+ break;
+ }
- iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
while (sdb_llist_iter_has_next(iter)) {
- sdb_store_base_t *attr = STORE_BASE(sdb_llist_iter_get_next(iter));
-
- /* if any of the attributes matches we found a matching service */
- if (match_obj(M(m->attr), attr)) {
- sdb_llist_iter_destroy(iter);
- return 1;
+ sdb_store_base_t *child = STORE_BASE(sdb_llist_iter_get_next(iter));
+ if (match_string(&NAME_M(m)->name, child->super.name)) {
+ status = 1;
+ break;
}
}
sdb_llist_iter_destroy(iter);
- return 0;
-} /* match_service */
+ return status;
+} /* match_name */
-/* match host specific values;
- * always call this function through match_obj() */
static int
-match_host(host_matcher_t *m, sdb_store_base_t *obj)
+match_attr(sdb_store_matcher_t *m, sdb_store_base_t *obj)
{
- sdb_llist_iter_t *iter;
- int status;
+ sdb_llist_iter_t *iter = NULL;
+ int status = 0;
assert(m && obj);
+ assert(m->type == MATCHER_ATTR);
if (obj->type != SDB_HOST)
return 0;
- if (m->service) {
- iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children);
- status = 0;
- }
- else {
- iter = NULL;
- status = 1;
- }
+ iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
while (sdb_llist_iter_has_next(iter)) {
- sdb_store_base_t *service = STORE_BASE(sdb_llist_iter_get_next(iter));
+ sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(iter));
+ char buf[sdb_data_strlen(&attr->value) + 1];
+
+ if (ATTR_M(m)->name && strcasecmp(ATTR_M(m)->name, SDB_OBJ(attr)->name))
+ continue;
- /* found a matching service */
- if (match_obj(M(m->service), service)) {
+ if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
+ return 0;
+ if (match_string(&ATTR_M(m)->value, buf)) {
status = 1;
break;
}
}
sdb_llist_iter_destroy(iter);
-
- if (! status)
- return status;
- else if (! m->attr)
- return 1;
-
- iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
- while (sdb_llist_iter_has_next(iter)) {
- sdb_store_base_t *attr = STORE_BASE(sdb_llist_iter_get_next(iter));
-
- /* if any attribute matches, we found a matching host */
- if (match_obj(M(m->attr), attr)) {
- sdb_llist_iter_destroy(iter);
- return 1;
- }
- }
- sdb_llist_iter_destroy(iter);
- return 0;
-} /* match_host */
+ return status;
+} /* match_attr */
/* generic matchers */
match_logical,
match_logical,
match_unary,
- match_obj,
- match_obj,
- match_obj,
+ match_name,
+ match_attr,
};
typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
logical_tostring,
logical_tostring,
unary_tostring,
+ name_tostring,
attr_tostring,
- service_tostring,
- host_tostring,
};
static int
return !sdb_store_matcher_matches(UOP_M(m)->op, obj);
} /* match_unary */
-static int
-match_obj(sdb_store_matcher_t *m, sdb_store_base_t *obj)
-{
- int status;
-
- assert(m && obj);
-
- status = match_name(&OBJ_M(m)->name, obj->super.name);
- if (! status)
- return status;
-
- switch (m->type) {
- case MATCHER_ATTR:
- return match_attr(ATTR_M(m), obj);
- break;
- case MATCHER_SERVICE:
- return match_service(SERVICE_M(m), obj);
- break;
- case MATCHER_HOST:
- return match_host(HOST_M(m), obj);
- break;
- }
- return 0;
-} /* match_obj */
-
/*
* 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 */
-/* 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);
- return name_matcher_init(&m->name, ap);
-} /* obj_matcher_init */
+ name_matcher_t *m = NAME_M(obj);
+ M(obj)->type = MATCHER_NAME;
+ 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 int
attr_matcher_init(sdb_object_t *obj, va_list ap)
{
attr_matcher_t *attr = ATTR_M(obj);
- int status;
+ const char *name = va_arg(ap, const char *);
M(obj)->type = MATCHER_ATTR;
-
- status = obj_matcher_init(obj, ap);
- if (! status)
- status = name_matcher_init(&attr->value, ap);
- return status;
+ if (name) {
+ attr->name = strdup(name);
+ if (! attr->name)
+ return -1;
+ }
+ return string_matcher_init(&attr->value, ap);
} /* attr_matcher_init */
static void
attr_matcher_destroy(sdb_object_t *obj)
{
attr_matcher_t *attr = ATTR_M(obj);
-
- obj_matcher_destroy(obj);
- name_matcher_destroy(&attr->value);
+ if (attr->name)
+ free(attr->name);
+ attr->name = NULL;
+ string_matcher_destroy(&attr->value);
} /* attr_matcher_destroy */
-static int
-service_matcher_init(sdb_object_t *obj, va_list ap)
-{
- attr_matcher_t *attr;
- int status;
-
- M(obj)->type = MATCHER_SERVICE;
-
- status = obj_matcher_init(obj, ap);
- if (status)
- return status;
-
- attr = va_arg(ap, attr_matcher_t *);
-
- sdb_object_ref(SDB_OBJ(attr));
- SERVICE_M(obj)->attr = attr;
- return 0;
-} /* service_matcher_init */
-
-static void
-service_matcher_destroy(sdb_object_t *obj)
-{
- obj_matcher_destroy(obj);
- sdb_object_deref(SDB_OBJ(SERVICE_M(obj)->attr));
-} /* service_matcher_destroy */
-
-static int
-host_matcher_init(sdb_object_t *obj, va_list ap)
-{
- service_matcher_t *service;
- attr_matcher_t *attr;
- int status;
-
- M(obj)->type = MATCHER_HOST;
-
- status = obj_matcher_init(obj, ap);
- if (status)
- return status;
-
- service = va_arg(ap, service_matcher_t *);
- attr = va_arg(ap, attr_matcher_t *);
-
- sdb_object_ref(SDB_OBJ(service));
- HOST_M(obj)->service = service;
- sdb_object_ref(SDB_OBJ(attr));
- HOST_M(obj)->attr = attr;
- return 0;
-} /* host_matcher_init */
-
-static void
-host_matcher_destroy(sdb_object_t *obj)
-{
- obj_matcher_destroy(obj);
- sdb_object_deref(SDB_OBJ(HOST_M(obj)->service));
- sdb_object_deref(SDB_OBJ(HOST_M(obj)->attr));
-} /* host_matcher_destroy */
-
static int
op_matcher_init(sdb_object_t *obj, va_list ap)
{
sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
} /* uop_matcher_destroy */
+static sdb_type_t name_type = {
+ /* size = */ sizeof(name_matcher_t),
+ /* init = */ name_matcher_init,
+ /* destroy = */ name_matcher_destroy,
+};
+
static sdb_type_t attr_type = {
/* size = */ sizeof(attr_matcher_t),
/* init = */ attr_matcher_init,
/* destroy = */ attr_matcher_destroy,
};
-static sdb_type_t service_type = {
- /* size = */ sizeof(service_matcher_t),
- /* init = */ service_matcher_init,
- /* destroy = */ service_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,
*/
sdb_store_matcher_t *
-sdb_store_attr_matcher(const char *attr_name, const char *attr_name_re,
- const char *attr_value, const char *attr_value_re)
+sdb_store_name_matcher(int type, const char *name, _Bool re)
{
- return M(sdb_object_create("attr-matcher", attr_type,
- attr_name, attr_name_re, attr_value, attr_value_re));
-} /* sdb_store_attr_matcher */
+ sdb_store_matcher_t *m;
-sdb_store_matcher_t *
-sdb_store_service_matcher(const char *service_name, const char *service_name_re,
- sdb_store_matcher_t *attr_matcher)
-{
- return M(sdb_object_create("service-matcher", service_type,
- service_name, service_name_re, attr_matcher));
-} /* sdb_store_service_matcher */
+ if (re)
+ m = M(sdb_object_create("name-matcher", name_type,
+ NULL, name));
+ else
+ m = M(sdb_object_create("name-matcher", name_type,
+ name, NULL));
+
+ if (! m)
+ return NULL;
+
+ NAME_M(m)->obj_type = type;
+ return m;
+} /* sdb_store_name_matcher */
sdb_store_matcher_t *
-sdb_store_host_matcher(const char *host_name, const char *host_name_re,
- sdb_store_matcher_t *service_matcher,
- sdb_store_matcher_t *attr_matcher)
+sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
{
- return M(sdb_object_create("host-matcher", host_type,
- host_name, host_name_re, service_matcher, attr_matcher));
-} /* sdb_store_host_matcher */
+ if (re)
+ return M(sdb_object_create("attr-matcher", attr_type,
+ name, NULL, value));
+ return M(sdb_object_create("attr-matcher", attr_type,
+ name, value, NULL));
+} /* sdb_store_attr_matcher */
sdb_store_matcher_t *
sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
const char *op, const char *value)
{
- int typ = -1;
- int inv = 0;
+ int type = -1;
+ _Bool inv = 0;
+ _Bool re = 0;
sdb_store_matcher_t *m = NULL;
- const char *matcher = NULL;
- const char *matcher_re = NULL;
-
if (! strcasecmp(obj_type, "host"))
- typ = SDB_HOST;
+ type = SDB_HOST;
else if (! strcasecmp(obj_type, "service"))
- typ = SDB_SERVICE;
+ type = SDB_SERVICE;
else if (! strcasecmp(obj_type, "attribute"))
- typ = SDB_ATTRIBUTE;
+ type = SDB_ATTRIBUTE;
/* TODO: support other operators */
if (! strcasecmp(op, "=")) {
- matcher = value;
+ /* nothing to do */
}
else if (! strcasecmp(op, "!=")) {
- matcher = value;
inv = 1;
}
else if (! strcasecmp(op, "=~")) {
- matcher_re = value;
+ re = 1;
}
else if (! strcasecmp(op, "!~")) {
- matcher_re = value;
inv = 1;
+ re = 1;
}
else
return NULL;
- if (! strcasecmp(attr, "name")) {
- /* accept */
- }
- else if (typ == SDB_ATTRIBUTE)
- m = sdb_store_host_matcher(/* name = */ NULL, NULL,
- /* service = */ NULL,
- sdb_store_attr_matcher(attr, NULL, matcher, matcher_re));
- else
+ if (! strcasecmp(attr, "name"))
+ m = sdb_store_name_matcher(type, value, re);
+ else if (type == SDB_ATTRIBUTE)
+ m = sdb_store_attr_matcher(attr, value, re);
+
+ if (! m)
return NULL;
- if (m) {
- /* accept the attribute value matcher */
- }
- else if (typ == SDB_HOST)
- m = sdb_store_host_matcher(matcher, matcher_re, NULL, NULL);
- else if (typ == SDB_SERVICE)
- m = sdb_store_host_matcher(/* name = */ NULL, NULL,
- sdb_store_service_matcher(matcher, matcher_re, NULL),
- /* attr = */ NULL);
- else if (typ == SDB_ATTRIBUTE)
- m = sdb_store_host_matcher(/* name = */ NULL, NULL,
- /* service = */ NULL,
- sdb_store_attr_matcher(matcher, matcher_re, NULL, NULL));
-
- if (m && inv) {
+ if (inv) {
sdb_store_matcher_t *tmp;
tmp = sdb_store_inv_matcher(m);
/* pass ownership to the inverse matcher */