#include #include "notmuch-private.h" #include "sexp.h" typedef struct { const char *name; Xapian::Query::op xapian_op; Xapian::Query initial; } _sexp_op_t; typedef enum { SEXP_FLAG_NONE = 0, SEXP_FLAG_WILDCARD = 1 << 0, } _sexp_flag_t; /* * define bitwise operators to hide casts */ inline _sexp_flag_t operator| (_sexp_flag_t a, _sexp_flag_t b) { return static_cast<_sexp_flag_t>( static_cast(a) | static_cast(b)); } inline _sexp_flag_t operator& (_sexp_flag_t a, _sexp_flag_t b) { return static_cast<_sexp_flag_t>( static_cast(a) & static_cast(b)); } typedef struct { const char *name; Xapian::Query::op xapian_op; _sexp_flag_t flags_allowed; } _sexp_field_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 }, { } }; static _sexp_field_t fields[] = { { "attachment", Xapian::Query::OP_PHRASE, SEXP_FLAG_WILDCARD }, { "body", Xapian::Query::OP_PHRASE, SEXP_FLAG_WILDCARD }, { "from", Xapian::Query::OP_PHRASE, SEXP_FLAG_NONE }, { "folder", Xapian::Query::OP_OR, SEXP_FLAG_NONE }, { "id", Xapian::Query::OP_OR, SEXP_FLAG_NONE }, { "is", Xapian::Query::OP_AND, SEXP_FLAG_WILDCARD }, { "mid", Xapian::Query::OP_OR, SEXP_FLAG_NONE }, { "mimetype", Xapian::Query::OP_PHRASE, SEXP_FLAG_NONE }, { "path", Xapian::Query::OP_OR, SEXP_FLAG_NONE }, { "property", Xapian::Query::OP_AND, SEXP_FLAG_WILDCARD }, { "subject", Xapian::Query::OP_PHRASE, SEXP_FLAG_NONE }, { "tag", Xapian::Query::OP_AND, SEXP_FLAG_WILDCARD }, { "thread", Xapian::Query::OP_OR, SEXP_FLAG_NONE }, { "to", Xapian::Query::OP_PHRASE, SEXP_FLAG_NONE }, { } }; typedef struct { const char *name; _sexp_flag_t flag; } _sexp_keyword_t; static _sexp_keyword_t keywords[] = { { "any", SEXP_FLAG_WILDCARD }, { "*", SEXP_FLAG_WILDCARD }, { } }; static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch, sexp_t *sx, Xapian::Query &output); static notmuch_status_t _sexp_combine_query (notmuch_database_t *notmuch, Xapian::Query::op operation, Xapian::Query left, sexp_t *sx, Xapian::Query &output) { Xapian::Query subquery; notmuch_status_t status; /* if we run out elements, return accumulator */ if (! sx) { output = left; return NOTMUCH_STATUS_SUCCESS; } status = _sexp_to_xapian_query (notmuch, sx, subquery); if (status) return status; return _sexp_combine_query (notmuch, operation, Xapian::Query (operation, left, subquery), sx->next, output); } notmuch_status_t _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr, Xapian::Query &output) { sexp_t *sx = NULL; char *buf = talloc_strdup (notmuch, querystr); sx = parse_sexp (buf, strlen (querystr)); if (!sx) return NOTMUCH_STATUS_ILLEGAL_ARGUMENT; return _sexp_to_xapian_query (notmuch, sx, output); } static notmuch_status_t _sexp_combine_field (const char *prefix, Xapian::Query::op operation, sexp_t *sx, Xapian::Query &output) { std::vector terms; for (sexp_t *cur = sx; cur; cur = cur->next) { std::string pref_str = prefix; if (operation == Xapian::Query::OP_PHRASE) { Xapian::Utf8Iterator p (cur->val); Xapian::Utf8Iterator end; while (p != end) { Xapian::Utf8Iterator start; while (p != end && ! Xapian::Unicode::is_wordchar (*p)) p++; if (p == end) break; start = p; while (p != end && Xapian::Unicode::is_wordchar (*p)) p++; if (p != start) { std::string word (start, p); word = Xapian::Unicode::tolower (word); terms.push_back (pref_str + word); } } } else { terms.push_back (pref_str + cur->val); } } output = Xapian::Query (operation, terms.begin (), terms.end ()); return NOTMUCH_STATUS_SUCCESS; } static notmuch_status_t _sexp_parse_keywords (notmuch_database_t *notmuch, const char *prefix, sexp_t *sx, _sexp_flag_t mask, _sexp_flag_t &flags, sexp_t *&rest) { flags = SEXP_FLAG_NONE; for (; sx && sx->ty == SEXP_VALUE && sx->aty == SEXP_BASIC && sx->val[0] == ':'; sx = sx->next) { _sexp_keyword_t *keyword; for (keyword = keywords; keyword && keyword->name; keyword++) { if (strcasecmp (keyword->name, sx->val+1) == 0) { if ((mask & keyword->flag) == 0) { _notmuch_database_log_append (notmuch, "unsupported keyword '%s' for prefix '%s'\n", sx->val, prefix); return NOTMUCH_STATUS_ILLEGAL_ARGUMENT; } else { flags = flags | keyword->flag; break; } } } if (! keyword || ! keyword->name) { _notmuch_database_log (notmuch, "unknown keyword %s\n", sx->val); return NOTMUCH_STATUS_ILLEGAL_ARGUMENT; } } rest = sx; return NOTMUCH_STATUS_SUCCESS; } /* 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 notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch, sexp_t *sx, Xapian::Query &output) { const _sexp_op_t *op; /* Currently we don't understand atoms */ assert (sx->ty == SEXP_LIST); /* Empty list */ if (! sx->list) { output = Xapian::Query::MatchAll; return NOTMUCH_STATUS_SUCCESS; } for (op = operations; op && op->name; op++) { if (strcasecmp (op->name, hd_sexp (sx)->val) == 0) { return _sexp_combine_query (notmuch, op->xapian_op, op->initial, sx->list->next, output); } } for (const _sexp_field_t *field = fields; field && field->name; field++) { if (strcasecmp (field->name, hd_sexp (sx)->val) == 0) { _sexp_flag_t flags; sexp_t *rest; const char *term_prefix; notmuch_status_t status = _sexp_parse_keywords (notmuch, sx->list->val, sx->list->next, field->flags_allowed, flags, rest); if (status) return status; term_prefix = _find_prefix (field->name); if (flags & SEXP_FLAG_WILDCARD) { output = Xapian::Query (Xapian::Query::OP_WILDCARD, term_prefix); return NOTMUCH_STATUS_SUCCESS; } else { return _sexp_combine_field (term_prefix, field->xapian_op, rest, output); } } } _notmuch_database_log_append (notmuch, "unimplemented prefix %s\n", sx->list->val); return NOTMUCH_STATUS_ILLEGAL_ARGUMENT; }