index b0b7bf8f9345644e037264a2abd50b6d14b697f8..8bdb40e63c41f9ff8bd487390663614ede8082d2 100644 (file)
--- a/src/core/store_lookup.c
+++ b/src/core/store_lookup.c
* private data types
*/
-/* match the name of something */
-typedef struct {
- char *name;
- regex_t *name_re;
-} name_matcher_t;
-
-/* matcher base type */
-struct sdb_store_matcher {
- sdb_object_t super;
- /* type of the matcher */
- int type;
-};
-#define M(m) ((sdb_store_matcher_t *)(m))
-
-/* logical operator matcher */
-typedef struct {
- sdb_store_matcher_t super;
-
- /* left and right hand operands */
- sdb_store_matcher_t *left;
- sdb_store_matcher_t *right;
-} op_matcher_t;
-#define OP_M(m) ((op_matcher_t *)(m))
-
-/* match any type of object by it's base information */
-typedef struct {
- sdb_store_matcher_t super;
-
- /* match by the name of the object */
- name_matcher_t name;
-} obj_matcher_t;
-#define OBJ_M(m) ((obj_matcher_t *)(m))
-
-/* match attributes */
-typedef struct {
- obj_matcher_t super;
- /* XXX: this needs to be more flexible;
- * add support for type-specific operators */
- name_matcher_t value;
-} attr_matcher_t;
-#define ATTR_M(m) ((attr_matcher_t *)(m))
-
-/* match services */
-typedef struct {
- obj_matcher_t super;
- /* match by attributes assigned to the service */
- attr_matcher_t *attr;
-} service_matcher_t;
-#define SERVICE_M(m) ((service_matcher_t *)(m))
-
-/* match hosts */
-typedef struct {
- obj_matcher_t super;
- /* match by services assigned to the host */
- service_matcher_t *service;
- /* match by attributes assigned to the host */
- attr_matcher_t *attr;
-} host_matcher_t;
-#define HOST_M(m) ((host_matcher_t *)(m))
-
typedef struct {
sdb_store_matcher_t *m;
sdb_store_lookup_cb cb;
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);
+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_obj_name(name_matcher_t *m, const char *name)
{
assert(m);
/* matches */ 0, NULL, /* flags = */ 0))
return 0;
return 1;
+} /* match_obj_name */
+
+static char *
+obj_name_tostring(name_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;
+} /* obj_name_tostring */
+
+static char *
+logical_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;
+} /* logical_tostring */
+
+static char *
+unary_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;
+} /* 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 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 }",
+ obj_name_tostring(&OBJ_M(m)->name, name, sizeof(name)),
+ obj_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 }",
+ obj_name_tostring(&OBJ_M(m)->name, name, sizeof(name)),
+ attr_tostring(SDB_STORE_MATCHER(SERVICE_M(m)->attr),
+ attr, sizeof(attr)));
+ return buf;
+} /* service_tostring */
+
+static char *
+host_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
+{
+ char name[buflen + 1], service[buflen + 1], attr[buflen + 1];
+
+ if (! m) {
+ snprintf(buf, buflen, "HOST{}");
+ return buf;
+ }
+
+ assert(m->type == MATCHER_HOST);
+ snprintf(buf, buflen, "HOST{ NAME%s, %s, %s }",
+ obj_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)));
+ return buf;
+} /* host_tostring */
+
+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);
+
+ if (obj->type != SDB_HOST)
+ return 0;
+
+ switch (OBJ_M(m)->obj_type) {
+ case SDB_HOST:
+ return match_obj_name(&OBJ_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;
+ }
+
+ 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)) {
+ status = 1;
+ break;
+ }
+ }
+ sdb_llist_iter_destroy(iter);
+ return status;
} /* match_name */
/* match attribute specific values;
if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
return 0;
- return match_name(&m->value, buf);
+ return match_obj_name(&m->value, buf);
}
} /* match_attr */
/* generic matchers */
-enum {
- MATCHER_OR,
- MATCHER_AND,
- MATCHER_ATTR,
- MATCHER_SERVICE,
- MATCHER_HOST,
-};
-
typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_base_t *);
-/* this array needs to be indexable by the matcher types */
+/* this array needs to be indexable by the matcher types;
+ * -> update the enum in store-private.h when updating this */
static matcher_cb matchers[] = {
match_logical,
match_logical,
+ match_unary,
+ match_name,
match_obj,
match_obj,
match_obj,
};
+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,
+ service_tostring,
+ host_tostring,
+};
+
static int
match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj)
{
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 */
+
static int
match_obj(sdb_store_matcher_t *m, sdb_store_base_t *obj)
{
assert(m && obj);
- status = match_name(&OBJ_M(m)->name, obj->super.name);
+ status = match_obj_name(&OBJ_M(m)->name, obj->super.name);
if (! status)
return status;
case MATCHER_HOST:
return match_host(HOST_M(m), obj);
break;
+ default:
+ assert(m->type != m->type);
+ break;
}
return 0;
} /* match_obj */
obj_matcher_init(sdb_object_t *obj, va_list ap)
{
obj_matcher_t *m = OBJ_M(obj);
+ M(obj)->type = MATCHER_NAME;
return name_matcher_init(&m->name, ap);
} /* obj_matcher_init */
attr_matcher_t *attr = ATTR_M(obj);
int status;
- M(obj)->type = MATCHER_ATTR;
-
status = obj_matcher_init(obj, ap);
if (! status)
status = name_matcher_init(&attr->value, ap);
+
+ M(obj)->type = MATCHER_ATTR;
return status;
} /* attr_matcher_init */
attr_matcher_t *attr;
int status;
- M(obj)->type = MATCHER_SERVICE;
-
status = obj_matcher_init(obj, ap);
if (status)
return status;
sdb_object_ref(SDB_OBJ(attr));
SERVICE_M(obj)->attr = attr;
+
+ M(obj)->type = MATCHER_SERVICE;
return 0;
} /* service_matcher_init */
attr_matcher_t *attr;
int status;
- M(obj)->type = MATCHER_HOST;
-
status = obj_matcher_init(obj, ap);
if (status)
return status;
HOST_M(obj)->service = service;
sdb_object_ref(SDB_OBJ(attr));
HOST_M(obj)->attr = attr;
+
+ M(obj)->type = MATCHER_HOST;
return 0;
} /* host_matcher_init */
sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
} /* op_matcher_destroy */
+static int
+uop_matcher_init(sdb_object_t *obj, va_list ap)
+{
+ M(obj)->type = va_arg(ap, int);
+ if (M(obj)->type != MATCHER_NOT)
+ return -1;
+
+ UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
+ sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
+
+ if (! UOP_M(obj)->op)
+ return -1;
+ return 0;
+} /* uop_matcher_init */
+
+static void
+uop_matcher_destroy(sdb_object_t *obj)
+{
+ if (UOP_M(obj)->op)
+ sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
+} /* uop_matcher_destroy */
+
+static sdb_type_t name_type = {
+ /* size = */ sizeof(obj_matcher_t),
+ /* init = */ obj_matcher_init,
+ /* destroy = */ obj_matcher_destroy,
+};
+
static sdb_type_t attr_type = {
/* size = */ sizeof(attr_matcher_t),
/* init = */ attr_matcher_init,
/* destroy = */ op_matcher_destroy,
};
+static sdb_type_t uop_type = {
+ /* size = */ sizeof(uop_matcher_t),
+ /* init = */ uop_matcher_init,
+ /* destroy = */ uop_matcher_destroy,
+};
+
/*
* public API
*/
+sdb_store_matcher_t *
+sdb_store_name_matcher(int type, const char *name, _Bool re)
+{
+ sdb_store_matcher_t *m;
+
+ 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;
+
+ OBJ_M(m)->obj_type = type;
+ return m;
+} /* sdb_store_name_matcher */
+
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)
host_name, host_name_re, service_matcher, 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 typ = -1;
+ int inv = 0;
+
+ sdb_store_matcher_t *m = NULL;
+
+ const char *matcher = NULL;
+ const char *matcher_re = NULL;
+
+ if (! strcasecmp(obj_type, "host"))
+ typ = SDB_HOST;
+ else if (! strcasecmp(obj_type, "service"))
+ typ = SDB_SERVICE;
+ else if (! strcasecmp(obj_type, "attribute"))
+ typ = SDB_ATTRIBUTE;
+
+ /* TODO: support other operators */
+ if (! strcasecmp(op, "=")) {
+ matcher = value;
+ }
+ else if (! strcasecmp(op, "!=")) {
+ matcher = value;
+ inv = 1;
+ }
+ else if (! strcasecmp(op, "=~")) {
+ matcher_re = value;
+ }
+ else if (! strcasecmp(op, "!~")) {
+ matcher_re = value;
+ inv = 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
+ 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)
+ return NULL;
+
+ /* pass ownership to the host matcher */
+ sdb_object_deref(SDB_OBJ(HOST_M(m)->service));
+ sdb_object_deref(SDB_OBJ(HOST_M(m)->attr));
+
+ if (inv) {
+ sdb_store_matcher_t *tmp;
+ tmp = sdb_store_inv_matcher(m);
+ /* pass ownership to the inverse matcher */
+ sdb_object_deref(SDB_OBJ(m));
+ m = tmp;
+ }
+ return m;
+} /* sdb_store_matcher_parse_cmp */
+
sdb_store_matcher_t *
sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
{
left, right));
} /* sdb_store_con_matcher */
+sdb_store_matcher_t *
+sdb_store_inv_matcher(sdb_store_matcher_t *m)
+{
+ return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
+} /* sdb_store_inv_matcher */
+
int
sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj)
{
return matchers[m->type](m, obj);
} /* sdb_store_matcher_matches */
+char *
+sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
+{
+ if (! m)
+ return NULL;
+
+ if ((m->type < 0)
+ || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
+ return NULL;
+ return matchers_tostring[m->type](m, buf, buflen);
+} /* sdb_store_matcher_tostring */
+
int
sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
void *user_data)