1 #include "database-private.h"
5 #include "unicode-util.h"
7 /* _sexp is used for file scope symbols to avoid clashing with
8 * definitions from sexp.h */
12 SEXP_FLAG_FIELD = 1 << 0,
13 SEXP_FLAG_BOOLEAN = 1 << 1,
14 SEXP_FLAG_SINGLE = 1 << 2,
15 SEXP_FLAG_WILDCARD = 1 << 3,
16 SEXP_FLAG_REGEX = 1 << 4,
17 SEXP_FLAG_DO_REGEX = 1 << 5,
18 SEXP_FLAG_EXPAND = 1 << 6,
19 SEXP_FLAG_DO_EXPAND = 1 << 7,
20 SEXP_FLAG_ORPHAN = 1 << 8,
24 * define bitwise operators to hide casts */
27 operator| (_sexp_flag_t a, _sexp_flag_t b)
29 return static_cast<_sexp_flag_t>(
30 static_cast<unsigned>(a) | static_cast<unsigned>(b));
34 operator& (_sexp_flag_t a, _sexp_flag_t b)
36 return static_cast<_sexp_flag_t>(
37 static_cast<unsigned>(a) & static_cast<unsigned>(b));
42 Xapian::Query::op xapian_op;
43 Xapian::Query initial;
47 static _sexp_prefix_t prefixes[] =
49 { "and", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
51 { "attachment", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
52 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
53 { "body", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
55 { "from", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
56 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
57 { "folder", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
58 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
59 { "id", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
60 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
61 { "infix", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
62 SEXP_FLAG_SINGLE | SEXP_FLAG_ORPHAN },
63 { "is", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
64 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
65 { "matching", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
66 SEXP_FLAG_DO_EXPAND },
67 { "mid", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
68 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
69 { "mimetype", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
70 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
71 { "not", Xapian::Query::OP_AND_NOT, Xapian::Query::MatchAll,
73 { "of", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
74 SEXP_FLAG_DO_EXPAND },
75 { "or", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
77 { "path", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
78 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
79 { "property", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
80 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
81 { "query", Xapian::Query::OP_INVALID, Xapian::Query::MatchNothing,
82 SEXP_FLAG_SINGLE | SEXP_FLAG_ORPHAN },
83 { "regex", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
84 SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX },
85 { "rx", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
86 SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX },
87 { "starts-with", Xapian::Query::OP_WILDCARD, Xapian::Query::MatchAll,
89 { "subject", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
90 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
91 { "tag", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
92 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
93 { "thread", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
94 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
95 { "to", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
96 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
100 static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch,
101 const _sexp_prefix_t *parent,
103 Xapian::Query &output);
105 static notmuch_status_t
106 _sexp_combine_query (notmuch_database_t *notmuch,
107 const _sexp_prefix_t *parent,
108 Xapian::Query::op operation,
111 Xapian::Query &output)
113 Xapian::Query subquery;
115 notmuch_status_t status;
117 /* if we run out elements, return accumulator */
121 return NOTMUCH_STATUS_SUCCESS;
124 status = _sexp_to_xapian_query (notmuch, parent, sx, subquery);
128 return _sexp_combine_query (notmuch,
131 Xapian::Query (operation, left, subquery),
135 static notmuch_status_t
136 _sexp_parse_phrase (std::string term_prefix, const char *phrase, Xapian::Query &output)
138 Xapian::Utf8Iterator p (phrase);
139 Xapian::Utf8Iterator end;
140 std::vector<std::string> terms;
143 Xapian::Utf8Iterator start;
144 while (p != end && ! Xapian::Unicode::is_wordchar (*p))
152 while (p != end && Xapian::Unicode::is_wordchar (*p))
156 std::string word (start, p);
157 word = Xapian::Unicode::tolower (word);
158 terms.push_back (term_prefix + word);
161 output = Xapian::Query (Xapian::Query::OP_PHRASE, terms.begin (), terms.end ());
162 return NOTMUCH_STATUS_SUCCESS;
165 static notmuch_status_t
166 _sexp_parse_wildcard (notmuch_database_t *notmuch,
167 const _sexp_prefix_t *parent,
169 Xapian::Query &output)
172 std::string term_prefix = parent ? _notmuch_database_prefix (notmuch, parent->name) : "";
174 if (parent && ! (parent->flags & SEXP_FLAG_WILDCARD)) {
175 _notmuch_database_log (notmuch, "'%s' does not support wildcard queries\n", parent->name);
176 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
179 output = Xapian::Query (Xapian::Query::OP_WILDCARD,
180 term_prefix + Xapian::Unicode::tolower (match));
181 return NOTMUCH_STATUS_SUCCESS;
184 static notmuch_status_t
185 _sexp_parse_one_term (notmuch_database_t *notmuch, std::string term_prefix, const sexp_t *sx,
186 Xapian::Query &output)
188 Xapian::Stem stem = *(notmuch->stemmer);
190 if (sx->aty == SEXP_BASIC && unicode_word_utf8 (sx->val)) {
191 std::string term = Xapian::Unicode::tolower (sx->val);
193 output = Xapian::Query ("Z" + term_prefix + stem (term));
194 return NOTMUCH_STATUS_SUCCESS;
196 return _sexp_parse_phrase (term_prefix, sx->val, output);
202 _sexp_parse_regex (notmuch_database_t *notmuch,
203 const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent,
204 std::string val, Xapian::Query &output)
207 _notmuch_database_log (notmuch, "illegal '%s' outside field\n",
209 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
212 if (! (parent->flags & SEXP_FLAG_REGEX)) {
213 _notmuch_database_log (notmuch, "'%s' not supported in field '%s'\n",
214 prefix->name, parent->name);
215 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
218 std::string msg; /* ignored */
220 return _notmuch_regexp_to_query (notmuch, Xapian::BAD_VALUENO, parent->name,
225 static notmuch_status_t
226 _sexp_expand_query (notmuch_database_t *notmuch,
227 const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent,
228 const sexp_t *sx, Xapian::Query &output)
230 Xapian::Query subquery;
231 notmuch_status_t status;
234 if (! (parent->flags & SEXP_FLAG_EXPAND)) {
235 _notmuch_database_log (notmuch, "'%s' unsupported inside '%s'\n", prefix->name, parent->name);
236 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
239 status = _sexp_combine_query (notmuch, NULL, prefix->xapian_op, prefix->initial, sx, subquery);
243 status = _notmuch_query_expand (notmuch, parent->name, subquery, output, msg);
245 _notmuch_database_log (notmuch, "error expanding query %s\n", msg.c_str ());
250 static notmuch_status_t
251 _sexp_parse_infix (notmuch_database_t *notmuch, const sexp_t *sx, Xapian::Query &output)
254 output = notmuch->query_parser->parse_query (sx->val, NOTMUCH_QUERY_PARSER_FLAGS);
255 } catch (const Xapian::QueryParserError &error) {
256 _notmuch_database_log (notmuch, "Syntax error in infix query: %s\n", sx->val);
257 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
258 } catch (const Xapian::Error &error) {
259 if (! notmuch->exception_reported) {
260 _notmuch_database_log (notmuch,
261 "A Xapian exception occurred parsing query: %s\n",
262 error.get_msg ().c_str ());
263 _notmuch_database_log_append (notmuch,
264 "Query string was: %s\n",
266 notmuch->exception_reported = true;
267 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
270 return NOTMUCH_STATUS_SUCCESS;
273 static notmuch_status_t
274 _sexp_parse_header (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
275 const sexp_t *sx, Xapian::Query &output)
277 _sexp_prefix_t user_prefix;
279 user_prefix.name = sx->list->val;
280 user_prefix.flags = SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD;
283 _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
284 sx->list->val, parent->name);
285 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
288 parent = &user_prefix;
290 return _sexp_combine_query (notmuch, parent, Xapian::Query::OP_AND, Xapian::Query::MatchAll,
291 sx->list->next, output);
294 /* Here we expect the s-expression to be a proper list, with first
295 * element defining and operation, or as a special case the empty
298 static notmuch_status_t
299 _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent, const sexp_t *sx,
300 Xapian::Query &output)
302 if (sx->ty == SEXP_VALUE) {
303 std::string term_prefix = parent ? _notmuch_database_prefix (notmuch, parent->name) : "";
305 if (sx->aty == SEXP_BASIC && strcmp (sx->val, "*") == 0) {
306 return _sexp_parse_wildcard (notmuch, parent, "", output);
309 if (parent && (parent->flags & SEXP_FLAG_BOOLEAN)) {
310 output = Xapian::Query (term_prefix + sx->val);
311 return NOTMUCH_STATUS_SUCCESS;
315 return _sexp_parse_one_term (notmuch, term_prefix, sx, output);
317 Xapian::Query accumulator;
318 for (_sexp_prefix_t *prefix = prefixes; prefix->name; prefix++) {
319 if (prefix->flags & SEXP_FLAG_FIELD) {
320 notmuch_status_t status;
321 Xapian::Query subquery;
322 term_prefix = _notmuch_database_prefix (notmuch, prefix->name);
323 status = _sexp_parse_one_term (notmuch, term_prefix, sx, subquery);
326 accumulator = Xapian::Query (Xapian::Query::OP_OR, accumulator, subquery);
329 output = accumulator;
330 return NOTMUCH_STATUS_SUCCESS;
336 output = Xapian::Query::MatchAll;
337 return NOTMUCH_STATUS_SUCCESS;
340 if (sx->list->ty == SEXP_LIST) {
341 _notmuch_database_log (notmuch, "unexpected list in field/operation position\n",
343 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
346 /* Check for user defined field */
347 if (_notmuch_string_map_get (notmuch->user_prefix, sx->list->val)) {
348 return _sexp_parse_header (notmuch, parent, sx, output);
351 for (_sexp_prefix_t *prefix = prefixes; prefix && prefix->name; prefix++) {
352 if (strcmp (prefix->name, sx->list->val) == 0) {
353 if (prefix->flags & SEXP_FLAG_FIELD) {
355 _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
356 prefix->name, parent->name);
357 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
362 if (parent && (prefix->flags & SEXP_FLAG_ORPHAN)) {
363 _notmuch_database_log (notmuch, "'%s' not supported inside '%s'\n",
364 prefix->name, parent->name);
365 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
368 if ((prefix->flags & SEXP_FLAG_SINGLE) &&
369 (! sx->list->next || sx->list->next->next || sx->list->next->ty != SEXP_VALUE)) {
370 _notmuch_database_log (notmuch, "'%s' expects single atom as argument\n",
372 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
375 if (strcmp (prefix->name, "infix") == 0) {
376 return _sexp_parse_infix (notmuch, sx->list->next, output);
379 if (strcmp (prefix->name, "query") == 0) {
380 return _notmuch_query_name_to_query (notmuch, sx->list->next->val, output);
383 if (prefix->xapian_op == Xapian::Query::OP_WILDCARD)
384 return _sexp_parse_wildcard (notmuch, parent, sx->list->next->val, output);
386 if (prefix->flags & SEXP_FLAG_DO_REGEX) {
387 return _sexp_parse_regex (notmuch, prefix, parent, sx->list->next->val, output);
390 if (prefix->flags & SEXP_FLAG_DO_EXPAND) {
391 return _sexp_expand_query (notmuch, prefix, parent, sx->list->next, output);
394 return _sexp_combine_query (notmuch, parent, prefix->xapian_op, prefix->initial,
395 sx->list->next, output);
399 _notmuch_database_log (notmuch, "unknown prefix '%s'\n", sx->list->val);
400 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
404 _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr,
405 Xapian::Query &output)
407 const sexp_t *sx = NULL;
408 char *buf = talloc_strdup (notmuch, querystr);
410 sx = parse_sexp (buf, strlen (querystr));
412 _notmuch_database_log (notmuch, "invalid s-expression: '%s'\n", querystr);
413 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
416 return _sexp_to_xapian_query (notmuch, NULL, sx, output);