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 */
12 SEXP_FLAG_FIELD = 1 << 0,
13 SEXP_FLAG_BOOLEAN = 1 << 1,
14 SEXP_FLAG_SINGLE = 1 << 2,
15 SEXP_FLAG_WILDCARD = 1 << 3,
16 SEXP_FLAG_REGEX = 1 << 4,
17 SEXP_FLAG_DO_REGEX = 1 << 5,
21 * define bitwise operators to hide casts */
24 operator| (_sexp_flag_t a, _sexp_flag_t b)
26 return static_cast<_sexp_flag_t>(
27 static_cast<unsigned>(a) | static_cast<unsigned>(b));
31 operator& (_sexp_flag_t a, _sexp_flag_t b)
33 return static_cast<_sexp_flag_t>(
34 static_cast<unsigned>(a) & static_cast<unsigned>(b));
39 Xapian::Query::op xapian_op;
40 Xapian::Query initial;
44 static _sexp_prefix_t prefixes[] =
46 { "and", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
48 { "attachment", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
49 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD },
50 { "body", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
52 { "from", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
53 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
54 { "folder", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
55 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
56 { "id", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
57 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
58 { "is", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
59 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
60 { "mid", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
61 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
62 { "mimetype", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
63 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD },
64 { "not", Xapian::Query::OP_AND_NOT, Xapian::Query::MatchAll,
66 { "or", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
68 { "path", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
69 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
70 { "property", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
71 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
72 { "regex", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
73 SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX },
74 { "rx", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
75 SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX },
76 { "starts-with", Xapian::Query::OP_WILDCARD, Xapian::Query::MatchAll,
78 { "subject", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
79 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
80 { "tag", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
81 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
82 { "thread", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
83 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
84 { "to", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
85 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD },
89 static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch,
90 const _sexp_prefix_t *parent,
92 Xapian::Query &output);
94 static notmuch_status_t
95 _sexp_combine_query (notmuch_database_t *notmuch,
96 const _sexp_prefix_t *parent,
97 Xapian::Query::op operation,
100 Xapian::Query &output)
102 Xapian::Query subquery;
104 notmuch_status_t status;
106 /* if we run out elements, return accumulator */
110 return NOTMUCH_STATUS_SUCCESS;
113 status = _sexp_to_xapian_query (notmuch, parent, sx, subquery);
117 return _sexp_combine_query (notmuch,
120 Xapian::Query (operation, left, subquery),
124 static notmuch_status_t
125 _sexp_parse_phrase (std::string term_prefix, const char *phrase, Xapian::Query &output)
127 Xapian::Utf8Iterator p (phrase);
128 Xapian::Utf8Iterator end;
129 std::vector<std::string> terms;
132 Xapian::Utf8Iterator start;
133 while (p != end && ! Xapian::Unicode::is_wordchar (*p))
141 while (p != end && Xapian::Unicode::is_wordchar (*p))
145 std::string word (start, p);
146 word = Xapian::Unicode::tolower (word);
147 terms.push_back (term_prefix + word);
150 output = Xapian::Query (Xapian::Query::OP_PHRASE, terms.begin (), terms.end ());
151 return NOTMUCH_STATUS_SUCCESS;
154 static notmuch_status_t
155 _sexp_parse_wildcard (notmuch_database_t *notmuch,
156 const _sexp_prefix_t *parent,
158 Xapian::Query &output)
161 std::string term_prefix = parent ? _find_prefix (parent->name) : "";
163 if (parent && ! (parent->flags & SEXP_FLAG_WILDCARD)) {
164 _notmuch_database_log (notmuch, "'%s' does not support wildcard queries\n", parent->name);
165 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
168 output = Xapian::Query (Xapian::Query::OP_WILDCARD,
169 term_prefix + Xapian::Unicode::tolower (match));
170 return NOTMUCH_STATUS_SUCCESS;
173 static notmuch_status_t
174 _sexp_parse_one_term (notmuch_database_t *notmuch, std::string term_prefix, const sexp_t *sx,
175 Xapian::Query &output)
177 Xapian::Stem stem = *(notmuch->stemmer);
179 if (sx->aty == SEXP_BASIC && unicode_word_utf8 (sx->val)) {
180 std::string term = Xapian::Unicode::tolower (sx->val);
182 output = Xapian::Query ("Z" + term_prefix + stem (term));
183 return NOTMUCH_STATUS_SUCCESS;
185 return _sexp_parse_phrase (term_prefix, sx->val, output);
191 _sexp_parse_regex (notmuch_database_t *notmuch,
192 const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent,
193 std::string val, Xapian::Query &output)
196 _notmuch_database_log (notmuch, "illegal '%s' outside field\n",
198 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
201 if (! (parent->flags & SEXP_FLAG_REGEX)) {
202 _notmuch_database_log (notmuch, "'%s' not supported in field '%s'\n",
203 prefix->name, parent->name);
204 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
207 std::string msg; /* ignored */
209 return _notmuch_regexp_to_query (notmuch, Xapian::BAD_VALUENO, parent->name,
213 /* Here we expect the s-expression to be a proper list, with first
214 * element defining and operation, or as a special case the empty
217 static notmuch_status_t
218 _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent, const sexp_t *sx,
219 Xapian::Query &output)
221 if (sx->ty == SEXP_VALUE) {
222 std::string term = Xapian::Unicode::tolower (sx->val);
223 Xapian::Stem stem = *(notmuch->stemmer);
224 std::string term_prefix = parent ? _find_prefix (parent->name) : "";
226 if (sx->aty == SEXP_BASIC && strcmp (sx->val, "*") == 0) {
227 return _sexp_parse_wildcard (notmuch, parent, "", output);
230 if (parent && (parent->flags & SEXP_FLAG_BOOLEAN)) {
231 output = Xapian::Query (term_prefix + sx->val);
232 return NOTMUCH_STATUS_SUCCESS;
235 return _sexp_parse_one_term (notmuch, term_prefix, sx, output);
237 Xapian::Query accumulator;
238 for (_sexp_prefix_t *prefix = prefixes; prefix->name; prefix++) {
239 if (prefix->flags & SEXP_FLAG_FIELD) {
240 notmuch_status_t status;
241 Xapian::Query subquery;
242 term_prefix = _find_prefix (prefix->name);
243 status = _sexp_parse_one_term (notmuch, term_prefix, sx, subquery);
246 accumulator = Xapian::Query (Xapian::Query::OP_OR, accumulator, subquery);
249 output = accumulator;
250 return NOTMUCH_STATUS_SUCCESS;
256 output = Xapian::Query::MatchAll;
257 return NOTMUCH_STATUS_SUCCESS;
260 if (sx->list->ty == SEXP_LIST) {
261 _notmuch_database_log (notmuch, "unexpected list in field/operation position\n",
263 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
266 for (_sexp_prefix_t *prefix = prefixes; prefix && prefix->name; prefix++) {
267 if (strcmp (prefix->name, sx->list->val) == 0) {
268 if (prefix->flags & SEXP_FLAG_FIELD) {
270 _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
271 prefix->name, parent->name);
272 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
277 if ((prefix->flags & SEXP_FLAG_SINGLE) &&
278 (! sx->list->next || sx->list->next->next || sx->list->next->ty != SEXP_VALUE)) {
279 _notmuch_database_log (notmuch, "'%s' expects single atom as argument\n",
281 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
284 if (prefix->xapian_op == Xapian::Query::OP_WILDCARD)
285 return _sexp_parse_wildcard (notmuch, parent, sx->list->next->val, output);
287 if (prefix->flags & SEXP_FLAG_DO_REGEX) {
288 return _sexp_parse_regex (notmuch, prefix, parent, sx->list->next->val, output);
291 return _sexp_combine_query (notmuch, parent, prefix->xapian_op, prefix->initial,
292 sx->list->next, output);
296 _notmuch_database_log (notmuch, "unknown prefix '%s'\n", sx->list->val);
298 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
302 _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr,
303 Xapian::Query &output)
305 const sexp_t *sx = NULL;
306 char *buf = talloc_strdup (notmuch, querystr);
308 sx = parse_sexp (buf, strlen (querystr));
310 _notmuch_database_log (notmuch, "invalid s-expression: '%s'\n", querystr);
311 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
314 return _sexp_to_xapian_query (notmuch, NULL, sx, output);