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 { "is", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
61 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
62 { "matching", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
63 SEXP_FLAG_DO_EXPAND },
64 { "mid", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
65 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
66 { "mimetype", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
67 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
68 { "not", Xapian::Query::OP_AND_NOT, Xapian::Query::MatchAll,
70 { "of", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
71 SEXP_FLAG_DO_EXPAND },
72 { "or", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
74 { "path", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
75 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
76 { "property", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
77 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
78 { "regex", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
79 SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX },
80 { "rx", Xapian::Query::OP_INVALID, Xapian::Query::MatchAll,
81 SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX },
82 { "starts-with", Xapian::Query::OP_WILDCARD, Xapian::Query::MatchAll,
84 { "subject", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
85 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
86 { "tag", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
87 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
88 { "thread", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
89 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
90 { "to", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
91 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
95 static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch,
96 const _sexp_prefix_t *parent,
98 Xapian::Query &output);
100 static notmuch_status_t
101 _sexp_combine_query (notmuch_database_t *notmuch,
102 const _sexp_prefix_t *parent,
103 Xapian::Query::op operation,
106 Xapian::Query &output)
108 Xapian::Query subquery;
110 notmuch_status_t status;
112 /* if we run out elements, return accumulator */
116 return NOTMUCH_STATUS_SUCCESS;
119 status = _sexp_to_xapian_query (notmuch, parent, sx, subquery);
123 return _sexp_combine_query (notmuch,
126 Xapian::Query (operation, left, subquery),
130 static notmuch_status_t
131 _sexp_parse_phrase (std::string term_prefix, const char *phrase, Xapian::Query &output)
133 Xapian::Utf8Iterator p (phrase);
134 Xapian::Utf8Iterator end;
135 std::vector<std::string> terms;
138 Xapian::Utf8Iterator start;
139 while (p != end && ! Xapian::Unicode::is_wordchar (*p))
147 while (p != end && Xapian::Unicode::is_wordchar (*p))
151 std::string word (start, p);
152 word = Xapian::Unicode::tolower (word);
153 terms.push_back (term_prefix + word);
156 output = Xapian::Query (Xapian::Query::OP_PHRASE, terms.begin (), terms.end ());
157 return NOTMUCH_STATUS_SUCCESS;
160 static notmuch_status_t
161 _sexp_parse_wildcard (notmuch_database_t *notmuch,
162 const _sexp_prefix_t *parent,
164 Xapian::Query &output)
167 std::string term_prefix = parent ? _find_prefix (parent->name) : "";
169 if (parent && ! (parent->flags & SEXP_FLAG_WILDCARD)) {
170 _notmuch_database_log (notmuch, "'%s' does not support wildcard queries\n", parent->name);
171 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
174 output = Xapian::Query (Xapian::Query::OP_WILDCARD,
175 term_prefix + Xapian::Unicode::tolower (match));
176 return NOTMUCH_STATUS_SUCCESS;
179 static notmuch_status_t
180 _sexp_parse_one_term (notmuch_database_t *notmuch, std::string term_prefix, const sexp_t *sx,
181 Xapian::Query &output)
183 Xapian::Stem stem = *(notmuch->stemmer);
185 if (sx->aty == SEXP_BASIC && unicode_word_utf8 (sx->val)) {
186 std::string term = Xapian::Unicode::tolower (sx->val);
188 output = Xapian::Query ("Z" + term_prefix + stem (term));
189 return NOTMUCH_STATUS_SUCCESS;
191 return _sexp_parse_phrase (term_prefix, sx->val, output);
197 _sexp_parse_regex (notmuch_database_t *notmuch,
198 const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent,
199 std::string val, Xapian::Query &output)
202 _notmuch_database_log (notmuch, "illegal '%s' outside field\n",
204 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
207 if (! (parent->flags & SEXP_FLAG_REGEX)) {
208 _notmuch_database_log (notmuch, "'%s' not supported in field '%s'\n",
209 prefix->name, parent->name);
210 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
213 std::string msg; /* ignored */
215 return _notmuch_regexp_to_query (notmuch, Xapian::BAD_VALUENO, parent->name,
220 static notmuch_status_t
221 _sexp_expand_query (notmuch_database_t *notmuch,
222 const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent,
223 const sexp_t *sx, Xapian::Query &output)
225 Xapian::Query subquery;
226 notmuch_status_t status;
229 if (! (parent->flags & SEXP_FLAG_EXPAND)) {
230 _notmuch_database_log (notmuch, "'%s' unsupported inside '%s'\n", prefix->name, parent->name);
231 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
234 status = _sexp_combine_query (notmuch, NULL, prefix->xapian_op, prefix->initial, sx, subquery);
238 status = _notmuch_query_expand (notmuch, parent->name, subquery, output, msg);
240 _notmuch_database_log (notmuch, "error expanding query %s\n", msg.c_str ());
245 /* Here we expect the s-expression to be a proper list, with first
246 * element defining and operation, or as a special case the empty
249 static notmuch_status_t
250 _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent, const sexp_t *sx,
251 Xapian::Query &output)
253 if (sx->ty == SEXP_VALUE) {
254 std::string term_prefix = parent ? _find_prefix (parent->name) : "";
256 if (sx->aty == SEXP_BASIC && strcmp (sx->val, "*") == 0) {
257 return _sexp_parse_wildcard (notmuch, parent, "", output);
260 if (parent && (parent->flags & SEXP_FLAG_BOOLEAN)) {
261 output = Xapian::Query (term_prefix + sx->val);
262 return NOTMUCH_STATUS_SUCCESS;
265 return _sexp_parse_one_term (notmuch, term_prefix, sx, output);
267 Xapian::Query accumulator;
268 for (_sexp_prefix_t *prefix = prefixes; prefix->name; prefix++) {
269 if (prefix->flags & SEXP_FLAG_FIELD) {
270 notmuch_status_t status;
271 Xapian::Query subquery;
272 term_prefix = _find_prefix (prefix->name);
273 status = _sexp_parse_one_term (notmuch, term_prefix, sx, subquery);
276 accumulator = Xapian::Query (Xapian::Query::OP_OR, accumulator, subquery);
279 output = accumulator;
280 return NOTMUCH_STATUS_SUCCESS;
286 output = Xapian::Query::MatchAll;
287 return NOTMUCH_STATUS_SUCCESS;
290 if (sx->list->ty == SEXP_LIST) {
291 _notmuch_database_log (notmuch, "unexpected list in field/operation position\n",
293 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
296 for (_sexp_prefix_t *prefix = prefixes; prefix && prefix->name; prefix++) {
297 if (strcmp (prefix->name, sx->list->val) == 0) {
298 if (prefix->flags & SEXP_FLAG_FIELD) {
300 _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
301 prefix->name, parent->name);
302 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
307 if ((prefix->flags & SEXP_FLAG_SINGLE) &&
308 (! sx->list->next || sx->list->next->next || sx->list->next->ty != SEXP_VALUE)) {
309 _notmuch_database_log (notmuch, "'%s' expects single atom as argument\n",
311 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
314 if (prefix->xapian_op == Xapian::Query::OP_WILDCARD)
315 return _sexp_parse_wildcard (notmuch, parent, sx->list->next->val, output);
317 if (prefix->flags & SEXP_FLAG_DO_REGEX) {
318 return _sexp_parse_regex (notmuch, prefix, parent, sx->list->next->val, output);
321 if (prefix->flags & SEXP_FLAG_DO_EXPAND) {
322 return _sexp_expand_query (notmuch, prefix, parent, sx->list->next, output);
325 return _sexp_combine_query (notmuch, parent, prefix->xapian_op, prefix->initial,
326 sx->list->next, output);
330 _notmuch_database_log (notmuch, "unknown prefix '%s'\n", sx->list->val);
332 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
336 _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr,
337 Xapian::Query &output)
339 const sexp_t *sx = NULL;
340 char *buf = talloc_strdup (notmuch, querystr);
342 sx = parse_sexp (buf, strlen (querystr));
344 _notmuch_database_log (notmuch, "invalid s-expression: '%s'\n", querystr);
345 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
348 return _sexp_to_xapian_query (notmuch, NULL, sx, output);