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 */
17 SEXP_FLAG_FIELD = 1 << 0,
18 SEXP_FLAG_BOOLEAN = 1 << 1,
19 SEXP_FLAG_SINGLE = 1 << 2,
20 SEXP_FLAG_WILDCARD = 1 << 3,
21 SEXP_FLAG_REGEX = 1 << 4,
22 SEXP_FLAG_DO_REGEX = 1 << 5,
23 SEXP_FLAG_EXPAND = 1 << 6,
24 SEXP_FLAG_DO_EXPAND = 1 << 7,
25 SEXP_FLAG_ORPHAN = 1 << 8,
29 * define bitwise operators to hide casts */
32 operator| (_sexp_flag_t a, _sexp_flag_t b)
34 return static_cast<_sexp_flag_t>(
35 static_cast<unsigned>(a) | static_cast<unsigned>(b));
39 operator& (_sexp_flag_t a, _sexp_flag_t b)
41 return static_cast<_sexp_flag_t>(
42 static_cast<unsigned>(a) & static_cast<unsigned>(b));
47 Xapian::Query::op xapian_op;
48 Xapian::Query initial;
52 static _sexp_prefix_t prefixes[] =
54 { "and", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
56 { "attachment", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
57 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
58 { "body", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
60 { "from", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
61 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
62 { "folder", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
63 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
64 { "id", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
65 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
66 { "infix", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
67 SEXP_FLAG_SINGLE | SEXP_FLAG_ORPHAN },
68 { "is", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
69 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
70 { "matching", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
71 SEXP_FLAG_DO_EXPAND },
72 { "mid", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
73 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
74 { "mimetype", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
75 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
76 { "not", Xapian::Query::OP_AND_NOT, Xapian::Query::MatchAll,
78 { "of", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
79 SEXP_FLAG_DO_EXPAND },
80 { "or", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
82 { "path", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
83 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
84 { "property", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
85 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
86 { "query", Xapian::Query::OP_INVALID, Xapian::Query::MatchNothing,
87 SEXP_FLAG_SINGLE | SEXP_FLAG_ORPHAN },
88 { "regex", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
89 SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX },
90 { "rx", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
91 SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX },
92 { "starts-with", Xapian::Query::OP_WILDCARD, Xapian::Query::MatchAll,
94 { "subject", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
95 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
96 { "tag", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
97 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
98 { "thread", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
99 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
100 { "to", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
101 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
105 static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch,
106 const _sexp_prefix_t *parent,
107 const _sexp_binding_t *env,
109 Xapian::Query &output);
111 static notmuch_status_t
112 _sexp_combine_query (notmuch_database_t *notmuch,
113 const _sexp_prefix_t *parent,
114 const _sexp_binding_t *env,
115 Xapian::Query::op operation,
118 Xapian::Query &output)
120 Xapian::Query subquery;
122 notmuch_status_t status;
124 /* if we run out elements, return accumulator */
128 return NOTMUCH_STATUS_SUCCESS;
131 status = _sexp_to_xapian_query (notmuch, parent, env, sx, subquery);
135 return _sexp_combine_query (notmuch,
139 Xapian::Query (operation, left, subquery),
143 static notmuch_status_t
144 _sexp_parse_phrase (std::string term_prefix, const char *phrase, Xapian::Query &output)
146 Xapian::Utf8Iterator p (phrase);
147 Xapian::Utf8Iterator end;
148 std::vector<std::string> terms;
151 Xapian::Utf8Iterator start;
152 while (p != end && ! Xapian::Unicode::is_wordchar (*p))
160 while (p != end && Xapian::Unicode::is_wordchar (*p))
164 std::string word (start, p);
165 word = Xapian::Unicode::tolower (word);
166 terms.push_back (term_prefix + word);
169 output = Xapian::Query (Xapian::Query::OP_PHRASE, terms.begin (), terms.end ());
170 return NOTMUCH_STATUS_SUCCESS;
173 static notmuch_status_t
174 _sexp_parse_wildcard (notmuch_database_t *notmuch,
175 const _sexp_prefix_t *parent,
176 unused(const _sexp_binding_t *env),
178 Xapian::Query &output)
181 std::string term_prefix = parent ? _notmuch_database_prefix (notmuch, parent->name) : "";
183 if (parent && ! (parent->flags & SEXP_FLAG_WILDCARD)) {
184 _notmuch_database_log (notmuch, "'%s' does not support wildcard queries\n", parent->name);
185 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
188 output = Xapian::Query (Xapian::Query::OP_WILDCARD,
189 term_prefix + Xapian::Unicode::tolower (match));
190 return NOTMUCH_STATUS_SUCCESS;
193 static notmuch_status_t
194 _sexp_parse_one_term (notmuch_database_t *notmuch, std::string term_prefix, const sexp_t *sx,
195 Xapian::Query &output)
197 Xapian::Stem stem = *(notmuch->stemmer);
199 if (sx->aty == SEXP_BASIC && unicode_word_utf8 (sx->val)) {
200 std::string term = Xapian::Unicode::tolower (sx->val);
202 output = Xapian::Query ("Z" + term_prefix + stem (term));
203 return NOTMUCH_STATUS_SUCCESS;
205 return _sexp_parse_phrase (term_prefix, sx->val, output);
211 _sexp_parse_regex (notmuch_database_t *notmuch,
212 const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent,
213 unused(const _sexp_binding_t *env),
214 std::string val, Xapian::Query &output)
217 _notmuch_database_log (notmuch, "illegal '%s' outside field\n",
219 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
222 if (! (parent->flags & SEXP_FLAG_REGEX)) {
223 _notmuch_database_log (notmuch, "'%s' not supported in field '%s'\n",
224 prefix->name, parent->name);
225 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
228 std::string msg; /* ignored */
230 return _notmuch_regexp_to_query (notmuch, Xapian::BAD_VALUENO, parent->name,
235 static notmuch_status_t
236 _sexp_expand_query (notmuch_database_t *notmuch,
237 const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent,
238 unused(const _sexp_binding_t *env), const sexp_t *sx, Xapian::Query &output)
240 Xapian::Query subquery;
241 notmuch_status_t status;
244 if (! (parent->flags & SEXP_FLAG_EXPAND)) {
245 _notmuch_database_log (notmuch, "'%s' unsupported inside '%s'\n", prefix->name, parent->name);
246 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
249 status = _sexp_combine_query (notmuch, NULL, NULL, prefix->xapian_op, prefix->initial, sx,
254 status = _notmuch_query_expand (notmuch, parent->name, subquery, output, msg);
256 _notmuch_database_log (notmuch, "error expanding query %s\n", msg.c_str ());
261 static notmuch_status_t
262 _sexp_parse_infix (notmuch_database_t *notmuch, const sexp_t *sx, Xapian::Query &output)
265 output = notmuch->query_parser->parse_query (sx->val, NOTMUCH_QUERY_PARSER_FLAGS);
266 } catch (const Xapian::QueryParserError &error) {
267 _notmuch_database_log (notmuch, "Syntax error in infix query: %s\n", sx->val);
268 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
269 } catch (const Xapian::Error &error) {
270 if (! notmuch->exception_reported) {
271 _notmuch_database_log (notmuch,
272 "A Xapian exception occurred parsing query: %s\n",
273 error.get_msg ().c_str ());
274 _notmuch_database_log_append (notmuch,
275 "Query string was: %s\n",
277 notmuch->exception_reported = true;
278 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
281 return NOTMUCH_STATUS_SUCCESS;
284 static notmuch_status_t
285 _sexp_parse_header (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
286 const _sexp_binding_t *env, const sexp_t *sx, Xapian::Query &output)
288 _sexp_prefix_t user_prefix;
290 user_prefix.name = sx->list->val;
291 user_prefix.flags = SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD;
294 _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
295 sx->list->val, parent->name);
296 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
299 parent = &user_prefix;
301 return _sexp_combine_query (notmuch, parent, env, Xapian::Query::OP_AND, Xapian::Query::MatchAll,
302 sx->list->next, output);
305 static notmuch_status_t
306 maybe_saved_squery (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
307 const _sexp_binding_t *env, const sexp_t *sx, Xapian::Query &output)
310 char *expansion = NULL;
311 notmuch_status_t status;
313 void *local = talloc_new (notmuch);
316 key = talloc_asprintf (local, "squery.%s", sx->list->val);
318 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
322 status = notmuch_database_get_config (notmuch, key, &expansion);
325 if (EMPTY_STRING (expansion)) {
326 status = NOTMUCH_STATUS_IGNORED;
330 buf = talloc_strdup (local, expansion);
331 /* XXX TODO: free this memory */
332 saved_sexp = parse_sexp (buf, strlen (expansion));
334 _notmuch_database_log (notmuch, "invalid saved s-expression query: '%s'\n", expansion);
335 status = NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
339 status = _sexp_to_xapian_query (notmuch, parent, env, saved_sexp, output);
348 /* Here we expect the s-expression to be a proper list, with first
349 * element defining and operation, or as a special case the empty
352 static notmuch_status_t
353 _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
354 const _sexp_binding_t *env, const sexp_t *sx, Xapian::Query &output)
356 notmuch_status_t status;
358 if (sx->ty == SEXP_VALUE) {
359 std::string term_prefix = parent ? _notmuch_database_prefix (notmuch, parent->name) : "";
361 if (sx->aty == SEXP_BASIC && strcmp (sx->val, "*") == 0) {
362 return _sexp_parse_wildcard (notmuch, parent, env, "", output);
365 if (parent && (parent->flags & SEXP_FLAG_BOOLEAN)) {
366 output = Xapian::Query (term_prefix + sx->val);
367 return NOTMUCH_STATUS_SUCCESS;
371 return _sexp_parse_one_term (notmuch, term_prefix, sx, output);
373 Xapian::Query accumulator;
374 for (_sexp_prefix_t *prefix = prefixes; prefix->name; prefix++) {
375 if (prefix->flags & SEXP_FLAG_FIELD) {
376 Xapian::Query subquery;
377 term_prefix = _notmuch_database_prefix (notmuch, prefix->name);
378 status = _sexp_parse_one_term (notmuch, term_prefix, sx, subquery);
381 accumulator = Xapian::Query (Xapian::Query::OP_OR, accumulator, subquery);
384 output = accumulator;
385 return NOTMUCH_STATUS_SUCCESS;
391 output = Xapian::Query::MatchAll;
392 return NOTMUCH_STATUS_SUCCESS;
395 if (sx->list->ty == SEXP_LIST) {
396 _notmuch_database_log (notmuch, "unexpected list in field/operation position\n",
398 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
401 status = maybe_saved_squery (notmuch, parent, env, sx, output);
402 if (status != NOTMUCH_STATUS_IGNORED)
405 /* Check for user defined field */
406 if (_notmuch_string_map_get (notmuch->user_prefix, sx->list->val)) {
407 return _sexp_parse_header (notmuch, parent, env, sx, output);
410 for (_sexp_prefix_t *prefix = prefixes; prefix && prefix->name; prefix++) {
411 if (strcmp (prefix->name, sx->list->val) == 0) {
412 if (prefix->flags & SEXP_FLAG_FIELD) {
414 _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
415 prefix->name, parent->name);
416 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
421 if (parent && (prefix->flags & SEXP_FLAG_ORPHAN)) {
422 _notmuch_database_log (notmuch, "'%s' not supported inside '%s'\n",
423 prefix->name, parent->name);
424 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
427 if ((prefix->flags & SEXP_FLAG_SINGLE) &&
428 (! sx->list->next || sx->list->next->next || sx->list->next->ty != SEXP_VALUE)) {
429 _notmuch_database_log (notmuch, "'%s' expects single atom as argument\n",
431 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
434 if (strcmp (prefix->name, "infix") == 0) {
435 return _sexp_parse_infix (notmuch, sx->list->next, output);
438 if (strcmp (prefix->name, "query") == 0) {
439 return _notmuch_query_name_to_query (notmuch, sx->list->next->val, output);
442 if (prefix->xapian_op == Xapian::Query::OP_WILDCARD)
443 return _sexp_parse_wildcard (notmuch, parent, env, sx->list->next->val, output);
445 if (prefix->flags & SEXP_FLAG_DO_REGEX) {
446 return _sexp_parse_regex (notmuch, prefix, parent, env, sx->list->next->val, output);
449 if (prefix->flags & SEXP_FLAG_DO_EXPAND) {
450 return _sexp_expand_query (notmuch, prefix, parent, env, sx->list->next, output);
453 return _sexp_combine_query (notmuch, parent, env, prefix->xapian_op, prefix->initial,
454 sx->list->next, output);
458 _notmuch_database_log (notmuch, "unknown prefix '%s'\n", sx->list->val);
459 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
463 _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr,
464 Xapian::Query &output)
466 const sexp_t *sx = NULL;
467 char *buf = talloc_strdup (notmuch, querystr);
469 sx = parse_sexp (buf, strlen (querystr));
471 _notmuch_database_log (notmuch, "invalid s-expression: '%s'\n", querystr);
472 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
475 return _sexp_to_xapian_query (notmuch, NULL, NULL, sx, output);