]> git.notmuchmail.org Git - notmuch/blob - lib/parse-sexp.cc
dbc3f89d903305969dccc2752582b97d3c45f74a
[notmuch] / lib / parse-sexp.cc
1 #include "database-private.h"
2
3 #if HAVE_SFSEXP
4 #include "sexp.h"
5 #include "unicode-util.h"
6
7 /* _sexp is used for file scope symbols to avoid clashing with
8  * definitions from sexp.h */
9
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
14  * a sexp.
15  */
16
17 typedef struct sexp_binding {
18     const char *name;
19     const sexp_t *sx;
20     const struct sexp_binding *context;
21     const struct sexp_binding *next;
22 } _sexp_binding_t;
23
24 typedef enum {
25     SEXP_FLAG_NONE      = 0,
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_t;
37
38 /*
39  * define bitwise operators to hide casts */
40
41 inline _sexp_flag_t
42 operator| (_sexp_flag_t a, _sexp_flag_t b)
43 {
44     return static_cast<_sexp_flag_t>(
45         static_cast<unsigned>(a) | static_cast<unsigned>(b));
46 }
47
48 inline _sexp_flag_t
49 operator& (_sexp_flag_t a, _sexp_flag_t b)
50 {
51     return static_cast<_sexp_flag_t>(
52         static_cast<unsigned>(a) & static_cast<unsigned>(b));
53 }
54
55 typedef struct  {
56     const char *name;
57     Xapian::Query::op xapian_op;
58     Xapian::Query initial;
59     _sexp_flag_t flags;
60 } _sexp_prefix_t;
61
62 static _sexp_prefix_t prefixes[] =
63 {
64     { "and",            Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
65       SEXP_FLAG_NONE },
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,
69       SEXP_FLAG_FIELD },
70     { "date",           Xapian::Query::OP_INVALID,      Xapian::Query::MatchAll,
71       SEXP_FLAG_RANGE },
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,
89       SEXP_FLAG_NONE },
90     { "of",             Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
91       SEXP_FLAG_DO_EXPAND },
92     { "or",             Xapian::Query::OP_OR,           Xapian::Query::MatchNothing,
93       SEXP_FLAG_NONE },
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,
105       SEXP_FLAG_SINGLE },
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 },
114     { }
115 };
116
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,
120                                                const sexp_t *sx,
121                                                Xapian::Query &output);
122
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,
128                      Xapian::Query left,
129                      const sexp_t *sx,
130                      Xapian::Query &output)
131 {
132     Xapian::Query subquery;
133
134     notmuch_status_t status;
135
136     /* if we run out elements, return accumulator */
137
138     if (! sx) {
139         output = left;
140         return NOTMUCH_STATUS_SUCCESS;
141     }
142
143     status = _sexp_to_xapian_query (notmuch, parent, env, sx, subquery);
144     if (status)
145         return status;
146
147     return _sexp_combine_query (notmuch,
148                                 parent,
149                                 env,
150                                 operation,
151                                 Xapian::Query (operation, left, subquery),
152                                 sx->next, output);
153 }
154
155 static notmuch_status_t
156 _sexp_parse_phrase (std::string term_prefix, const char *phrase, Xapian::Query &output)
157 {
158     Xapian::Utf8Iterator p (phrase);
159     Xapian::Utf8Iterator end;
160     std::vector<std::string> terms;
161
162     while (p != end) {
163         Xapian::Utf8Iterator start;
164         while (p != end && ! Xapian::Unicode::is_wordchar (*p))
165             p++;
166
167         if (p == end)
168             break;
169
170         start = p;
171
172         while (p != end && Xapian::Unicode::is_wordchar (*p))
173             p++;
174
175         if (p != start) {
176             std::string word (start, p);
177             word = Xapian::Unicode::tolower (word);
178             terms.push_back (term_prefix + word);
179         }
180     }
181     output = Xapian::Query (Xapian::Query::OP_PHRASE, terms.begin (), terms.end ());
182     return NOTMUCH_STATUS_SUCCESS;
183 }
184
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),
189                       std::string match,
190                       Xapian::Query &output)
191 {
192
193     std::string term_prefix = parent ? _notmuch_database_prefix (notmuch, parent->name) : "";
194
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;
198     }
199
200     output = Xapian::Query (Xapian::Query::OP_WILDCARD,
201                             term_prefix + Xapian::Unicode::tolower (match));
202     return NOTMUCH_STATUS_SUCCESS;
203 }
204
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)
208 {
209     Xapian::Stem stem = *(notmuch->stemmer);
210
211     if (sx->aty == SEXP_BASIC && unicode_word_utf8 (sx->val)) {
212         std::string term = Xapian::Unicode::tolower (sx->val);
213
214         output = Xapian::Query ("Z" + term_prefix + stem (term));
215         return NOTMUCH_STATUS_SUCCESS;
216     } else {
217         return _sexp_parse_phrase (term_prefix, sx->val, output);
218     }
219
220 }
221
222 notmuch_status_t
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)
227 {
228     if (! parent) {
229         _notmuch_database_log (notmuch, "illegal '%s' outside field\n",
230                                prefix->name);
231         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
232     }
233
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;
238     }
239
240     std::string msg; /* ignored */
241
242     return _notmuch_regexp_to_query (notmuch, Xapian::BAD_VALUENO, parent->name,
243                                      val, output, msg);
244 }
245
246
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)
251 {
252     Xapian::Query subquery;
253     notmuch_status_t status;
254     std::string msg;
255
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;
259     }
260
261     status = _sexp_combine_query (notmuch, NULL, NULL, prefix->xapian_op, prefix->initial, sx,
262                                   subquery);
263     if (status)
264         return status;
265
266     status = _notmuch_query_expand (notmuch, parent->name, subquery, output, msg);
267     if (status) {
268         _notmuch_database_log (notmuch, "error expanding query %s\n", msg.c_str ());
269     }
270     return status;
271 }
272
273 static notmuch_status_t
274 _sexp_parse_infix (notmuch_database_t *notmuch, const sexp_t *sx, Xapian::Query &output)
275 {
276     try {
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",
288                                           sx->val);
289             notmuch->exception_reported = true;
290             return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
291         }
292     }
293     return NOTMUCH_STATUS_SUCCESS;
294 }
295
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)
299 {
300     _sexp_prefix_t user_prefix;
301
302     user_prefix.name = sx->list->val;
303     user_prefix.flags = SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD;
304
305     if (parent) {
306         _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
307                                sx->list->val, parent->name);
308         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
309     }
310
311     parent = &user_prefix;
312
313     return _sexp_combine_query (notmuch, parent, env, Xapian::Query::OP_AND, Xapian::Query::MatchAll,
314                                 sx->list->next, output);
315 }
316
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)
320 {
321     _sexp_binding_t *binding = talloc (ctx, _sexp_binding_t);
322
323     binding->name = talloc_strdup (ctx, name);
324     binding->sx = sx;
325     binding->context = context;
326     binding->next = env;
327     return binding;
328 }
329
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)
334 {
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;
339
340     if (sx->list->ty != SEXP_VALUE || strcmp (sx->list->val, "macro") != 0) {
341         status = NOTMUCH_STATUS_IGNORED;
342         goto DONE;
343     }
344
345     params = sx->list->next;
346
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;
350     }
351
352     body = params->next;
353
354     if (! body) {
355         _notmuch_database_log (notmuch, "missing body of macro\n");
356         status = NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
357         goto DONE;
358     }
359
360     for (param = params->list, arg = args;
361          param && arg;
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;
366             goto DONE;
367         }
368         new_env = _sexp_bind (local, new_env, param->val, arg, env);
369     }
370
371     if (param && ! arg) {
372         _notmuch_database_log (notmuch, "too few arguments to macro\n");
373         status = NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
374         goto DONE;
375     }
376
377     if (! param && arg) {
378         _notmuch_database_log (notmuch, "too many arguments to macro\n");
379         status = NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
380         goto DONE;
381     }
382
383     status = _sexp_to_xapian_query (notmuch, parent, new_env, body, output);
384
385   DONE:
386     if (local)
387         talloc_free (local);
388
389     return status;
390 }
391
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)
395 {
396     char *key;
397     char *expansion = NULL;
398     notmuch_status_t status;
399     sexp_t *saved_sexp;
400     void *local = talloc_new (notmuch);
401     char *buf;
402
403     key = talloc_asprintf (local, "squery.%s", sx->list->val);
404     if (! key) {
405         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
406         goto DONE;
407     }
408
409     status = notmuch_database_get_config (notmuch, key, &expansion);
410     if (status)
411         goto DONE;
412     if (EMPTY_STRING (expansion)) {
413         status = NOTMUCH_STATUS_IGNORED;
414         goto DONE;
415     }
416
417     buf = talloc_strdup (local, expansion);
418     /* XXX TODO: free this memory */
419     saved_sexp = parse_sexp (buf, strlen (expansion));
420     if (! saved_sexp) {
421         _notmuch_database_log (notmuch, "invalid saved s-expression query: '%s'\n", expansion);
422         status = NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
423         goto DONE;
424     }
425
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);
429
430   DONE:
431     if (local)
432         talloc_free (local);
433
434     return status;
435 }
436
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)
441 {
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,
445                                           output);
446         }
447     }
448     _notmuch_database_log (notmuch, "undefined parameter %s\n", name);
449     return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
450 }
451
452 static notmuch_status_t
453 _sexp_parse_range (notmuch_database_t *notmuch,  const _sexp_prefix_t *prefix,
454                    const sexp_t *sx, Xapian::Query &output)
455 {
456     const char *from, *to;
457     std::string msg;
458
459     /* empty range matches everything */
460     if (! sx) {
461         output = Xapian::Query::MatchAll;
462         return NOTMUCH_STATUS_SUCCESS;
463     }
464
465     if (sx->ty == SEXP_LIST) {
466         _notmuch_database_log (notmuch, "expected atom as first argument of '%s'\n", prefix->name);
467         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
468     }
469
470     from = sx->val;
471     to = from;
472
473     if (sx->next) {
474         if (sx->next->ty == SEXP_LIST) {
475             _notmuch_database_log (notmuch, "expected atom as second argument of '%s'\n",
476                                    prefix->name);
477             return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
478         }
479
480         if (sx->next->next) {
481             _notmuch_database_log (notmuch, "'%s' expects maximum of two arguments\n", prefix->name);
482             return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
483         }
484
485         to = sx->next->val;
486     }
487
488     if (strcmp (prefix->name, "date") == 0) {
489         notmuch_status_t status;
490         status = _notmuch_date_strings_to_query (NOTMUCH_VALUE_TIMESTAMP, from, to, output, msg);
491         if (status) {
492             if (! msg.empty ())
493                 _notmuch_database_log (notmuch, "%s\n", msg.c_str ());
494         }
495         return status;
496     }
497
498     _notmuch_database_log (notmuch, "unimplimented range prefix: '%s'\n", prefix->name);
499     return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
500 }
501
502 /* Here we expect the s-expression to be a proper list, with first
503  * element defining and operation, or as a special case the empty
504  * list */
505
506 static notmuch_status_t
507 _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
508                        const _sexp_binding_t *env, const sexp_t *sx, Xapian::Query &output)
509 {
510     notmuch_status_t status;
511
512     if (sx->ty == SEXP_VALUE && sx->aty == SEXP_BASIC && sx->val[0] == ',') {
513         return _sexp_expand_param (notmuch, parent, env, sx->val + 1, output);
514     }
515
516     if (sx->ty == SEXP_VALUE) {
517         std::string term_prefix = parent ? _notmuch_database_prefix (notmuch, parent->name) : "";
518
519         if (sx->aty == SEXP_BASIC && strcmp (sx->val, "*") == 0) {
520             return _sexp_parse_wildcard (notmuch, parent, env, "", output);
521         }
522
523         if (parent && (parent->flags & SEXP_FLAG_BOOLEAN)) {
524             output = Xapian::Query (term_prefix + sx->val);
525             return NOTMUCH_STATUS_SUCCESS;
526         }
527
528         if (parent) {
529             return _sexp_parse_one_term (notmuch, term_prefix, sx, output);
530         } else {
531             Xapian::Query accumulator;
532             for (_sexp_prefix_t *prefix = prefixes; prefix->name; prefix++) {
533                 if (prefix->flags & SEXP_FLAG_FIELD) {
534                     Xapian::Query subquery;
535                     term_prefix = _notmuch_database_prefix (notmuch, prefix->name);
536                     status = _sexp_parse_one_term (notmuch, term_prefix, sx, subquery);
537                     if (status)
538                         return status;
539                     accumulator = Xapian::Query (Xapian::Query::OP_OR, accumulator, subquery);
540                 }
541             }
542             output = accumulator;
543             return NOTMUCH_STATUS_SUCCESS;
544         }
545     }
546
547     /* Empty list */
548     if (! sx->list) {
549         output = Xapian::Query::MatchAll;
550         return NOTMUCH_STATUS_SUCCESS;
551     }
552
553     if (sx->list->ty == SEXP_LIST) {
554         _notmuch_database_log (notmuch, "unexpected list in field/operation position\n",
555                                sx->list->val);
556         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
557     }
558
559     status = maybe_saved_squery (notmuch, parent, env, sx, output);
560     if (status != NOTMUCH_STATUS_IGNORED)
561         return status;
562
563     /* Check for user defined field */
564     if (_notmuch_string_map_get (notmuch->user_prefix, sx->list->val)) {
565         return _sexp_parse_header (notmuch, parent, env, sx, output);
566     }
567
568     if (strcmp (sx->list->val, "macro") == 0) {
569         _notmuch_database_log (notmuch, "macro definition not permitted here\n");
570         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
571     }
572
573     for (_sexp_prefix_t *prefix = prefixes; prefix && prefix->name; prefix++) {
574         if (strcmp (prefix->name, sx->list->val) == 0) {
575             if (prefix->flags & (SEXP_FLAG_FIELD | SEXP_FLAG_RANGE)) {
576                 if (parent) {
577                     _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
578                                            prefix->name, parent->name);
579                     return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
580                 }
581                 parent = prefix;
582             }
583
584             if (parent && (prefix->flags & SEXP_FLAG_ORPHAN)) {
585                 _notmuch_database_log (notmuch, "'%s' not supported inside '%s'\n",
586                                        prefix->name, parent->name);
587                 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
588             }
589
590             if ((prefix->flags & SEXP_FLAG_SINGLE) &&
591                 (! sx->list->next || sx->list->next->next || sx->list->next->ty != SEXP_VALUE)) {
592                 _notmuch_database_log (notmuch, "'%s' expects single atom as argument\n",
593                                        prefix->name);
594                 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
595             }
596
597             if (prefix->flags & SEXP_FLAG_RANGE)
598                 return _sexp_parse_range (notmuch, prefix, sx->list->next, output);
599
600             if (strcmp (prefix->name, "infix") == 0) {
601                 return _sexp_parse_infix (notmuch, sx->list->next, output);
602             }
603
604             if (strcmp (prefix->name, "query") == 0) {
605                 return _notmuch_query_name_to_query (notmuch, sx->list->next->val, output);
606             }
607
608             if (prefix->xapian_op == Xapian::Query::OP_WILDCARD)
609                 return _sexp_parse_wildcard (notmuch, parent, env, sx->list->next->val, output);
610
611             if (prefix->flags & SEXP_FLAG_DO_REGEX) {
612                 return _sexp_parse_regex (notmuch, prefix, parent, env, sx->list->next->val, output);
613             }
614
615             if (prefix->flags & SEXP_FLAG_DO_EXPAND) {
616                 return _sexp_expand_query (notmuch, prefix, parent, env, sx->list->next, output);
617             }
618
619             return _sexp_combine_query (notmuch, parent, env, prefix->xapian_op, prefix->initial,
620                                         sx->list->next, output);
621         }
622     }
623
624     _notmuch_database_log (notmuch, "unknown prefix '%s'\n", sx->list->val);
625     return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
626 }
627
628 notmuch_status_t
629 _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr,
630                                       Xapian::Query &output)
631 {
632     const sexp_t *sx = NULL;
633     char *buf = talloc_strdup (notmuch, querystr);
634
635     sx = parse_sexp (buf, strlen (querystr));
636     if (! sx) {
637         _notmuch_database_log (notmuch, "invalid s-expression: '%s'\n", querystr);
638         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
639     }
640
641     return _sexp_to_xapian_query (notmuch, NULL, NULL, sx, output);
642 }
643 #endif