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 */
10 /* sexp_binding structs attach name to a sexp and a defining
11 * context. The latter allows lazy evaluation of parameters whose
12 * definition contains other parameters. Lazy evaluation is needed
13 * because a primary goal of macros is to change the parent field for
17 typedef struct sexp_binding {
20 const struct sexp_binding *context;
21 const struct sexp_binding *next;
26 SEXP_FLAG_FIELD = 1 << 0,
27 SEXP_FLAG_BOOLEAN = 1 << 1,
28 SEXP_FLAG_SINGLE = 1 << 2,
29 SEXP_FLAG_WILDCARD = 1 << 3,
30 SEXP_FLAG_REGEX = 1 << 4,
31 SEXP_FLAG_DO_REGEX = 1 << 5,
32 SEXP_FLAG_EXPAND = 1 << 6,
33 SEXP_FLAG_DO_EXPAND = 1 << 7,
34 SEXP_FLAG_ORPHAN = 1 << 8,
35 SEXP_FLAG_RANGE = 1 << 9,
39 * define bitwise operators to hide casts */
42 operator| (_sexp_flag_t a, _sexp_flag_t b)
44 return static_cast<_sexp_flag_t>(
45 static_cast<unsigned>(a) | static_cast<unsigned>(b));
49 operator& (_sexp_flag_t a, _sexp_flag_t b)
51 return static_cast<_sexp_flag_t>(
52 static_cast<unsigned>(a) & static_cast<unsigned>(b));
57 Xapian::Query::op xapian_op;
58 Xapian::Query initial;
62 static _sexp_prefix_t prefixes[] =
64 { "and", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
66 { "attachment", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
67 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
68 { "body", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
70 { "date", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
72 { "from", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
73 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
74 { "folder", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
75 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
76 { "id", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
77 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
78 { "infix", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
79 SEXP_FLAG_SINGLE | SEXP_FLAG_ORPHAN },
80 { "is", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
81 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
82 { "matching", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
83 SEXP_FLAG_DO_EXPAND },
84 { "mid", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
85 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
86 { "mimetype", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
87 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
88 { "not", Xapian::Query::OP_AND_NOT, Xapian::Query::MatchAll,
90 { "of", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
91 SEXP_FLAG_DO_EXPAND },
92 { "or", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
94 { "path", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
95 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
96 { "property", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
97 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
98 { "query", Xapian::Query::OP_INVALID, Xapian::Query::MatchNothing,
99 SEXP_FLAG_SINGLE | SEXP_FLAG_ORPHAN },
100 { "regex", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
101 SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX },
102 { "rx", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
103 SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX },
104 { "starts-with", Xapian::Query::OP_WILDCARD, Xapian::Query::MatchAll,
106 { "subject", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
107 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
108 { "tag", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
109 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
110 { "thread", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
111 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
112 { "to", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
113 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
117 static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch,
118 const _sexp_prefix_t *parent,
119 const _sexp_binding_t *env,
121 Xapian::Query &output);
123 static notmuch_status_t
124 _sexp_combine_query (notmuch_database_t *notmuch,
125 const _sexp_prefix_t *parent,
126 const _sexp_binding_t *env,
127 Xapian::Query::op operation,
130 Xapian::Query &output)
132 Xapian::Query subquery;
134 notmuch_status_t status;
136 /* if we run out elements, return accumulator */
140 return NOTMUCH_STATUS_SUCCESS;
143 status = _sexp_to_xapian_query (notmuch, parent, env, sx, subquery);
147 return _sexp_combine_query (notmuch,
151 Xapian::Query (operation, left, subquery),
155 static notmuch_status_t
156 _sexp_parse_phrase (std::string term_prefix, const char *phrase, Xapian::Query &output)
158 Xapian::Utf8Iterator p (phrase);
159 Xapian::Utf8Iterator end;
160 std::vector<std::string> terms;
163 Xapian::Utf8Iterator start;
164 while (p != end && ! Xapian::Unicode::is_wordchar (*p))
172 while (p != end && Xapian::Unicode::is_wordchar (*p))
176 std::string word (start, p);
177 word = Xapian::Unicode::tolower (word);
178 terms.push_back (term_prefix + word);
181 output = Xapian::Query (Xapian::Query::OP_PHRASE, terms.begin (), terms.end ());
182 return NOTMUCH_STATUS_SUCCESS;
185 static notmuch_status_t
186 _sexp_parse_wildcard (notmuch_database_t *notmuch,
187 const _sexp_prefix_t *parent,
188 unused(const _sexp_binding_t *env),
190 Xapian::Query &output)
193 std::string term_prefix = parent ? _notmuch_database_prefix (notmuch, parent->name) : "";
195 if (parent && ! (parent->flags & SEXP_FLAG_WILDCARD)) {
196 _notmuch_database_log (notmuch, "'%s' does not support wildcard queries\n", parent->name);
197 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
200 output = Xapian::Query (Xapian::Query::OP_WILDCARD,
201 term_prefix + Xapian::Unicode::tolower (match));
202 return NOTMUCH_STATUS_SUCCESS;
205 static notmuch_status_t
206 _sexp_parse_one_term (notmuch_database_t *notmuch, std::string term_prefix, const sexp_t *sx,
207 Xapian::Query &output)
209 Xapian::Stem stem = *(notmuch->stemmer);
211 if (sx->aty == SEXP_BASIC && unicode_word_utf8 (sx->val)) {
212 std::string term = Xapian::Unicode::tolower (sx->val);
214 output = Xapian::Query ("Z" + term_prefix + stem (term));
215 return NOTMUCH_STATUS_SUCCESS;
217 return _sexp_parse_phrase (term_prefix, sx->val, output);
223 _sexp_parse_regex (notmuch_database_t *notmuch,
224 const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent,
225 unused(const _sexp_binding_t *env),
226 std::string val, Xapian::Query &output)
229 _notmuch_database_log (notmuch, "illegal '%s' outside field\n",
231 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
234 if (! (parent->flags & SEXP_FLAG_REGEX)) {
235 _notmuch_database_log (notmuch, "'%s' not supported in field '%s'\n",
236 prefix->name, parent->name);
237 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
240 std::string msg; /* ignored */
242 return _notmuch_regexp_to_query (notmuch, Xapian::BAD_VALUENO, parent->name,
247 static notmuch_status_t
248 _sexp_expand_query (notmuch_database_t *notmuch,
249 const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent,
250 unused(const _sexp_binding_t *env), const sexp_t *sx, Xapian::Query &output)
252 Xapian::Query subquery;
253 notmuch_status_t status;
256 if (! (parent->flags & SEXP_FLAG_EXPAND)) {
257 _notmuch_database_log (notmuch, "'%s' unsupported inside '%s'\n", prefix->name, parent->name);
258 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
261 status = _sexp_combine_query (notmuch, NULL, NULL, prefix->xapian_op, prefix->initial, sx,
266 status = _notmuch_query_expand (notmuch, parent->name, subquery, output, msg);
268 _notmuch_database_log (notmuch, "error expanding query %s\n", msg.c_str ());
273 static notmuch_status_t
274 _sexp_parse_infix (notmuch_database_t *notmuch, const sexp_t *sx, Xapian::Query &output)
277 output = notmuch->query_parser->parse_query (sx->val, NOTMUCH_QUERY_PARSER_FLAGS);
278 } catch (const Xapian::QueryParserError &error) {
279 _notmuch_database_log (notmuch, "Syntax error in infix query: %s\n", sx->val);
280 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
281 } catch (const Xapian::Error &error) {
282 if (! notmuch->exception_reported) {
283 _notmuch_database_log (notmuch,
284 "A Xapian exception occurred parsing query: %s\n",
285 error.get_msg ().c_str ());
286 _notmuch_database_log_append (notmuch,
287 "Query string was: %s\n",
289 notmuch->exception_reported = true;
290 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
293 return NOTMUCH_STATUS_SUCCESS;
296 static notmuch_status_t
297 _sexp_parse_header (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
298 const _sexp_binding_t *env, const sexp_t *sx, Xapian::Query &output)
300 _sexp_prefix_t user_prefix;
302 user_prefix.name = sx->list->val;
303 user_prefix.flags = SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD;
306 _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
307 sx->list->val, parent->name);
308 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
311 parent = &user_prefix;
313 return _sexp_combine_query (notmuch, parent, env, Xapian::Query::OP_AND, Xapian::Query::MatchAll,
314 sx->list->next, output);
317 static _sexp_binding_t *
318 _sexp_bind (void *ctx, const _sexp_binding_t *env, const char *name, const sexp_t *sx, const
319 _sexp_binding_t *context)
321 _sexp_binding_t *binding = talloc (ctx, _sexp_binding_t);
323 binding->name = talloc_strdup (ctx, name);
325 binding->context = context;
330 static notmuch_status_t
331 maybe_apply_macro (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
332 const _sexp_binding_t *env, const sexp_t *sx, const sexp_t *args,
333 Xapian::Query &output)
335 const sexp_t *params, *param, *arg, *body;
336 void *local = talloc_new (notmuch);
337 _sexp_binding_t *new_env = NULL;
338 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
340 if (sx->list->ty != SEXP_VALUE || strcmp (sx->list->val, "macro") != 0) {
341 status = NOTMUCH_STATUS_IGNORED;
345 params = sx->list->next;
347 if (! params || (params->ty != SEXP_LIST)) {
348 _notmuch_database_log (notmuch, "missing (possibly empty) list of arguments to macro\n");
349 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
355 _notmuch_database_log (notmuch, "missing body of macro\n");
356 status = NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
360 for (param = params->list, arg = args;
362 param = param->next, arg = arg->next) {
363 if (param->ty != SEXP_VALUE || param->aty != SEXP_BASIC) {
364 _notmuch_database_log (notmuch, "macro parameters must be unquoted atoms\n");
365 status = NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
368 new_env = _sexp_bind (local, new_env, param->val, arg, env);
371 if (param && ! arg) {
372 _notmuch_database_log (notmuch, "too few arguments to macro\n");
373 status = NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
377 if (! param && arg) {
378 _notmuch_database_log (notmuch, "too many arguments to macro\n");
379 status = NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
383 status = _sexp_to_xapian_query (notmuch, parent, new_env, body, output);
392 static notmuch_status_t
393 maybe_saved_squery (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
394 const _sexp_binding_t *env, const sexp_t *sx, Xapian::Query &output)
397 char *expansion = NULL;
398 notmuch_status_t status;
400 void *local = talloc_new (notmuch);
403 key = talloc_asprintf (local, "squery.%s", sx->list->val);
405 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
409 status = notmuch_database_get_config (notmuch, key, &expansion);
412 if (EMPTY_STRING (expansion)) {
413 status = NOTMUCH_STATUS_IGNORED;
417 buf = talloc_strdup (local, expansion);
418 /* XXX TODO: free this memory */
419 saved_sexp = parse_sexp (buf, strlen (expansion));
421 _notmuch_database_log (notmuch, "invalid saved s-expression query: '%s'\n", expansion);
422 status = NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
426 status = maybe_apply_macro (notmuch, parent, env, saved_sexp, sx->list->next, output);
427 if (status == NOTMUCH_STATUS_IGNORED)
428 status = _sexp_to_xapian_query (notmuch, parent, env, saved_sexp, output);
437 static notmuch_status_t
438 _sexp_expand_param (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
439 const _sexp_binding_t *env, const char *name,
440 Xapian::Query &output)
442 for (; env; env = env->next) {
443 if (strcmp (name, env->name) == 0) {
444 return _sexp_to_xapian_query (notmuch, parent, env->context, env->sx,
448 _notmuch_database_log (notmuch, "undefined parameter %s\n", name);
449 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
452 static notmuch_status_t
453 _sexp_parse_date (notmuch_database_t *notmuch, const sexp_t *sx, Xapian::Query &output)
455 /* empty date matches everything */
457 output = Xapian::Query::MatchAll;
458 return NOTMUCH_STATUS_SUCCESS;
461 _notmuch_database_log (notmuch, "unimplemented date query\n");
462 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
465 /* Here we expect the s-expression to be a proper list, with first
466 * element defining and operation, or as a special case the empty
469 static notmuch_status_t
470 _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
471 const _sexp_binding_t *env, const sexp_t *sx, Xapian::Query &output)
473 notmuch_status_t status;
475 if (sx->ty == SEXP_VALUE && sx->aty == SEXP_BASIC && sx->val[0] == ',') {
476 return _sexp_expand_param (notmuch, parent, env, sx->val + 1, output);
479 if (sx->ty == SEXP_VALUE) {
480 std::string term_prefix = parent ? _notmuch_database_prefix (notmuch, parent->name) : "";
482 if (sx->aty == SEXP_BASIC && strcmp (sx->val, "*") == 0) {
483 return _sexp_parse_wildcard (notmuch, parent, env, "", output);
486 if (parent && (parent->flags & SEXP_FLAG_BOOLEAN)) {
487 output = Xapian::Query (term_prefix + sx->val);
488 return NOTMUCH_STATUS_SUCCESS;
492 return _sexp_parse_one_term (notmuch, term_prefix, sx, output);
494 Xapian::Query accumulator;
495 for (_sexp_prefix_t *prefix = prefixes; prefix->name; prefix++) {
496 if (prefix->flags & SEXP_FLAG_FIELD) {
497 Xapian::Query subquery;
498 term_prefix = _notmuch_database_prefix (notmuch, prefix->name);
499 status = _sexp_parse_one_term (notmuch, term_prefix, sx, subquery);
502 accumulator = Xapian::Query (Xapian::Query::OP_OR, accumulator, subquery);
505 output = accumulator;
506 return NOTMUCH_STATUS_SUCCESS;
512 output = Xapian::Query::MatchAll;
513 return NOTMUCH_STATUS_SUCCESS;
516 if (sx->list->ty == SEXP_LIST) {
517 _notmuch_database_log (notmuch, "unexpected list in field/operation position\n",
519 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
522 status = maybe_saved_squery (notmuch, parent, env, sx, output);
523 if (status != NOTMUCH_STATUS_IGNORED)
526 /* Check for user defined field */
527 if (_notmuch_string_map_get (notmuch->user_prefix, sx->list->val)) {
528 return _sexp_parse_header (notmuch, parent, env, sx, output);
531 if (strcmp (sx->list->val, "macro") == 0) {
532 _notmuch_database_log (notmuch, "macro definition not permitted here\n");
533 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
536 for (_sexp_prefix_t *prefix = prefixes; prefix && prefix->name; prefix++) {
537 if (strcmp (prefix->name, sx->list->val) == 0) {
538 if (prefix->flags & (SEXP_FLAG_FIELD | SEXP_FLAG_RANGE)) {
540 _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
541 prefix->name, parent->name);
542 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
547 if (parent && (prefix->flags & SEXP_FLAG_ORPHAN)) {
548 _notmuch_database_log (notmuch, "'%s' not supported inside '%s'\n",
549 prefix->name, parent->name);
550 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
553 if ((prefix->flags & SEXP_FLAG_SINGLE) &&
554 (! sx->list->next || sx->list->next->next || sx->list->next->ty != SEXP_VALUE)) {
555 _notmuch_database_log (notmuch, "'%s' expects single atom as argument\n",
557 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
560 if (strcmp (prefix->name, "date") == 0) {
561 return _sexp_parse_date (notmuch, sx->list->next, output);
564 if (strcmp (prefix->name, "infix") == 0) {
565 return _sexp_parse_infix (notmuch, sx->list->next, output);
568 if (strcmp (prefix->name, "query") == 0) {
569 return _notmuch_query_name_to_query (notmuch, sx->list->next->val, output);
572 if (prefix->xapian_op == Xapian::Query::OP_WILDCARD)
573 return _sexp_parse_wildcard (notmuch, parent, env, sx->list->next->val, output);
575 if (prefix->flags & SEXP_FLAG_DO_REGEX) {
576 return _sexp_parse_regex (notmuch, prefix, parent, env, sx->list->next->val, output);
579 if (prefix->flags & SEXP_FLAG_DO_EXPAND) {
580 return _sexp_expand_query (notmuch, prefix, parent, env, sx->list->next, output);
583 return _sexp_combine_query (notmuch, parent, env, prefix->xapian_op, prefix->initial,
584 sx->list->next, output);
588 _notmuch_database_log (notmuch, "unknown prefix '%s'\n", sx->list->val);
589 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
593 _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr,
594 Xapian::Query &output)
596 const sexp_t *sx = NULL;
597 char *buf = talloc_strdup (notmuch, querystr);
599 sx = parse_sexp (buf, strlen (querystr));
601 _notmuch_database_log (notmuch, "invalid s-expression: '%s'\n", querystr);
602 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
605 return _sexp_to_xapian_query (notmuch, NULL, NULL, sx, output);