unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob 09c16ee892afc1716d50d4e5402e6b560520cb88 9295 bytes (raw)
name: lib/parse-sexp.cc 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
 
#include <xapian.h>
#include "notmuch-private.h"
#include "sexp.h"
#include "parse-time-vrp.h"
#include "query-fp.h"

typedef struct  {
    const char *name;
    Xapian::Query::op xapian_op;
    Xapian::Query initial;
} _sexp_op_t;

typedef enum {
    SEXP_FLAG_NONE	= 0,
    SEXP_FLAG_WILDCARD	= 1 << 0,
} _sexp_flag_t;

/*
 * define bitwise operators to hide casts */

inline _sexp_flag_t
operator| (_sexp_flag_t a, _sexp_flag_t b)
{
    return static_cast<_sexp_flag_t>(
	static_cast<unsigned>(a) | static_cast<unsigned>(b));
}

inline _sexp_flag_t
operator& (_sexp_flag_t a, _sexp_flag_t b)
{
    return static_cast<_sexp_flag_t>(
	static_cast<unsigned>(a) & static_cast<unsigned>(b));
}

typedef struct  {
    const char *name;
    Xapian::Query::op xapian_op;
    _sexp_flag_t flags_allowed;
} _sexp_field_t;

static _sexp_op_t operations[] =
{
    { "and",    Xapian::Query::OP_AND,          Xapian::Query::MatchAll },
    { "not",    Xapian::Query::OP_AND_NOT,      Xapian::Query::MatchAll },
    { "or",     Xapian::Query::OP_OR,           Xapian::Query::MatchNothing },
    { }
};

static _sexp_field_t fields[] =
{
    { "attachment",   Xapian::Query::OP_PHRASE,       SEXP_FLAG_WILDCARD },
    { "body",         Xapian::Query::OP_PHRASE,       SEXP_FLAG_NONE },
    { "date",         Xapian::Query::OP_INVALID,      SEXP_FLAG_NONE },
    { "from",         Xapian::Query::OP_PHRASE,       SEXP_FLAG_NONE },
    { "folder",       Xapian::Query::OP_OR,           SEXP_FLAG_NONE },
    { "id",           Xapian::Query::OP_OR,           SEXP_FLAG_NONE },
    { "is",           Xapian::Query::OP_AND,          SEXP_FLAG_WILDCARD },
    { "mid",          Xapian::Query::OP_OR,           SEXP_FLAG_NONE },
    { "mimetype",     Xapian::Query::OP_PHRASE,       SEXP_FLAG_NONE },
    { "path",         Xapian::Query::OP_OR,           SEXP_FLAG_NONE },
    { "property",     Xapian::Query::OP_AND,          SEXP_FLAG_WILDCARD },
    { "query",        Xapian::Query::OP_INVALID,      SEXP_FLAG_NONE },
    { "subject",      Xapian::Query::OP_PHRASE,       SEXP_FLAG_NONE },
    { "tag",          Xapian::Query::OP_AND,          SEXP_FLAG_WILDCARD },
    { "thread",       Xapian::Query::OP_OR,           SEXP_FLAG_NONE },
    { "to",           Xapian::Query::OP_PHRASE,       SEXP_FLAG_NONE },
    { }
};

typedef struct {
    const char *name;
    _sexp_flag_t flag;
} _sexp_keyword_t;

static _sexp_keyword_t keywords[] =
{
    { "any", SEXP_FLAG_WILDCARD },
    { "*", SEXP_FLAG_WILDCARD },
    { }
};

static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch, const sexp_t *sx,
					       Xapian::Query &output);

static notmuch_status_t
_sexp_combine_query (notmuch_database_t *notmuch,
		     Xapian::Query::op operation,
		     Xapian::Query left,
		     const sexp_t *sx,
		     Xapian::Query &output)
{
    Xapian::Query subquery;

    notmuch_status_t status;

    /* if we run out elements, return accumulator */

    if (! sx) {
	output = left;
	return NOTMUCH_STATUS_SUCCESS;
    }

    status = _sexp_to_xapian_query (notmuch, sx, subquery);
    if (status)
	return status;

    return _sexp_combine_query (notmuch,
				operation,
				Xapian::Query (operation, left, subquery),
				sx->next, output);
}

notmuch_status_t
_notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr,
				      Xapian::Query &output)
{
    const sexp_t *sx = NULL;
    char *buf = talloc_strdup (notmuch, querystr);

    sx = parse_sexp (buf, strlen (querystr));
    if (! sx)
	return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;

    return _sexp_to_xapian_query (notmuch, sx, output);
}

static void
_sexp_find_words (const char *str, std::string pref_str, std::vector<std::string> &terms)
{
    Xapian::Utf8Iterator p (str);
    Xapian::Utf8Iterator end;

    while (p != end) {
	Xapian::Utf8Iterator start;
	while (p != end && ! Xapian::Unicode::is_wordchar (*p))
	    p++;

	if (p == end)
	    break;

	start = p;

	while (p != end && Xapian::Unicode::is_wordchar (*p))
	    p++;

	if (p != start) {
	    std::string word (start, p);
	    word = Xapian::Unicode::tolower (word);
	    terms.push_back (pref_str + word);
	}
    }

}

static notmuch_status_t
_sexp_combine_field (const char *prefix,
		     Xapian::Query::op operation,
		     const sexp_t *sx,
		     Xapian::Query &output)
{
    std::vector<std::string> terms;

    for (const sexp_t *cur = sx; cur; cur = cur->next) {
	std::string pref_str = prefix;

	if (operation == Xapian::Query::OP_PHRASE) {
	    _sexp_find_words (cur->val, pref_str, terms);
	} else {
	    terms.push_back (pref_str + cur->val);
	}
    }
    output = Xapian::Query (operation, terms.begin (), terms.end ());
    return NOTMUCH_STATUS_SUCCESS;
}

static notmuch_status_t
_sexp_parse_date (notmuch_database_t *notmuch, const sexp_t *sx, Xapian::Query &output)
{
    std::string begin, end, msg;
    notmuch_status_t status;
    const sexp_t *cur = sx->list->next;

    if (cur) {
	begin = cur->val;
	cur = cur->next;
	if (cur) {
	    end = cur->val;
	    cur = cur->next;
	    if (cur) {
		_notmuch_database_log_append (notmuch, "extra argument(s) to date");
		return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
	    }
	    status = _notmuch_time_range_to_query (NOTMUCH_VALUE_TIMESTAMP, begin, end, msg, output);
	} else {
	    status = _notmuch_time_string_to_query (NOTMUCH_VALUE_TIMESTAMP, begin, msg, output);
	}
	if (status)
	    _notmuch_database_log_append (notmuch, "%s", msg.c_str ());
	return status;
    } else {
	_notmuch_database_log_append (notmuch, "missing argument(s) to date");
	return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
    }
}

static notmuch_status_t
_sexp_parse_keywords (notmuch_database_t *notmuch, const char *prefix, const sexp_t *sx,
		      _sexp_flag_t mask,
		      _sexp_flag_t &flags, const sexp_t *&rest)
{
    flags = SEXP_FLAG_NONE;

    for (; sx && sx->ty == SEXP_VALUE && sx->aty == SEXP_BASIC && sx->val[0] == ':'; sx = sx->next) {
	_sexp_keyword_t *keyword;
	for (keyword = keywords; keyword && keyword->name; keyword++) {
	    if (strcasecmp (keyword->name, sx->val + 1) == 0) {
		if ((mask & keyword->flag) == 0) {
		    _notmuch_database_log_append (notmuch,
						  "unsupported keyword '%s' for prefix '%s'\n",
						  sx->val, prefix);
		    return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
		} else {
		    flags = flags | keyword->flag;
		    break;
		}
	    }
	}
	if (! keyword || ! keyword->name) {
	    _notmuch_database_log (notmuch, "unknown keyword %s\n", sx->val);
	    return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
	}
    }

    rest = sx;
    return NOTMUCH_STATUS_SUCCESS;
}

/* Here we expect the s-expression to be a proper list, with first
 * element defining and operation, or as a special case the empty
 * list */

static notmuch_status_t
_sexp_to_xapian_query (notmuch_database_t *notmuch, const sexp_t *sx, Xapian::Query &output)
{

    const _sexp_op_t *op;

    if (sx->ty == SEXP_VALUE) {
	Xapian::Query accumulator;
	for (const _sexp_field_t *field = fields; field && field->name; field++) {
	    std::vector<std::string> terms;
	    if (field->xapian_op == Xapian::Query::OP_INVALID)
		continue;
	    _sexp_find_words (sx->val, _find_prefix (field->name), terms);
	    accumulator = Xapian::Query (Xapian::Query::OP_OR, accumulator,
					 Xapian::Query (Xapian::Query::OP_PHRASE,
							terms.begin (), terms.end ()));
	}
	output = accumulator;
	return NOTMUCH_STATUS_SUCCESS;
    }
    /* Empty list */
    if (! sx->list) {
	output = Xapian::Query::MatchAll;
	return NOTMUCH_STATUS_SUCCESS;
    }

    for (op = operations; op && op->name; op++) {
	if (strcasecmp (op->name, hd_sexp (sx)->val) == 0) {
	    return _sexp_combine_query (notmuch, op->xapian_op, op->initial, sx->list->next, output);
	}
    }

    for (const _sexp_field_t *field = fields; field && field->name; field++) {
	if (strcasecmp (field->name, hd_sexp (sx)->val) == 0) {
	    _sexp_flag_t flags;
	    const char *term_prefix;
	    const sexp_t *rest;

	    notmuch_status_t status = _sexp_parse_keywords (notmuch, sx->list->val,
							    sx->list->next, field->flags_allowed,
							    flags, rest);
	    if (status)
		return status;

	    /* some fields need to be handled specially */
	    if (strcasecmp (field->name, "date") == 0) {
		return _sexp_parse_date (notmuch, sx, output);
	    }
	    if (strcasecmp (field->name, "query") == 0) {
		if (! sx->list->next || ! sx->list->next->val) {
		    _notmuch_database_log (notmuch, "missing query name\n");
		    return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
		} else {
		    return _notmuch_query_name_to_query (notmuch, sx->list->next->val, output);
		}
	    }

	    term_prefix = _find_prefix (field->name);

	    if (flags & SEXP_FLAG_WILDCARD) {
		if (rest) {
		    _notmuch_database_log (notmuch, "extra term(s) after wildcard\n", sx->val);
		    return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
		}
		output = Xapian::Query (Xapian::Query::OP_WILDCARD, term_prefix);
		return NOTMUCH_STATUS_SUCCESS;
	    } else {
		return _sexp_combine_field (term_prefix, field->xapian_op, rest,
					    output);
	    }
	}
    }

    _notmuch_database_log_append (notmuch, "unimplemented prefix %s\n", sx->list->val);
    return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
}

debug log:

solving 95ee7c99 ...
found 95ee7c99 in https://yhetil.org/notmuch/20210718024021.3850340-17-david@tethera.net/
found 14420c0e in https://yhetil.org/notmuch/20210718024021.3850340-16-david@tethera.net/
found 1be2a4be in https://yhetil.org/notmuch/20210718024021.3850340-15-david@tethera.net/
found 29c5cd31 in https://yhetil.org/notmuch/20210718024021.3850340-13-david@tethera.net/
found c8bc3432 in https://yhetil.org/notmuch/20210718024021.3850340-11-david@tethera.net/
found 5865dc88 in https://yhetil.org/notmuch/20210718024021.3850340-10-david@tethera.net/
found fc6eb2d7 in https://yhetil.org/notmuch/20210718024021.3850340-9-david@tethera.net/
found 898cfdd0 in https://yhetil.org/notmuch/20210718024021.3850340-8-david@tethera.net/
found dfbebf2b in https://yhetil.org/notmuch/20210718024021.3850340-7-david@tethera.net/

applying [1/9] https://yhetil.org/notmuch/20210718024021.3850340-7-david@tethera.net/
diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc
new file mode 100644
index 00000000..dfbebf2b


applying [2/9] https://yhetil.org/notmuch/20210718024021.3850340-8-david@tethera.net/
diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc\r
index dfbebf2b..898cfdd0 100644\r


applying [3/9] https://yhetil.org/notmuch/20210718024021.3850340-9-david@tethera.net/
diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc\r
index 898cfdd0..fc6eb2d7 100644\r


applying [4/9] https://yhetil.org/notmuch/20210718024021.3850340-10-david@tethera.net/
diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc\r
index fc6eb2d7..5865dc88 100644\r


applying [5/9] https://yhetil.org/notmuch/20210718024021.3850340-11-david@tethera.net/
diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc\r
index 5865dc88..c8bc3432 100644\r


applying [6/9] https://yhetil.org/notmuch/20210718024021.3850340-13-david@tethera.net/
diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc\r
index c8bc3432..29c5cd31 100644\r


applying [7/9] https://yhetil.org/notmuch/20210718024021.3850340-15-david@tethera.net/
diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc
index 29c5cd31..1be2a4be 100644


applying [8/9] https://yhetil.org/notmuch/20210718024021.3850340-16-david@tethera.net/
diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc
index 1be2a4be..14420c0e 100644


applying [9/9] https://yhetil.org/notmuch/20210718024021.3850340-17-david@tethera.net/
diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc
index 14420c0e..95ee7c99 100644

Checking patch lib/parse-sexp.cc...
Applied patch lib/parse-sexp.cc cleanly.
Checking patch lib/parse-sexp.cc...
Applied patch lib/parse-sexp.cc cleanly.
Checking patch lib/parse-sexp.cc...
Applied patch lib/parse-sexp.cc cleanly.
Checking patch lib/parse-sexp.cc...
Applied patch lib/parse-sexp.cc cleanly.
Checking patch lib/parse-sexp.cc...
Applied patch lib/parse-sexp.cc cleanly.
Checking patch lib/parse-sexp.cc...
Applied patch lib/parse-sexp.cc cleanly.
Checking patch lib/parse-sexp.cc...
Applied patch lib/parse-sexp.cc cleanly.
Checking patch lib/parse-sexp.cc...
Applied patch lib/parse-sexp.cc cleanly.
Checking patch lib/parse-sexp.cc...
Applied patch lib/parse-sexp.cc cleanly.

index at:
100644 09c16ee892afc1716d50d4e5402e6b560520cb88	lib/parse-sexp.cc

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).