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,
36 SEXP_FLAG_PATHNAME = 1 << 10,
40 * define bitwise operators to hide casts */
43 operator| (_sexp_flag_t a, _sexp_flag_t b)
45 return static_cast<_sexp_flag_t>(
46 static_cast<unsigned>(a) | static_cast<unsigned>(b));
50 operator& (_sexp_flag_t a, _sexp_flag_t b)
52 return static_cast<_sexp_flag_t>(
53 static_cast<unsigned>(a) & static_cast<unsigned>(b));
58 Xapian::Query::op xapian_op;
59 Xapian::Query initial;
63 static _sexp_prefix_t prefixes[] =
65 { "and", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
67 { "attachment", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
68 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
69 { "body", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
71 { "date", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
73 { "from", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
74 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
75 { "folder", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
76 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND |
78 { "id", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
79 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
80 { "infix", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
81 SEXP_FLAG_SINGLE | SEXP_FLAG_ORPHAN },
82 { "is", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
83 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
84 { "lastmod", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
86 { "matching", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
87 SEXP_FLAG_DO_EXPAND },
88 { "mid", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
89 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
90 { "mimetype", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
91 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
92 { "not", Xapian::Query::OP_AND_NOT, Xapian::Query::MatchAll,
94 { "of", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
95 SEXP_FLAG_DO_EXPAND },
96 { "or", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
98 { "path", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
99 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX |
100 SEXP_FLAG_PATHNAME },
101 { "property", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
102 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
103 { "query", Xapian::Query::OP_INVALID, Xapian::Query::MatchNothing,
104 SEXP_FLAG_SINGLE | SEXP_FLAG_ORPHAN },
105 { "regex", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
106 SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX },
107 { "rx", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
108 SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX },
109 { "starts-with", Xapian::Query::OP_WILDCARD, Xapian::Query::MatchAll,
111 { "subject", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
112 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
113 { "tag", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
114 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
115 { "thread", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
116 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
117 { "to", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
118 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
122 static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch,
123 const _sexp_prefix_t *parent,
124 const _sexp_binding_t *env,
126 Xapian::Query &output);
128 static notmuch_status_t
129 _sexp_combine_query (notmuch_database_t *notmuch,
130 const _sexp_prefix_t *parent,
131 const _sexp_binding_t *env,
132 Xapian::Query::op operation,
135 Xapian::Query &output)
137 Xapian::Query subquery;
139 notmuch_status_t status;
141 /* if we run out elements, return accumulator */
145 return NOTMUCH_STATUS_SUCCESS;
148 status = _sexp_to_xapian_query (notmuch, parent, env, sx, subquery);
152 return _sexp_combine_query (notmuch,
156 Xapian::Query (operation, left, subquery),
160 static notmuch_status_t
161 _sexp_parse_phrase (std::string term_prefix, const char *phrase, Xapian::Query &output)
163 Xapian::Utf8Iterator p (phrase);
164 Xapian::Utf8Iterator end;
165 std::vector<std::string> terms;
168 Xapian::Utf8Iterator start;
169 while (p != end && ! Xapian::Unicode::is_wordchar (*p))
177 while (p != end && Xapian::Unicode::is_wordchar (*p))
181 std::string word (start, p);
182 word = Xapian::Unicode::tolower (word);
183 terms.push_back (term_prefix + word);
186 output = Xapian::Query (Xapian::Query::OP_PHRASE, terms.begin (), terms.end ());
187 return NOTMUCH_STATUS_SUCCESS;
190 static notmuch_status_t
191 resolve_binding (notmuch_database_t *notmuch, const _sexp_binding_t *env, const char *name,
192 const _sexp_binding_t **out)
194 for (; env; env = env->next) {
195 if (strcmp (name, env->name) == 0) {
197 return NOTMUCH_STATUS_SUCCESS;
201 _notmuch_database_log (notmuch, "undefined parameter '%s'\n", name);
202 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
205 static notmuch_status_t
206 _sexp_expand_term (notmuch_database_t *notmuch,
207 const _sexp_prefix_t *prefix,
208 const _sexp_binding_t *env,
212 notmuch_status_t status;
215 return NOTMUCH_STATUS_NULL_POINTER;
217 while (sx->ty == SEXP_VALUE && sx->aty == SEXP_BASIC && sx->val[0] == ',') {
218 const char *name = sx->val + 1;
219 const _sexp_binding_t *binding;
221 status = resolve_binding (notmuch, env, name, &binding);
226 env = binding->context;
229 if (sx->ty != SEXP_VALUE) {
230 _notmuch_database_log (notmuch, "'%s' expects single atom as argument\n",
232 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
236 return NOTMUCH_STATUS_SUCCESS;
239 static notmuch_status_t
240 _sexp_parse_wildcard (notmuch_database_t *notmuch,
241 const _sexp_prefix_t *parent,
242 unused(const _sexp_binding_t *env),
244 Xapian::Query &output)
247 std::string term_prefix = parent ? _notmuch_database_prefix (notmuch, parent->name) : "";
249 if (parent && ! (parent->flags & SEXP_FLAG_WILDCARD)) {
250 _notmuch_database_log (notmuch, "'%s' does not support wildcard queries\n", parent->name);
251 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
254 output = Xapian::Query (Xapian::Query::OP_WILDCARD,
255 term_prefix + Xapian::Unicode::tolower (match));
256 return NOTMUCH_STATUS_SUCCESS;
259 static notmuch_status_t
260 _sexp_parse_one_term (notmuch_database_t *notmuch, std::string term_prefix, const sexp_t *sx,
261 Xapian::Query &output)
263 Xapian::Stem stem = *(notmuch->stemmer);
265 if (sx->aty == SEXP_BASIC && unicode_word_utf8 (sx->val)) {
266 std::string term = Xapian::Unicode::tolower (sx->val);
268 output = Xapian::Query ("Z" + term_prefix + stem (term));
269 return NOTMUCH_STATUS_SUCCESS;
271 return _sexp_parse_phrase (term_prefix, sx->val, output);
277 _sexp_parse_regex (notmuch_database_t *notmuch,
278 const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent,
279 const _sexp_binding_t *env,
280 const sexp_t *term, Xapian::Query &output)
283 _notmuch_database_log (notmuch, "illegal '%s' outside field\n",
285 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
288 if (! (parent->flags & SEXP_FLAG_REGEX)) {
289 _notmuch_database_log (notmuch, "'%s' not supported in field '%s'\n",
290 prefix->name, parent->name);
291 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
294 std::string msg; /* ignored */
296 notmuch_status_t status;
298 status = _sexp_expand_term (notmuch, prefix, env, term, &str);
302 return _notmuch_regexp_to_query (notmuch, Xapian::BAD_VALUENO, parent->name,
307 static notmuch_status_t
308 _sexp_expand_query (notmuch_database_t *notmuch,
309 const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent,
310 unused(const _sexp_binding_t *env), const sexp_t *sx, Xapian::Query &output)
312 Xapian::Query subquery;
313 notmuch_status_t status;
316 if (! (parent->flags & SEXP_FLAG_EXPAND)) {
317 _notmuch_database_log (notmuch, "'%s' unsupported inside '%s'\n", prefix->name, parent->name);
318 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
321 status = _sexp_combine_query (notmuch, NULL, NULL, prefix->xapian_op, prefix->initial, sx,
326 status = _notmuch_query_expand (notmuch, parent->name, subquery, output, msg);
328 _notmuch_database_log (notmuch, "error expanding query %s\n", msg.c_str ());
333 static notmuch_status_t
334 _sexp_parse_infix (notmuch_database_t *notmuch, const sexp_t *sx, Xapian::Query &output)
337 output = notmuch->query_parser->parse_query (sx->val, NOTMUCH_QUERY_PARSER_FLAGS);
338 } catch (const Xapian::QueryParserError &error) {
339 _notmuch_database_log (notmuch, "Syntax error in infix query: %s\n", sx->val);
340 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
341 } catch (const Xapian::Error &error) {
342 if (! notmuch->exception_reported) {
343 _notmuch_database_log (notmuch,
344 "A Xapian exception occurred parsing query: %s\n",
345 error.get_msg ().c_str ());
346 _notmuch_database_log_append (notmuch,
347 "Query string was: %s\n",
349 notmuch->exception_reported = true;
350 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
353 return NOTMUCH_STATUS_SUCCESS;
356 static notmuch_status_t
357 _sexp_parse_header (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
358 const _sexp_binding_t *env, const sexp_t *sx, Xapian::Query &output)
360 _sexp_prefix_t user_prefix;
362 user_prefix.name = sx->list->val;
363 user_prefix.flags = SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD;
366 _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
367 sx->list->val, parent->name);
368 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
371 parent = &user_prefix;
373 return _sexp_combine_query (notmuch, parent, env, Xapian::Query::OP_AND, Xapian::Query::MatchAll,
374 sx->list->next, output);
377 static _sexp_binding_t *
378 _sexp_bind (void *ctx, const _sexp_binding_t *env, const char *name, const sexp_t *sx, const
379 _sexp_binding_t *context)
381 _sexp_binding_t *binding = talloc (ctx, _sexp_binding_t);
383 binding->name = talloc_strdup (ctx, name);
385 binding->context = context;
390 static notmuch_status_t
391 maybe_apply_macro (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
392 const _sexp_binding_t *env, const sexp_t *sx, const sexp_t *args,
393 Xapian::Query &output)
395 const sexp_t *params, *param, *arg, *body;
396 void *local = talloc_new (notmuch);
397 _sexp_binding_t *new_env = NULL;
398 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
400 if (sx->list->ty != SEXP_VALUE || strcmp (sx->list->val, "macro") != 0) {
401 status = NOTMUCH_STATUS_IGNORED;
405 params = sx->list->next;
407 if (! params || (params->ty != SEXP_LIST)) {
408 _notmuch_database_log (notmuch, "missing (possibly empty) list of arguments to macro\n");
409 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
415 _notmuch_database_log (notmuch, "missing body of macro\n");
416 status = NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
420 for (param = params->list, arg = args;
422 param = param->next, arg = arg->next) {
423 if (param->ty != SEXP_VALUE || param->aty != SEXP_BASIC) {
424 _notmuch_database_log (notmuch, "macro parameters must be unquoted atoms\n");
425 status = NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
428 new_env = _sexp_bind (local, new_env, param->val, arg, env);
431 if (param && ! arg) {
432 _notmuch_database_log (notmuch, "too few arguments to macro\n");
433 status = NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
437 if (! param && arg) {
438 _notmuch_database_log (notmuch, "too many arguments to macro\n");
439 status = NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
443 status = _sexp_to_xapian_query (notmuch, parent, new_env, body, output);
452 static notmuch_status_t
453 maybe_saved_squery (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
454 const _sexp_binding_t *env, const sexp_t *sx, Xapian::Query &output)
457 char *expansion = NULL;
458 notmuch_status_t status;
460 void *local = talloc_new (notmuch);
463 key = talloc_asprintf (local, "squery.%s", sx->list->val);
465 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
469 status = notmuch_database_get_config (notmuch, key, &expansion);
472 if (EMPTY_STRING (expansion)) {
473 status = NOTMUCH_STATUS_IGNORED;
477 buf = talloc_strdup (local, expansion);
478 /* XXX TODO: free this memory */
479 saved_sexp = parse_sexp (buf, strlen (expansion));
481 _notmuch_database_log (notmuch, "invalid saved s-expression query: '%s'\n", expansion);
482 status = NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
486 status = maybe_apply_macro (notmuch, parent, env, saved_sexp, sx->list->next, output);
487 if (status == NOTMUCH_STATUS_IGNORED)
488 status = _sexp_to_xapian_query (notmuch, parent, env, saved_sexp, output);
497 static notmuch_status_t
498 _sexp_expand_param (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
499 const _sexp_binding_t *env, const char *name,
500 Xapian::Query &output)
502 notmuch_status_t status;
504 const _sexp_binding_t *binding;
506 status = resolve_binding (notmuch, env, name, &binding);
510 return _sexp_to_xapian_query (notmuch, parent, binding->context, binding->sx,
514 static notmuch_status_t
515 _sexp_parse_range (notmuch_database_t *notmuch, const _sexp_prefix_t *prefix,
516 const sexp_t *sx, Xapian::Query &output)
518 const char *from, *to;
521 /* empty range matches everything */
523 output = Xapian::Query::MatchAll;
524 return NOTMUCH_STATUS_SUCCESS;
527 if (sx->ty == SEXP_LIST) {
528 _notmuch_database_log (notmuch, "expected atom as first argument of '%s'\n", prefix->name);
529 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
533 if (strcmp (from, "*") == 0)
539 if (sx->next->ty == SEXP_LIST) {
540 _notmuch_database_log (notmuch, "expected atom as second argument of '%s'\n",
542 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
545 if (sx->next->next) {
546 _notmuch_database_log (notmuch, "'%s' expects maximum of two arguments\n", prefix->name);
547 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
551 if (strcmp (to, "*") == 0)
555 if (strcmp (prefix->name, "date") == 0) {
556 notmuch_status_t status;
557 status = _notmuch_date_strings_to_query (NOTMUCH_VALUE_TIMESTAMP, from, to, output, msg);
560 _notmuch_database_log (notmuch, "%s\n", msg.c_str ());
565 if (strcmp (prefix->name, "lastmod") == 0) {
566 long from_idx, to_idx;
569 if (EMPTY_STRING (from))
572 from_idx = std::stol (from);
573 } catch (std::logic_error &e) {
574 _notmuch_database_log (notmuch, "bad 'from' revision: '%s'\n", from);
575 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
579 from_idx += notmuch_database_get_revision (notmuch, NULL);
582 if (EMPTY_STRING (to))
585 to_idx = std::stol (to);
586 } catch (std::logic_error &e) {
587 _notmuch_database_log (notmuch, "bad 'to' revision: '%s'\n", to);
588 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
592 to_idx += notmuch_database_get_revision (notmuch, NULL);
594 output = Xapian::Query (Xapian::Query::OP_VALUE_RANGE, NOTMUCH_VALUE_LAST_MOD,
595 Xapian::sortable_serialise (from_idx),
596 Xapian::sortable_serialise (to_idx));
597 return NOTMUCH_STATUS_SUCCESS;
600 _notmuch_database_log (notmuch, "unimplimented range prefix: '%s'\n", prefix->name);
601 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
604 /* Here we expect the s-expression to be a proper list, with first
605 * element defining and operation, or as a special case the empty
608 static notmuch_status_t
609 _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
610 const _sexp_binding_t *env, const sexp_t *sx, Xapian::Query &output)
612 notmuch_status_t status;
614 if (sx->ty == SEXP_VALUE && sx->aty == SEXP_BASIC && sx->val[0] == ',') {
615 return _sexp_expand_param (notmuch, parent, env, sx->val + 1, output);
618 if (sx->ty == SEXP_VALUE) {
619 std::string term_prefix = parent ? _notmuch_database_prefix (notmuch, parent->name) : "";
621 if (sx->aty == SEXP_BASIC && strcmp (sx->val, "*") == 0) {
622 return _sexp_parse_wildcard (notmuch, parent, env, "", output);
625 char *atom = sx->val;
627 if (parent && parent->flags & SEXP_FLAG_PATHNAME)
628 strip_trailing (atom, '/');
630 if (parent && (parent->flags & SEXP_FLAG_BOOLEAN)) {
631 output = Xapian::Query (term_prefix + atom);
632 return NOTMUCH_STATUS_SUCCESS;
636 return _sexp_parse_one_term (notmuch, term_prefix, sx, output);
638 Xapian::Query accumulator;
639 for (_sexp_prefix_t *prefix = prefixes; prefix->name; prefix++) {
640 if (prefix->flags & SEXP_FLAG_FIELD) {
641 Xapian::Query subquery;
642 term_prefix = _notmuch_database_prefix (notmuch, prefix->name);
643 status = _sexp_parse_one_term (notmuch, term_prefix, sx, subquery);
646 accumulator = Xapian::Query (Xapian::Query::OP_OR, accumulator, subquery);
649 output = accumulator;
650 return NOTMUCH_STATUS_SUCCESS;
656 output = Xapian::Query::MatchAll;
657 return NOTMUCH_STATUS_SUCCESS;
660 if (sx->list->ty == SEXP_LIST) {
661 _notmuch_database_log (notmuch, "unexpected list in field/operation position\n",
663 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
666 status = maybe_saved_squery (notmuch, parent, env, sx, output);
667 if (status != NOTMUCH_STATUS_IGNORED)
670 /* Check for user defined field */
671 if (_notmuch_string_map_get (notmuch->user_prefix, sx->list->val)) {
672 return _sexp_parse_header (notmuch, parent, env, sx, output);
675 if (strcmp (sx->list->val, "macro") == 0) {
676 _notmuch_database_log (notmuch, "macro definition not permitted here\n");
677 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
680 for (_sexp_prefix_t *prefix = prefixes; prefix && prefix->name; prefix++) {
681 if (strcmp (prefix->name, sx->list->val) == 0) {
682 if (prefix->flags & (SEXP_FLAG_FIELD | SEXP_FLAG_RANGE)) {
684 _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
685 prefix->name, parent->name);
686 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
691 if (parent && (prefix->flags & SEXP_FLAG_ORPHAN)) {
692 _notmuch_database_log (notmuch, "'%s' not supported inside '%s'\n",
693 prefix->name, parent->name);
694 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
697 if ((prefix->flags & SEXP_FLAG_SINGLE) &&
698 (! sx->list->next || sx->list->next->next || sx->list->next->ty != SEXP_VALUE)) {
699 _notmuch_database_log (notmuch, "'%s' expects single atom as argument\n",
701 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
704 if (prefix->flags & SEXP_FLAG_RANGE)
705 return _sexp_parse_range (notmuch, prefix, sx->list->next, output);
707 if (strcmp (prefix->name, "infix") == 0) {
708 return _sexp_parse_infix (notmuch, sx->list->next, output);
711 if (strcmp (prefix->name, "query") == 0) {
712 return _notmuch_query_name_to_query (notmuch, sx->list->next->val, output);
715 if (prefix->xapian_op == Xapian::Query::OP_WILDCARD) {
717 status = _sexp_expand_term (notmuch, prefix, env, sx->list->next, &str);
721 return _sexp_parse_wildcard (notmuch, parent, env, str, output);
724 if (prefix->flags & SEXP_FLAG_DO_REGEX) {
725 return _sexp_parse_regex (notmuch, prefix, parent, env, sx->list->next, output);
728 if (prefix->flags & SEXP_FLAG_DO_EXPAND) {
729 return _sexp_expand_query (notmuch, prefix, parent, env, sx->list->next, output);
732 return _sexp_combine_query (notmuch, parent, env, prefix->xapian_op, prefix->initial,
733 sx->list->next, output);
737 _notmuch_database_log (notmuch, "unknown prefix '%s'\n", sx->list->val);
738 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
742 _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr,
743 Xapian::Query &output)
745 const sexp_t *sx = NULL;
746 char *buf = talloc_strdup (notmuch, querystr);
748 sx = parse_sexp (buf, strlen (querystr));
750 _notmuch_database_log (notmuch, "invalid s-expression: '%s'\n", querystr);
751 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
754 return _sexp_to_xapian_query (notmuch, NULL, NULL, sx, output);