Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 7C826431FB6 for ; Sat, 29 Dec 2012 08:51:02 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -1.098 X-Spam-Level: X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5 tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001, NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id kZ6ZGevh3uL8 for ; Sat, 29 Dec 2012 08:50:58 -0800 (PST) Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 2A211431FAF for ; Sat, 29 Dec 2012 08:50:58 -0800 (PST) Received: from smtp.qmul.ac.uk ([138.37.6.40]) by mail2.qmul.ac.uk with esmtp (Exim 4.71) (envelope-from ) id 1TozcY-0000bk-68; Sat, 29 Dec 2012 16:50:52 +0000 Received: from 94.197.9.104.threembb.co.uk ([94.197.9.104] helo=localhost) by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.69) (envelope-from ) id 1TozcW-0000mF-TG; Sat, 29 Dec 2012 16:50:49 +0000 From: Mark Walters To: Austin Clements , notmuch@notmuchmail.org Subject: Re: [PATCH v3 2/5] util: Function to parse boolean term queries In-Reply-To: <1356719189-2837-3-git-send-email-amdragon@mit.edu> References: <1356719189-2837-1-git-send-email-amdragon@mit.edu> <1356719189-2837-3-git-send-email-amdragon@mit.edu> User-Agent: Notmuch/0.14+236~g1d0044f (http://notmuchmail.org) Emacs/23.4.1 (x86_64-pc-linux-gnu) Date: Sat, 29 Dec 2012 16:50:51 +0000 Message-ID: <87y5ggssyc.fsf@qmul.ac.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Sender-Host-Address: 94.197.9.104 X-QM-SPAM-Info: Sender has good ham record. :) X-QM-Body-MD5: df4242471a0c8f99ee5abc9849039f5f (of first 20000 bytes) X-SpamAssassin-Score: -2.3 X-SpamAssassin-SpamBar: -- X-SpamAssassin-Report: The QM spam filters have analysed this message to determine if it is spam. We require at least 5.0 points to mark a message as spam. This message scored -2.3 points. Summary of the scoring: * -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/, * medium trust * [138.37.6.40 listed in list.dnswl.org] * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider * (markwalters1009[at]gmail.com) * -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay * domain X-QM-Scan-Virus: ClamAV says the message is clean X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 29 Dec 2012 16:51:02 -0000 On Fri, 28 Dec 2012, Austin Clements wrote: > This parses the subset of Xapian's boolean term quoting rules that are > used by make_boolean_term. This is provided as a generic string > utility, but will be used shortly in notmuch restore to parse and > optimize for ID queries. > --- > util/string-util.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ > util/string-util.h | 11 +++++++++++ > 2 files changed, 66 insertions(+) > > diff --git a/util/string-util.c b/util/string-util.c > index e4bea21..83b4953 100644 > --- a/util/string-util.c > +++ b/util/string-util.c > @@ -96,3 +96,58 @@ make_boolean_term (void *ctx, const char *prefix, const char *term, > > return 0; > } > + > +int > +parse_boolean_term (void *ctx, const char *str, > + char **prefix_out, char **term_out) > +{ > + *prefix_out = *term_out = NULL; > + > + /* Parse prefix */ > + const char *pos = strchr (str, ':'); > + if (! pos) > + goto FAIL; > + *prefix_out = talloc_strndup (ctx, str, pos - str); > + ++pos; > + > + /* Implement de-quoting compatible with make_boolean_term. */ > + if (*pos == '"') { > + char *out = talloc_array (ctx, char, strlen (pos)); > + int closed = 0; > + *term_out = out; > + /* Skip the opening quote, find the closing quote, and > + * un-double doubled internal quotes. */ > + for (++pos; *pos; ) { > + if (*pos == '"') { > + ++pos; > + if (*pos != '"') { > + /* Found the closing quote. */ > + closed = 1; > + break; > + } > + } > + *out++ = *pos++; > + } > + /* Did the term terminate without a closing quote or is there > + * trailing text after the closing quote? */ > + if (!closed || *pos) > + goto FAIL; > + *out = '\0'; > + } else { > + const char *start = pos; > + /* Check for text after the boolean term. */ > + while (*pos > ' ' && *pos != ')') > + ++pos; > + if (*pos) > + goto FAIL; > + /* No trailing text; dup the string so the caller can free > + * it. */ > + *term_out = talloc_strdup (ctx, start); > + } > + return 0; This looks like it will fail if a line contains trailing white space (and that seemed to be the case from my experiment). Is that wanted? If yes we may want to make the error message clearer as I got an error of the form Warning: cannot parse query: id:message-id@example.com and the fact that line contained trailing spaces was not clear. > + > + FAIL: > + talloc_free (*prefix_out); > + talloc_free (*term_out); > + return 1; > +} > diff --git a/util/string-util.h b/util/string-util.h > index b8844a3..43d49d0 100644 > --- a/util/string-util.h > +++ b/util/string-util.h > @@ -33,4 +33,15 @@ char *strtok_len (char *s, const char *delim, size_t *len); > int make_boolean_term (void *talloc_ctx, const char *prefix, const char *term, > char **buf, size_t *len); > > +/* Parse a boolean term query produced by make_boolean_term, returning > + * the prefix in *prefix_out and the term in *term_out. *prefix_out > + * and *term_out will be talloc'd with context ctx. > + * > + * Return: 0 on success, non-zero on parse error (including trailing > + * data in str). > + */ I think it would be worth detailing what requirements a boolean term here should meet to be parsed. In particular, since make_boolean_term is more restrictive than Xapian we should say whether this is like make_boolean_term, like Xapian or somewhere in between. Best wishes Mark > +int > +parse_boolean_term (void *ctx, const char *str, > + char **prefix_out, char **term_out); > + > #endif > -- > 1.7.10.4