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,
18 SEXP_FLAG_EXPAND = 1 << 6,
19 SEXP_FLAG_DO_EXPAND = 1 << 7,
23 * define bitwise operators to hide casts */
26 operator| (_sexp_flag_t a, _sexp_flag_t b)
28 return static_cast<_sexp_flag_t>(
29 static_cast<unsigned>(a) | static_cast<unsigned>(b));
33 operator& (_sexp_flag_t a, _sexp_flag_t b)
35 return static_cast<_sexp_flag_t>(
36 static_cast<unsigned>(a) & static_cast<unsigned>(b));
41 Xapian::Query::op xapian_op;
42 Xapian::Query initial;
46 static _sexp_prefix_t prefixes[] =
48 { "and", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
50 { "attachment", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
51 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
52 { "body", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
54 { "from", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
55 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
56 { "folder", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
57 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
58 { "id", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
59 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
60 { "infix", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
62 { "is", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
63 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
64 { "matching", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
65 SEXP_FLAG_DO_EXPAND },
66 { "mid", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
67 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
68 { "mimetype", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
69 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
70 { "not", Xapian::Query::OP_AND_NOT, Xapian::Query::MatchAll,
72 { "of", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
73 SEXP_FLAG_DO_EXPAND },
74 { "or", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
76 { "path", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
77 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
78 { "property", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
79 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
80 { "regex", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
81 SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX },
82 { "rx", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
83 SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX },
84 { "starts-with", Xapian::Query::OP_WILDCARD, Xapian::Query::MatchAll,
86 { "subject", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
87 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
88 { "tag", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
89 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
90 { "thread", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
91 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
92 { "to", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
93 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
97 static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch,
98 const _sexp_prefix_t *parent,
100 Xapian::Query &output);
102 static notmuch_status_t
103 _sexp_combine_query (notmuch_database_t *notmuch,
104 const _sexp_prefix_t *parent,
105 Xapian::Query::op operation,
108 Xapian::Query &output)
110 Xapian::Query subquery;
112 notmuch_status_t status;
114 /* if we run out elements, return accumulator */
118 return NOTMUCH_STATUS_SUCCESS;
121 status = _sexp_to_xapian_query (notmuch, parent, sx, subquery);
125 return _sexp_combine_query (notmuch,
128 Xapian::Query (operation, left, subquery),
132 static notmuch_status_t
133 _sexp_parse_phrase (std::string term_prefix, const char *phrase, Xapian::Query &output)
135 Xapian::Utf8Iterator p (phrase);
136 Xapian::Utf8Iterator end;
137 std::vector<std::string> terms;
140 Xapian::Utf8Iterator start;
141 while (p != end && ! Xapian::Unicode::is_wordchar (*p))
149 while (p != end && Xapian::Unicode::is_wordchar (*p))
153 std::string word (start, p);
154 word = Xapian::Unicode::tolower (word);
155 terms.push_back (term_prefix + word);
158 output = Xapian::Query (Xapian::Query::OP_PHRASE, terms.begin (), terms.end ());
159 return NOTMUCH_STATUS_SUCCESS;
162 static notmuch_status_t
163 _sexp_parse_wildcard (notmuch_database_t *notmuch,
164 const _sexp_prefix_t *parent,
166 Xapian::Query &output)
169 std::string term_prefix = parent ? _notmuch_database_prefix (notmuch, parent->name) : "";
171 if (parent && ! (parent->flags & SEXP_FLAG_WILDCARD)) {
172 _notmuch_database_log (notmuch, "'%s' does not support wildcard queries\n", parent->name);
173 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
176 output = Xapian::Query (Xapian::Query::OP_WILDCARD,
177 term_prefix + Xapian::Unicode::tolower (match));
178 return NOTMUCH_STATUS_SUCCESS;
181 static notmuch_status_t
182 _sexp_parse_one_term (notmuch_database_t *notmuch, std::string term_prefix, const sexp_t *sx,
183 Xapian::Query &output)
185 Xapian::Stem stem = *(notmuch->stemmer);
187 if (sx->aty == SEXP_BASIC && unicode_word_utf8 (sx->val)) {
188 std::string term = Xapian::Unicode::tolower (sx->val);
190 output = Xapian::Query ("Z" + term_prefix + stem (term));
191 return NOTMUCH_STATUS_SUCCESS;
193 return _sexp_parse_phrase (term_prefix, sx->val, output);
199 _sexp_parse_regex (notmuch_database_t *notmuch,
200 const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent,
201 std::string val, Xapian::Query &output)
204 _notmuch_database_log (notmuch, "illegal '%s' outside field\n",
206 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
209 if (! (parent->flags & SEXP_FLAG_REGEX)) {
210 _notmuch_database_log (notmuch, "'%s' not supported in field '%s'\n",
211 prefix->name, parent->name);
212 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
215 std::string msg; /* ignored */
217 return _notmuch_regexp_to_query (notmuch, Xapian::BAD_VALUENO, parent->name,
222 static notmuch_status_t
223 _sexp_expand_query (notmuch_database_t *notmuch,
224 const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent,
225 const sexp_t *sx, Xapian::Query &output)
227 Xapian::Query subquery;
228 notmuch_status_t status;
231 if (! (parent->flags & SEXP_FLAG_EXPAND)) {
232 _notmuch_database_log (notmuch, "'%s' unsupported inside '%s'\n", prefix->name, parent->name);
233 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
236 status = _sexp_combine_query (notmuch, NULL, prefix->xapian_op, prefix->initial, sx, subquery);
240 status = _notmuch_query_expand (notmuch, parent->name, subquery, output, msg);
242 _notmuch_database_log (notmuch, "error expanding query %s\n", msg.c_str ());
247 static notmuch_status_t
248 _sexp_parse_infix (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
249 const sexp_t *sx, Xapian::Query &output)
252 _notmuch_database_log (notmuch, "'infix' not supported inside '%s'\n", parent->name);
253 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
256 output = notmuch->query_parser->parse_query (sx->val, NOTMUCH_QUERY_PARSER_FLAGS);
257 } catch (const Xapian::QueryParserError &error) {
258 _notmuch_database_log (notmuch, "Syntax error in infix query: %s\n", sx->val);
259 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
260 } catch (const Xapian::Error &error) {
261 if (! notmuch->exception_reported) {
262 _notmuch_database_log (notmuch,
263 "A Xapian exception occurred parsing query: %s\n",
264 error.get_msg ().c_str ());
265 _notmuch_database_log_append (notmuch,
266 "Query string was: %s\n",
268 notmuch->exception_reported = true;
269 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
272 return NOTMUCH_STATUS_SUCCESS;
275 static notmuch_status_t
276 _sexp_parse_header (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
277 const sexp_t *sx, Xapian::Query &output)
279 _sexp_prefix_t user_prefix;
281 user_prefix.name = sx->list->val;
282 user_prefix.flags = SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD;
285 _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
286 sx->list->val, parent->name);
287 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
290 parent = &user_prefix;
292 return _sexp_combine_query (notmuch, parent, Xapian::Query::OP_AND, Xapian::Query::MatchAll,
293 sx->list->next, output);
296 /* Here we expect the s-expression to be a proper list, with first
297 * element defining and operation, or as a special case the empty
300 static notmuch_status_t
301 _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent, const sexp_t *sx,
302 Xapian::Query &output)
304 if (sx->ty == SEXP_VALUE) {
305 std::string term_prefix = parent ? _notmuch_database_prefix (notmuch, parent->name) : "";
307 if (sx->aty == SEXP_BASIC && strcmp (sx->val, "*") == 0) {
308 return _sexp_parse_wildcard (notmuch, parent, "", output);
311 if (parent && (parent->flags & SEXP_FLAG_BOOLEAN)) {
312 output = Xapian::Query (term_prefix + sx->val);
313 return NOTMUCH_STATUS_SUCCESS;
317 return _sexp_parse_one_term (notmuch, term_prefix, sx, output);
319 Xapian::Query accumulator;
320 for (_sexp_prefix_t *prefix = prefixes; prefix->name; prefix++) {
321 if (prefix->flags & SEXP_FLAG_FIELD) {
322 notmuch_status_t status;
323 Xapian::Query subquery;
324 term_prefix = _notmuch_database_prefix (notmuch, prefix->name);
325 status = _sexp_parse_one_term (notmuch, term_prefix, sx, subquery);
328 accumulator = Xapian::Query (Xapian::Query::OP_OR, accumulator, subquery);
331 output = accumulator;
332 return NOTMUCH_STATUS_SUCCESS;
338 output = Xapian::Query::MatchAll;
339 return NOTMUCH_STATUS_SUCCESS;
342 if (sx->list->ty == SEXP_LIST) {
343 _notmuch_database_log (notmuch, "unexpected list in field/operation position\n",
345 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
348 /* Check for user defined field */
349 if (_notmuch_string_map_get (notmuch->user_prefix, sx->list->val)) {
350 return _sexp_parse_header (notmuch, parent, sx, output);
353 for (_sexp_prefix_t *prefix = prefixes; prefix && prefix->name; prefix++) {
354 if (strcmp (prefix->name, sx->list->val) == 0) {
355 if (prefix->flags & SEXP_FLAG_FIELD) {
357 _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
358 prefix->name, parent->name);
359 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
364 if ((prefix->flags & SEXP_FLAG_SINGLE) &&
365 (! sx->list->next || sx->list->next->next || sx->list->next->ty != SEXP_VALUE)) {
366 _notmuch_database_log (notmuch, "'%s' expects single atom as argument\n",
368 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
371 if (strcmp (prefix->name, "infix") == 0) {
372 return _sexp_parse_infix (notmuch, parent, sx->list->next, output);
375 if (prefix->xapian_op == Xapian::Query::OP_WILDCARD)
376 return _sexp_parse_wildcard (notmuch, parent, sx->list->next->val, output);
378 if (prefix->flags & SEXP_FLAG_DO_REGEX) {
379 return _sexp_parse_regex (notmuch, prefix, parent, sx->list->next->val, output);
382 if (prefix->flags & SEXP_FLAG_DO_EXPAND) {
383 return _sexp_expand_query (notmuch, prefix, parent, sx->list->next, output);
386 return _sexp_combine_query (notmuch, parent, prefix->xapian_op, prefix->initial,
387 sx->list->next, output);
391 _notmuch_database_log (notmuch, "unknown prefix '%s'\n", sx->list->val);
392 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
396 _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr,
397 Xapian::Query &output)
399 const sexp_t *sx = NULL;
400 char *buf = talloc_strdup (notmuch, querystr);
402 sx = parse_sexp (buf, strlen (querystr));
404 _notmuch_database_log (notmuch, "invalid s-expression: '%s'\n", querystr);
405 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
408 return _sexp_to_xapian_query (notmuch, NULL, sx, output);