X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Fcore%2Fstore_lookup.c;h=489dd1ac52c6fa817df40046d5f0725b6fb45386;hp=d4a3d6cccb83ba3612416faec7edd3b03995dedc;hb=56b97a180a53aecbfe9f7162b8ece3faae973cf9;hpb=39a45905e0b237e458b1826ff9b4fad1c4a59550 diff --git a/src/core/store_lookup.c b/src/core/store_lookup.c index d4a3d6c..489dd1a 100644 --- a/src/core/store_lookup.c +++ b/src/core/store_lookup.c @@ -47,271 +47,274 @@ #include #include +#include + /* * 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)) + sdb_store_matcher_t *m; + sdb_store_lookup_cb cb; + void *user_data; +} lookup_iter_data_t; -/* logical operator matcher */ -typedef struct { - sdb_store_matcher_t super; +/* + * private helper functions + */ - /* 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)) +static int +lookup_iter(sdb_store_obj_t *obj, void *user_data) +{ + lookup_iter_data_t *d = user_data; -/* match any type of object by it's base information */ -typedef struct { - sdb_store_matcher_t super; + if (sdb_store_matcher_matches(d->m, obj)) + return d->cb(obj, d->user_data); + return 0; +} /* lookup_iter */ - /* match by the name of the object */ - name_matcher_t name; -} obj_matcher_t; -#define OBJ_M(m) ((obj_matcher_t *)(m)) +static sdb_attribute_t * +attr_get(sdb_host_t *host, const char *name) +{ + sdb_llist_iter_t *iter = NULL; + sdb_attribute_t *attr = NULL; -/* 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)) + iter = sdb_llist_get_iter(host->attributes); + while (sdb_llist_iter_has_next(iter)) { + sdb_attribute_t *a = ATTR(sdb_llist_iter_get_next(iter)); -/* 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)) + if (strcasecmp(name, SDB_OBJ(a)->name)) + continue; + + assert(STORE_OBJ(a)->type == SDB_ATTRIBUTE); + attr = a; + break; + } + sdb_llist_iter_destroy(iter); + return attr; +} /* attr_get */ /* - * matcher implementations + * conditional implementations */ static int -match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj); -static int -match_obj(sdb_store_matcher_t *m, sdb_store_base_t *obj); +attr_cmp(sdb_host_t *host, sdb_store_cond_t *cond) +{ + sdb_attribute_t *attr; -/* specific matchers */ + attr = attr_get(host, ATTR_C(cond)->name); + if (! attr) + return INT_MAX; + if (attr->value.type != ATTR_C(cond)->value.type) + return INT_MAX; + return sdb_data_cmp(&attr->value, &ATTR_C(cond)->value); +} /* attr_cmp */ + +/* + * matcher implementations + */ static int -match_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 0; + return 1; if (! name) name = ""; if (m->name && strcasecmp(m->name, name)) - return -1; + return 0; if (m->name_re && regexec(m->name_re, name, /* matches */ 0, NULL, /* flags = */ 0)) - return -1; - return 0; -} /* match_name */ + return 0; + return 1; +} /* match_string */ -/* match attribute specific values; - * always call this function through match_obj() */ static int -match_attr(attr_matcher_t *m, sdb_store_base_t *obj) +match_logical(sdb_store_matcher_t *m, sdb_host_t *host) { - assert(m && obj); + int status; - if (obj->type != SDB_ATTRIBUTE) - return -1; + assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR)); + assert(OP_M(m)->left && OP_M(m)->right); - { - sdb_attribute_t *attr = SDB_ATTR(obj); - char buf[sdb_data_strlen(&attr->value) + 1]; + status = sdb_store_matcher_matches(OP_M(m)->left, STORE_OBJ(host)); + /* lazy evaluation */ + if ((! status) && (m->type == MATCHER_AND)) + return status; + else if (status && (m->type == MATCHER_OR)) + return status; - if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0) - return -1; - return match_name(&m->value, buf); - } -} /* match_attr */ + return sdb_store_matcher_matches(OP_M(m)->right, STORE_OBJ(host)); +} /* match_logical */ -/* 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_unary(sdb_store_matcher_t *m, sdb_host_t *host) { - sdb_llist_iter_t *iter; - - assert(m && obj); + assert(m->type == MATCHER_NOT); + assert(UOP_M(m)->op); - if (obj->type != SDB_SERVICE) - return -1; - - if (! m->attr) - return 0; - - 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)); + return !sdb_store_matcher_matches(UOP_M(m)->op, STORE_OBJ(host)); +} /* match_unary */ - /* if any of the attributes matches we found a matching service */ - if (! match_obj(M(m->attr), attr)) { - sdb_llist_iter_destroy(iter); - return 0; - } - } - sdb_llist_iter_destroy(iter); - return -1; -} /* match_service */ - -/* 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_name(sdb_store_matcher_t *m, sdb_host_t *host) { - sdb_llist_iter_t *iter; - int status; + sdb_llist_iter_t *iter = NULL; + int status = 0; - assert(m && obj); + assert(m->type == MATCHER_NAME); - if (obj->type != SDB_HOST) - return -1; - - if (m->service) { - iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children); - status = -1; - } - else { - iter = NULL; - status = 0; + switch (NAME_M(m)->obj_type) { + case SDB_HOST: + return match_string(&NAME_M(m)->name, SDB_OBJ(host)->name); + break; + case SDB_SERVICE: + iter = sdb_llist_get_iter(host->services); + break; + case SDB_ATTRIBUTE: + iter = sdb_llist_get_iter(host->attributes); + break; } - while (sdb_llist_iter_has_next(iter)) { - sdb_store_base_t *service = STORE_BASE(sdb_llist_iter_get_next(iter)); - /* found a matching service */ - if (! match_obj(M(m->service), service)) { - status = 0; + while (sdb_llist_iter_has_next(iter)) { + sdb_object_t *child = sdb_llist_iter_get_next(iter); + if (match_string(&NAME_M(m)->name, child->name)) { + status = 1; break; } } sdb_llist_iter_destroy(iter); + return status; +} /* match_name */ - if (status) - return status; - else if (! m->attr) - return 0; +static int +match_attr(sdb_store_matcher_t *m, sdb_host_t *host) +{ + sdb_attribute_t *attr; - 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)); + assert(m->type == MATCHER_ATTR); + assert(ATTR_M(m)->name); - /* if any attribute matches, we found a matching host */ - if (! match_obj(M(m->attr), attr)) { - sdb_llist_iter_destroy(iter); + attr = attr_get(host, 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_string(&ATTR_M(m)->value, buf)) + return 1; } - sdb_llist_iter_destroy(iter); - return -1; -} /* match_host */ + return 0; +} /* match_attr */ -/* generic matchers */ +static int +match_lt(sdb_store_matcher_t *m, sdb_host_t *host) +{ + int status; + assert(m->type == MATCHER_LT); + status = COND_M(m)->cond->cmp(host, COND_M(m)->cond); + return (status != INT_MAX) && (status < 0); +} /* match_lt */ -enum { - MATCHER_OR, - MATCHER_AND, - MATCHER_ATTR, - MATCHER_SERVICE, - MATCHER_HOST, -}; +static int +match_le(sdb_store_matcher_t *m, sdb_host_t *host) +{ + int status; + assert(m->type == MATCHER_LE); + status = COND_M(m)->cond->cmp(host, COND_M(m)->cond); + return (status != INT_MAX) && (status <= 0); +} /* match_le */ -typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_base_t *); +static int +match_eq(sdb_store_matcher_t *m, sdb_host_t *host) +{ + int status; + assert(m->type == MATCHER_EQ); + status = COND_M(m)->cond->cmp(host, COND_M(m)->cond); + return (status != INT_MAX) && (! status); +} /* match_eq */ -/* this array needs to be indexable by the matcher types */ -static matcher_cb matchers[] = { - match_logical, - match_logical, - match_obj, - match_obj, - match_obj, -}; +static int +match_ge(sdb_store_matcher_t *m, sdb_host_t *host) +{ + int status; + assert(m->type == MATCHER_GE); + status = COND_M(m)->cond->cmp(host, COND_M(m)->cond); + return (status != INT_MAX) && (status >= 0); +} /* match_ge */ static int -match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj) +match_gt(sdb_store_matcher_t *m, sdb_host_t *host) { int status; + assert(m->type == MATCHER_GT); + status = COND_M(m)->cond->cmp(host, COND_M(m)->cond); + return (status != INT_MAX) && (status > 0); +} /* match_gt */ - assert(m && obj); - assert(OP_M(m)->left && OP_M(m)->right); +typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_host_t *); - 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; +/* 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_attr, + match_lt, + match_le, + match_eq, + match_ge, + match_gt, +}; - return sdb_store_matcher_matches(OP_M(m)->right, obj); -} /* match_logical */ +/* + * private conditional types + */ static int -match_obj(sdb_store_matcher_t *m, sdb_store_base_t *obj) +attr_cond_init(sdb_object_t *obj, va_list ap) { - int status; + const char *name = va_arg(ap, const char *); + const sdb_data_t *value = va_arg(ap, const sdb_data_t *); - assert(m && obj); + if (! name) + return -1; - status = match_name(&OBJ_M(m)->name, obj->super.name); - if (status) - return status; + SDB_STORE_COND(obj)->cmp = attr_cmp; - 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 -1; -} /* match_obj */ + ATTR_C(obj)->name = strdup(name); + if (! ATTR_C(obj)->name) + return -1; + if (sdb_data_copy(&ATTR_C(obj)->value, value)) + return -1; + return 0; +} /* attr_cond_init */ + +static void +attr_cond_destroy(sdb_object_t *obj) +{ + if (ATTR_C(obj)->name) + free(ATTR_C(obj)->name); + sdb_data_free_datum(&ATTR_C(obj)->value); +} /* attr_cond_destroy */ + +static sdb_type_t attr_cond_type = { + /* size = */ sizeof(attr_cond_t), + /* init = */ attr_cond_init, + /* destroy = */ attr_cond_destroy, +}; /* * 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 *); @@ -329,10 +332,10 @@ name_matcher_init(name_matcher_t *m, va_list ap) 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); @@ -340,102 +343,120 @@ name_matcher_destroy(name_matcher_t *m) 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); - 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 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) { 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) +static char * +attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen) { - attr_matcher_t *attr; - int status; + char value[buflen + 1]; - M(obj)->type = MATCHER_SERVICE; - - status = obj_matcher_init(obj, ap); - if (status) - return status; - - attr = va_arg(ap, attr_matcher_t *); + if (! m) { + snprintf(buf, buflen, "ATTR{}"); + return buf; + } - 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 */ + 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 -host_matcher_init(sdb_object_t *obj, va_list ap) +cond_matcher_init(sdb_object_t *obj, va_list ap) { - service_matcher_t *service; - attr_matcher_t *attr; - int status; + int type = va_arg(ap, int); + sdb_store_cond_t *cond = va_arg(ap, sdb_store_cond_t *); - M(obj)->type = MATCHER_HOST; - - status = obj_matcher_init(obj, ap); - if (status) - return status; + if (! cond) + return -1; - service = va_arg(ap, service_matcher_t *); - attr = va_arg(ap, attr_matcher_t *); + sdb_object_ref(SDB_OBJ(cond)); - sdb_object_ref(SDB_OBJ(service)); - HOST_M(obj)->service = service; - sdb_object_ref(SDB_OBJ(attr)); - HOST_M(obj)->attr = attr; + M(obj)->type = type; + COND_M(obj)->cond = cond; return 0; -} /* host_matcher_init */ +} /* cond_matcher_init */ static void -host_matcher_destroy(sdb_object_t *obj) +cond_matcher_destroy(sdb_object_t *obj) +{ + sdb_object_deref(SDB_OBJ(COND_M(obj)->cond)); +} /* cond_matcher_destroy */ + +static char * +cond_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen) { - 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 */ + if (COND_M(m)->cond->cmp == attr_cmp) { + char value[buflen]; + if (sdb_data_format(&ATTR_C(COND_M(m)->cond)->value, + value, sizeof(value), SDB_SINGLE_QUOTED) < 0) + snprintf(value, sizeof(value), "ERR"); + snprintf(buf, buflen, "ATTR[%s]{ %s %s }", + ATTR_C(COND_M(m)->cond)->name, MATCHER_SYM(m->type), value); + } + return buf; +} /* cond_tostring */ static int op_matcher_init(sdb_object_t *obj, va_list ap) @@ -463,22 +484,80 @@ op_matcher_destroy(sdb_object_t *obj) 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) +{ + 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 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(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 cond_type = { + /* size = */ sizeof(cond_matcher_t), + /* init = */ cond_matcher_init, + /* destroy = */ cond_matcher_destroy, }; static sdb_type_t op_type = { @@ -487,34 +566,218 @@ static sdb_type_t op_type = { /* destroy = */ op_matcher_destroy, }; +static sdb_type_t uop_type = { + /* size = */ sizeof(uop_matcher_t), + /* init = */ uop_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, + cond_tostring, + cond_tostring, + cond_tostring, + cond_tostring, + cond_tostring, +}; + /* * public API */ +sdb_store_cond_t * +sdb_store_attr_cond(const char *name, const sdb_data_t *value) +{ + return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type, + name, value)); +} /* sdb_store_attr_cond */ + +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; + + NAME_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) +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)); return M(sdb_object_create("attr-matcher", attr_type, - attr_name, attr_name_re, attr_value, attr_value_re)); + name, value, NULL)); } /* sdb_store_attr_matcher */ sdb_store_matcher_t * -sdb_store_service_matcher(const char *service_name, const char *service_name_re, - sdb_store_matcher_t *attr_matcher) +sdb_store_lt_matcher(sdb_store_cond_t *cond) +{ + return M(sdb_object_create("lt-matcher", cond_type, + MATCHER_LT, cond)); +} /* sdb_store_lt_matcher */ + +sdb_store_matcher_t * +sdb_store_le_matcher(sdb_store_cond_t *cond) +{ + return M(sdb_object_create("le-matcher", cond_type, + MATCHER_LE, cond)); +} /* sdb_store_le_matcher */ + +sdb_store_matcher_t * +sdb_store_eq_matcher(sdb_store_cond_t *cond) { - return M(sdb_object_create("service-matcher", service_type, - service_name, service_name_re, attr_matcher)); -} /* sdb_store_service_matcher */ + return M(sdb_object_create("eq-matcher", cond_type, + MATCHER_EQ, cond)); +} /* sdb_store_eq_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_ge_matcher(sdb_store_cond_t *cond) { - return M(sdb_object_create("host-matcher", host_type, - host_name, host_name_re, service_matcher, attr_matcher)); -} /* sdb_store_host_matcher */ + return M(sdb_object_create("ge-matcher", cond_type, + MATCHER_GE, cond)); +} /* sdb_store_ge_matcher */ + +sdb_store_matcher_t * +sdb_store_gt_matcher(sdb_store_cond_t *cond) +{ + return M(sdb_object_create("gt-matcher", cond_type, + MATCHER_GT, cond)); +} /* sdb_store_gt_matcher */ + +static sdb_store_matcher_t * +parse_attr_cmp(const char *attr, const char *op, const sdb_data_t *value) +{ + sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL; + sdb_store_matcher_t *m; + sdb_store_cond_t *cond; + _Bool inv = 0; + + /* TODO: this will reject any attributes called "name"; + * use a different syntax for querying objects by name */ + if (! strcasecmp(attr, "name")) + return NULL; + + if (! strcasecmp(op, "<")) + matcher = sdb_store_lt_matcher; + else if (! strcasecmp(op, "<=")) + matcher = sdb_store_le_matcher; + else if (! strcasecmp(op, "=")) + matcher = sdb_store_eq_matcher; + else if (! strcasecmp(op, ">=")) + matcher = sdb_store_ge_matcher; + else if (! strcasecmp(op, ">")) + matcher = sdb_store_gt_matcher; + else if (! strcasecmp(op, "!=")) { + matcher = sdb_store_eq_matcher; + inv = 1; + } + else + return NULL; + + cond = sdb_store_attr_cond(attr, value); + if (! cond) + return NULL; + + m = matcher(cond); + /* pass ownership to 'm' or destroy in case of an error */ + sdb_object_deref(SDB_OBJ(cond)); + if (! m) + return NULL; + + 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; +} /* parse_attr_cmp */ + +sdb_store_matcher_t * +sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr, + const char *op, const sdb_data_t *value) +{ + int type = -1; + _Bool inv = 0; + _Bool re = 0; + + sdb_store_matcher_t *m = NULL; + + if (! strcasecmp(obj_type, "host")) + type = SDB_HOST; + else if (! strcasecmp(obj_type, "service")) + type = SDB_SERVICE; + else if (! strcasecmp(obj_type, "attribute")) + type = SDB_ATTRIBUTE; + else + return NULL; + + /* XXX: this code sucks! */ + if (! strcasecmp(op, "=")) { + /* nothing to do */ + } + else if (! strcasecmp(op, "!=")) { + inv = 1; + } + else if (! strcasecmp(op, "=~")) { + re = 1; + } + else if (! strcasecmp(op, "!~")) { + inv = 1; + re = 1; + } + else if (type == SDB_ATTRIBUTE) + return parse_attr_cmp(attr, op, value); + else + return NULL; + + if (value->type != SDB_TYPE_STRING) { + if (type == SDB_ATTRIBUTE) + return parse_attr_cmp(attr, op, value); + return NULL; + } + + if (! strcasecmp(attr, "name")) + m = sdb_store_name_matcher(type, value->data.string, re); + else if (type == SDB_ATTRIBUTE) + m = sdb_store_attr_matcher(attr, value->data.string, re); + + if (! m) + return NULL; + + 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) @@ -530,18 +793,50 @@ sdb_store_con_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) +sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj) { + if (obj->type != SDB_HOST) + return 0; + /* "NULL" always matches */ if ((! m) || (! obj)) - return 0; + return 1; if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers))) - return -1; + return 0; - return matchers[m->type](m, obj); + return matchers[m->type](m, HOST(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) +{ + lookup_iter_data_t data = { m, cb, user_data }; + + if (! cb) + return -1; + return sdb_store_iterate(lookup_iter, &data); +} /* sdb_store_lookup */ + /* vim: set tw=78 sw=4 ts=4 noexpandtab : */