+static struct cond_result *
+eval_cmp(const struct ec_parse *state, struct cond_result **in,
+ size_t in_len)
+{
+ struct cond_result *out = NULL;
+ struct ec_htable_elt_ref *iter;
+ bool eq = false, gt = false;
+
+ (void)state;
+
+ if (in_len != 3 || in[0]->type != STR || in[1]->type != in[2]->type) {
+ EC_LOG(LOG_ERR, "cmp() takes 3 arguments (str, <type>, <type>).\n");
+ errno = EINVAL;
+ goto fail;
+ }
+
+ if (strcmp(in[0]->str, "eq") && strcmp(in[0]->str, "ne") &&
+ strcmp(in[0]->str, "gt") && strcmp(in[0]->str, "lt") &&
+ strcmp(in[0]->str, "ge") && strcmp(in[0]->str, "le")) {
+ EC_LOG(LOG_ERR, "invalid comparison operator in cmp().\n");
+ errno = EINVAL;
+ goto fail;
+ }
+
+ if (strcmp(in[0]->str, "eq") && strcmp(in[0]->str, "ne") &&
+ in[1]->type != INT) {
+ EC_LOG(LOG_ERR, "cmp(gt|lt|ge|le, ...) is only allowed with integers.\n");
+ errno = EINVAL;
+ goto fail;
+ }
+
+ if (in[1]->type == INT) {
+ eq = in[1]->int64 == in[2]->int64;
+ gt = in[1]->int64 > in[2]->int64;
+ } else if (in[1]->type == NODESET &&
+ ec_htable_len(in[1]->htable) !=
+ ec_htable_len(in[2]->htable)) {
+ eq = false;
+ } else if (in[1]->type == NODESET) {
+ eq = true;
+ for (iter = ec_htable_iter(in[1]->htable);
+ iter != NULL; iter = ec_htable_iter_next(iter)) {
+ if (ec_htable_get(
+ in[2]->htable,
+ ec_htable_iter_get_key(iter),
+ sizeof(struct ec_parse *)) == NULL) {
+ eq = false;
+ break;
+ }
+ }
+ } else if (in[1]->type == STR) {
+ eq = !strcmp(in[1]->str, in[2]->str);;
+ } else if (in[1]->type == BOOLEAN) {
+ eq = in[1]->boolean == in[2]->boolean;
+ }
+
+ out = ec_malloc(sizeof(*out));
+ if (out == NULL)
+ goto fail;
+
+ out->type = BOOLEAN;
+ if (!strcmp(in[0]->str, "eq"))
+ out->boolean = eq;
+ else if (!strcmp(in[0]->str, "ne"))
+ out->boolean = !eq;
+ else if (!strcmp(in[0]->str, "lt"))
+ out->boolean = !gt && !eq;
+ else if (!strcmp(in[0]->str, "gt"))
+ out->boolean = gt && !eq;
+ else if (!strcmp(in[0]->str, "le"))
+ out->boolean = !gt || eq;
+ else if (!strcmp(in[0]->str, "ge"))
+ out->boolean = gt || eq;
+
+ cond_result_table_free(in, in_len);
+ return out;
+
+fail:
+ cond_result_free(out);
+ cond_result_table_free(in, in_len);
+ return NULL;
+}
+
+static struct cond_result *
+eval_count(const struct ec_parse *state, struct cond_result **in, size_t in_len)
+{
+ struct cond_result *out = NULL;
+
+ (void)state;
+
+ if (in_len != 1 || in[0]->type != NODESET) {
+ EC_LOG(LOG_ERR, "count() takes one argument of type nodeset.\n");
+ errno = EINVAL;
+ goto fail;
+ }
+
+ out = ec_malloc(sizeof(*out));
+ if (out == NULL)
+ goto fail;
+
+ out->type = INT;
+ out->int64 = ec_htable_len(in[0]->htable);
+
+ cond_result_table_free(in, in_len);
+ return out;
+
+fail:
+ cond_result_free(out);
+ cond_result_table_free(in, in_len);
+ return NULL;
+}
+