]> git.cworth.org Git - notmuch/blob - lib/parse-sexp.cc
lib/parse-sexp: parse single terms and the empty list.
[notmuch] / lib / parse-sexp.cc
1 #include <xapian.h>
2 #include "notmuch-private.h"
3
4 #if HAVE_SFSEXP
5 #include "sexp.h"
6
7
8 /* _sexp is used for file scope symbols to avoid clashing with
9  * definitions from sexp.h */
10
11 /* Here we expect the s-expression to be a proper list, with first
12  * element defining and operation, or as a special case the empty
13  * list */
14
15 static notmuch_status_t
16 _sexp_to_xapian_query (notmuch_database_t *notmuch, const sexp_t *sx,
17                        Xapian::Query &output)
18 {
19
20     if (sx->ty == SEXP_VALUE) {
21         output = Xapian::Query (Xapian::Unicode::tolower (sx->val));
22         return NOTMUCH_STATUS_SUCCESS;
23     }
24
25     /* Empty list */
26     if (! sx->list) {
27         output = Xapian::Query::MatchAll;
28         return NOTMUCH_STATUS_SUCCESS;
29     }
30
31     if (sx->list->ty == SEXP_VALUE)
32         _notmuch_database_log (notmuch, "unknown prefix '%s'\n", sx->list->val);
33     else
34         _notmuch_database_log (notmuch, "unexpected list in field/operation position\n",
35                                sx->list->val);
36
37     return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
38 }
39
40 notmuch_status_t
41 _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr,
42                                       Xapian::Query &output)
43 {
44     const sexp_t *sx = NULL;
45     char *buf = talloc_strdup (notmuch, querystr);
46
47     sx = parse_sexp (buf, strlen (querystr));
48     if (! sx) {
49         _notmuch_database_log (notmuch, "invalid s-expression: '%s'\n", querystr);
50         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
51     }
52
53     return _sexp_to_xapian_query (notmuch, sx, output);
54 }
55 #endif