X-Git-Url: https://git.notmuchmail.org/git?a=blobdiff_plain;ds=sidebyside;f=lib%2Fparse-sexp.cc;h=356c32ea254675749e4883743ba75f6fdd648121;hb=HEAD;hp=6282a4560983e40492d5b5625096a9b336df1feb;hpb=78632345868b5e4753ad402c806c29569946ce89;p=notmuch diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc index 6282a456..930888e9 100644 --- a/lib/parse-sexp.cc +++ b/lib/parse-sexp.cc @@ -3,6 +3,7 @@ #if HAVE_SFSEXP #include "sexp.h" #include "unicode-util.h" +#include "xapian-extra.h" /* _sexp is used for file scope symbols to avoid clashing with * definitions from sexp.h */ @@ -39,82 +40,99 @@ typedef enum { /* * define bitwise operators to hide casts */ -inline _sexp_flag_t +static 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 +static 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 enum { + SEXP_INITIAL_MATCH_ALL, + SEXP_INITIAL_MATCH_NOTHING, +} _sexp_initial_t; + +static inline Xapian::Query +_sexp_initial_query (_sexp_initial_t initial) +{ + switch (initial) { + case SEXP_INITIAL_MATCH_ALL: + return xapian_query_match_all (); + case SEXP_INITIAL_MATCH_NOTHING: + return Xapian::Query::MatchNothing; + } + INTERNAL_ERROR ("invalid initial sexp value %d", initial); +} + typedef struct { const char *name; Xapian::Query::op xapian_op; - Xapian::Query initial; + _sexp_initial_t initial; _sexp_flag_t flags; } _sexp_prefix_t; static _sexp_prefix_t prefixes[] = { - { "and", Xapian::Query::OP_AND, Xapian::Query::MatchAll, + { "and", Xapian::Query::OP_AND, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_NONE }, - { "attachment", Xapian::Query::OP_AND, Xapian::Query::MatchAll, + { "attachment", Xapian::Query::OP_AND, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND }, - { "body", Xapian::Query::OP_AND, Xapian::Query::MatchAll, + { "body", Xapian::Query::OP_AND, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_FIELD }, - { "date", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll, + { "date", Xapian::Query::OP_INVALID, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_RANGE }, - { "from", Xapian::Query::OP_AND, Xapian::Query::MatchAll, + { "from", Xapian::Query::OP_AND, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND }, - { "folder", Xapian::Query::OP_OR, Xapian::Query::MatchNothing, + { "folder", Xapian::Query::OP_OR, SEXP_INITIAL_MATCH_NOTHING, SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND | SEXP_FLAG_PATHNAME }, - { "id", Xapian::Query::OP_OR, Xapian::Query::MatchNothing, + { "id", Xapian::Query::OP_OR, SEXP_INITIAL_MATCH_NOTHING, SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX }, - { "infix", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll, + { "infix", Xapian::Query::OP_INVALID, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_SINGLE | SEXP_FLAG_ORPHAN }, - { "is", Xapian::Query::OP_AND, Xapian::Query::MatchAll, + { "is", Xapian::Query::OP_AND, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND }, - { "lastmod", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll, + { "lastmod", Xapian::Query::OP_INVALID, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_RANGE }, - { "matching", Xapian::Query::OP_AND, Xapian::Query::MatchAll, + { "matching", Xapian::Query::OP_AND, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_DO_EXPAND }, - { "mid", Xapian::Query::OP_OR, Xapian::Query::MatchNothing, + { "mid", Xapian::Query::OP_OR, SEXP_INITIAL_MATCH_NOTHING, SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX }, - { "mimetype", Xapian::Query::OP_AND, Xapian::Query::MatchAll, + { "mimetype", Xapian::Query::OP_AND, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND }, - { "not", Xapian::Query::OP_AND_NOT, Xapian::Query::MatchAll, + { "not", Xapian::Query::OP_AND_NOT, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_NONE }, - { "of", Xapian::Query::OP_AND, Xapian::Query::MatchAll, + { "of", Xapian::Query::OP_AND, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_DO_EXPAND }, - { "or", Xapian::Query::OP_OR, Xapian::Query::MatchNothing, + { "or", Xapian::Query::OP_OR, SEXP_INITIAL_MATCH_NOTHING, SEXP_FLAG_NONE }, - { "path", Xapian::Query::OP_OR, Xapian::Query::MatchNothing, + { "path", Xapian::Query::OP_OR, SEXP_INITIAL_MATCH_NOTHING, SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_PATHNAME }, - { "property", Xapian::Query::OP_AND, Xapian::Query::MatchAll, + { "property", Xapian::Query::OP_AND, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND }, - { "query", Xapian::Query::OP_INVALID, Xapian::Query::MatchNothing, + { "query", Xapian::Query::OP_INVALID, SEXP_INITIAL_MATCH_NOTHING, SEXP_FLAG_SINGLE | SEXP_FLAG_ORPHAN }, - { "regex", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll, + { "regex", Xapian::Query::OP_INVALID, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX }, - { "rx", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll, + { "rx", Xapian::Query::OP_INVALID, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX }, - { "starts-with", Xapian::Query::OP_WILDCARD, Xapian::Query::MatchAll, + { "starts-with", Xapian::Query::OP_WILDCARD, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_SINGLE }, - { "subject", Xapian::Query::OP_AND, Xapian::Query::MatchAll, + { "subject", Xapian::Query::OP_AND, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND }, - { "tag", Xapian::Query::OP_AND, Xapian::Query::MatchAll, + { "tag", Xapian::Query::OP_AND, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND }, - { "thread", Xapian::Query::OP_OR, Xapian::Query::MatchNothing, + { "thread", Xapian::Query::OP_OR, SEXP_INITIAL_MATCH_NOTHING, SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND }, - { "to", Xapian::Query::OP_AND, Xapian::Query::MatchAll, + { "to", Xapian::Query::OP_AND, SEXP_INITIAL_MATCH_ALL, SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND }, { } }; @@ -187,6 +205,55 @@ _sexp_parse_phrase (std::string term_prefix, const char *phrase, Xapian::Query & return NOTMUCH_STATUS_SUCCESS; } +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; +} + static notmuch_status_t _sexp_parse_wildcard (notmuch_database_t *notmuch, const _sexp_prefix_t *parent, @@ -227,8 +294,8 @@ _sexp_parse_one_term (notmuch_database_t *notmuch, std::string term_prefix, cons notmuch_status_t _sexp_parse_regex (notmuch_database_t *notmuch, const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent, - unused(const _sexp_binding_t *env), - std::string val, Xapian::Query &output) + const _sexp_binding_t *env, + const sexp_t *term, Xapian::Query &output) { if (! parent) { _notmuch_database_log (notmuch, "illegal '%s' outside field\n", @@ -243,9 +310,15 @@ _sexp_parse_regex (notmuch_database_t *notmuch, } std::string msg; /* ignored */ + const char *str; + notmuch_status_t status; + + status = _sexp_expand_term (notmuch, prefix, env, term, &str); + if (status) + return status; return _notmuch_regexp_to_query (notmuch, Xapian::BAD_VALUENO, parent->name, - val, output, msg); + str, output, msg); } @@ -263,7 +336,8 @@ _sexp_expand_query (notmuch_database_t *notmuch, return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; } - status = _sexp_combine_query (notmuch, NULL, NULL, prefix->xapian_op, prefix->initial, sx, + status = _sexp_combine_query (notmuch, NULL, NULL, prefix->xapian_op, + _sexp_initial_query (prefix->initial), sx, subquery); if (status) return status; @@ -315,7 +389,8 @@ _sexp_parse_header (notmuch_database_t *notmuch, const _sexp_prefix_t *parent, parent = &user_prefix; - return _sexp_combine_query (notmuch, parent, env, Xapian::Query::OP_AND, Xapian::Query::MatchAll, + return _sexp_combine_query (notmuch, parent, env, Xapian::Query::OP_AND, + xapian_query_match_all (), sx->list->next, output); } @@ -444,14 +519,16 @@ _sexp_expand_param (notmuch_database_t *notmuch, const _sexp_prefix_t *parent, const _sexp_binding_t *env, const char *name, Xapian::Query &output) { - for (; env; env = env->next) { - if (strcmp (name, env->name) == 0) { - return _sexp_to_xapian_query (notmuch, parent, env->context, env->sx, - output); - } - } - _notmuch_database_log (notmuch, "undefined parameter %s\n", name); - return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; + notmuch_status_t status; + + const _sexp_binding_t *binding; + + status = resolve_binding (notmuch, env, name, &binding); + if (status) + return status; + + return _sexp_to_xapian_query (notmuch, parent, binding->context, binding->sx, + output); } static notmuch_status_t @@ -463,7 +540,7 @@ _sexp_parse_range (notmuch_database_t *notmuch, const _sexp_prefix_t *prefix, /* empty range matches everything */ if (! sx) { - output = Xapian::Query::MatchAll; + output = xapian_query_match_all (); return NOTMUCH_STATUS_SUCCESS; } @@ -473,6 +550,9 @@ _sexp_parse_range (notmuch_database_t *notmuch, const _sexp_prefix_t *prefix, } from = sx->val; + if (strcmp (from, "*") == 0) + from = ""; + to = from; if (sx->next) { @@ -488,6 +568,8 @@ _sexp_parse_range (notmuch_database_t *notmuch, const _sexp_prefix_t *prefix, } to = sx->next->val; + if (strcmp (to, "*") == 0) + to = ""; } if (strcmp (prefix->name, "date") == 0) { @@ -501,32 +583,13 @@ _sexp_parse_range (notmuch_database_t *notmuch, const _sexp_prefix_t *prefix, } if (strcmp (prefix->name, "lastmod") == 0) { - long from_idx, to_idx; - - try { - if (EMPTY_STRING (from)) - from_idx = 0L; - else - from_idx = std::stol (from); - } catch (std::logic_error &e) { - _notmuch_database_log (notmuch, "bad 'from' revision: '%s'\n", from); - return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; - } - - try { - if (EMPTY_STRING (to)) - to_idx = LONG_MAX; - else - to_idx = std::stol (to); - } catch (std::logic_error &e) { - _notmuch_database_log (notmuch, "bad 'to' revision: '%s'\n", to); - return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; + notmuch_status_t status; + status = _notmuch_lastmod_strings_to_query (notmuch, from, to, output, msg); + if (status) { + if (! msg.empty ()) + _notmuch_database_log (notmuch, "%s\n", msg.c_str ()); } - - output = Xapian::Query (Xapian::Query::OP_VALUE_RANGE, NOTMUCH_VALUE_LAST_MOD, - Xapian::sortable_serialise (from_idx), - Xapian::sortable_serialise (to_idx)); - return NOTMUCH_STATUS_SUCCESS; + return status; } _notmuch_database_log (notmuch, "unimplimented range prefix: '%s'\n", prefix->name); @@ -585,7 +648,7 @@ _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent /* Empty list */ if (! sx->list) { - output = Xapian::Query::MatchAll; + output = xapian_query_match_all (); return NOTMUCH_STATUS_SUCCESS; } @@ -644,18 +707,25 @@ _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent return _notmuch_query_name_to_query (notmuch, sx->list->next->val, output); } - if (prefix->xapian_op == Xapian::Query::OP_WILDCARD) - return _sexp_parse_wildcard (notmuch, parent, env, sx->list->next->val, output); + if (prefix->xapian_op == Xapian::Query::OP_WILDCARD) { + const char *str; + status = _sexp_expand_term (notmuch, prefix, env, sx->list->next, &str); + if (status) + return status; + + return _sexp_parse_wildcard (notmuch, parent, env, str, output); + } if (prefix->flags & SEXP_FLAG_DO_REGEX) { - return _sexp_parse_regex (notmuch, prefix, parent, env, sx->list->next->val, output); + return _sexp_parse_regex (notmuch, prefix, parent, env, sx->list->next, output); } if (prefix->flags & SEXP_FLAG_DO_EXPAND) { return _sexp_expand_query (notmuch, prefix, parent, env, sx->list->next, output); } - return _sexp_combine_query (notmuch, parent, env, prefix->xapian_op, prefix->initial, + return _sexp_combine_query (notmuch, parent, env, prefix->xapian_op, + _sexp_initial_query (prefix->initial), sx->list->next, output); } }