#include #include "notmuch-private.h" #include "sexp.h" typedef struct { const char *name; Xapian::Query::op xapian_op; Xapian::Query initial; } _sexp_op_t; static _sexp_op_t operations[] = { { "and", Xapian::Query::OP_AND, Xapian::Query::MatchAll }, { "not", Xapian::Query::OP_AND_NOT, Xapian::Query::MatchAll }, { "or", Xapian::Query::OP_OR, Xapian::Query::MatchNothing }, { } }; typedef struct { const char *name; Xapian::Query::op xapian_op; } _sexp_field_t; static _sexp_field_t fields[] = { { "subject", Xapian::Query::OP_PHRASE }, { } }; static Xapian::Query _sexp_to_xapian_query (sexp_t *sx); static Xapian::Query _sexp_combine_query (Xapian::Query::op operation, Xapian::Query left, sexp_t *sx) { /* if we run out elements, return accumulator */ if (! sx) return left; return _sexp_combine_query (operation, Xapian::Query (operation, left, _sexp_to_xapian_query (sx)), sx->next); } Xapian::Query _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr) { sexp_t *sx = NULL; char *buf = talloc_strdup (notmuch, querystr); sx = parse_sexp (buf, strlen (querystr)); return _sexp_to_xapian_query (sx); } static Xapian::Query _sexp_combine_field (const char *prefix, Xapian::Query::op operation, sexp_t *sx) { std::vector terms; for (sexp_t *cur = sx; cur; cur = cur->next) { std::string pref_str = prefix; std::string word = cur->val; if (operation == Xapian::Query::OP_PHRASE) word = Xapian::Unicode::tolower (word); terms.push_back (pref_str + word); } return Xapian::Query (operation, terms.begin (), terms.end ()); } /* Here we expect the s-expression to be a proper list, with first * element defining and operation, or as a special case the empty * list */ static Xapian::Query _sexp_to_xapian_query (sexp_t *sx) { const _sexp_op_t *op; /* Currently we don't understand atoms */ assert (sx->ty == SEXP_LIST); /* Empty list */ if (! sx->list) return Xapian::Query::MatchAll; for (op = operations; op && op->name; op++) { if (strcasecmp (op->name, hd_sexp (sx)->val) == 0) return _sexp_combine_query (op->xapian_op, op->initial, sx->list->next); } for (const _sexp_field_t *field = fields; field && field->name; field++) { if (strcasecmp (field->name, hd_sexp (sx)->val) == 0) return _sexp_combine_field (_find_prefix (field->name), field->xapian_op, sx->list->next); } INTERNAL_ERROR ("unimplemented prefix %s\n", sx->list->val); }