+static notmuch_status_t
+resolve_binding (notmuch_database_t *notmuch, const _sexp_binding_t *env, const char *name,
+ const _sexp_binding_t **out)
+{
+ for (; env; env = env->next) {
+ if (strcmp (name, env->name) == 0) {
+ *out = env;
+ return NOTMUCH_STATUS_SUCCESS;
+ }
+ }
+
+ _notmuch_database_log (notmuch, "undefined parameter '%s'\n", name);
+ return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
+}
+
+static notmuch_status_t
+_sexp_expand_term (notmuch_database_t *notmuch,
+ const _sexp_prefix_t *prefix,
+ const _sexp_binding_t *env,
+ const sexp_t *sx,
+ const char **out)
+{
+ notmuch_status_t status;
+
+ if (! out)
+ return NOTMUCH_STATUS_NULL_POINTER;
+
+ while (sx->ty == SEXP_VALUE && sx->aty == SEXP_BASIC && sx->val[0] == ',') {
+ const char *name = sx->val + 1;
+ const _sexp_binding_t *binding;
+
+ status = resolve_binding (notmuch, env, name, &binding);
+ if (status)
+ return status;
+
+ sx = binding->sx;
+ env = binding->context;
+ }
+
+ if (sx->ty != SEXP_VALUE) {
+ _notmuch_database_log (notmuch, "'%s' expects single atom as argument\n",
+ prefix->name);
+ return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
+ }
+
+ *out = sx->val;
+ return NOTMUCH_STATUS_SUCCESS;
+}
+