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 8EE61431FBF for ; Tue, 25 Dec 2012 19:49:00 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 deUZlYMmh2AO for ; Tue, 25 Dec 2012 19:48:58 -0800 (PST) Received: from dmz-mailsec-scanner-5.mit.edu (DMZ-MAILSEC-SCANNER-5.MIT.EDU [18.7.68.34]) by olra.theworths.org (Postfix) with ESMTP id 58DE2431FAE for ; Tue, 25 Dec 2012 19:48:58 -0800 (PST) X-AuditID: 12074422-b7f616d000000e7c-17-50da73a900d4 Received: from mailhub-auth-3.mit.edu ( [18.9.21.43]) by dmz-mailsec-scanner-5.mit.edu (Symantec Messaging Gateway) with SMTP id C1.4B.03708.9A37AD05; Tue, 25 Dec 2012 22:48:57 -0500 (EST) Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) by mailhub-auth-3.mit.edu (8.13.8/8.9.2) with ESMTP id qBQ3muQ1025518; Tue, 25 Dec 2012 22:48:56 -0500 Received: from drake.dyndns.org (c-76-21-105-205.hsd1.ca.comcast.net [76.21.105.205]) (authenticated bits=0) (User authenticated as amdragon@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id qBQ3mrh5013182 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); Tue, 25 Dec 2012 22:48:55 -0500 (EST) Received: from amthrax by drake.dyndns.org with local (Exim 4.77) (envelope-from ) id 1Tnhz8-0002yy-O5; Tue, 25 Dec 2012 22:48:50 -0500 From: Austin Clements To: notmuch@notmuchmail.org Subject: [PATCH v2 2/5] util: Function to parse boolean term queries Date: Tue, 25 Dec 2012 22:48:40 -0500 Message-Id: <1356493723-11085-3-git-send-email-amdragon@mit.edu> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1356493723-11085-1-git-send-email-amdragon@mit.edu> References: <1356493723-11085-1-git-send-email-amdragon@mit.edu> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrPIsWRmVeSWpSXmKPExsUixCmqrbuy+FaAwazPRhY3WrsZLZqmO1us nstjcf3mTGYHFo+ds+6ye9y6/5rd49mqW8weWw69Zw5gieKySUnNySxLLdK3S+DKOHXuMWPB E5GKJ5c+szUwPuXvYuTkkBAwkbjRtIoNwhaTuHBvPZDNxSEksI9R4tvfbhYIZwOjxORfz1kh nItMEt+unmaHcOYySvw7uJ4ZpJ9NQENi2/7ljCC2iIC0xM67s1lBbGaBPImHj7ayg9jCAi4S d879YgGxWQRUJRYe+Qlm8wo4SPTufMoOcYeiRPezCUB3cHBwCjhK7HxQBxIWAiqZ+PA80wRG /gWMDKsYZVNyq3RzEzNzilOTdYuTE/PyUot0TfVyM0v0UlNKNzGCwo3dRWkH48+DSocYBTgY lXh4N3y/GSDEmlhWXJl7iFGSg0lJlHd7wa0AIb6k/JTKjMTijPii0pzU4kOMEhzMSiK8zh+B ynlTEiurUovyYVLSHCxK4rzXUm76CwmkJ5akZqemFqQWwWRlODiUJHg3FwENFSxKTU+tSMvM KUFIM3FwggznARp+E6SGt7ggMbc4Mx0if4pRUUqc9wJIQgAkkVGaB9cLSwevGMWBXhHm3QNS xQNMJXDdr4AGMwENjuW7ATK4JBEhJdUAjE3VbX8XztT72cIwa47oneUPZyTuT/F4sKmg0Mzq Rc4n6VJD2Ujb6w9yF8s1fte4Gp92YsKUN3u3FbC8UerX0BfYpTix5bUA92S3Sbf0vH8oHmCx OFV1alKjFYNbBpcTwyJunoN8jG9eRyzRm/TqkUirSyrni+V1PvsWeef8iJ+YpKXwzk5MiaU4 I9FQi7moOBEA49dt/+ICAAA= 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: Wed, 26 Dec 2012 03:49:01 -0000 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 | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ util/string-util.h | 11 +++++++++++ 2 files changed, 62 insertions(+) diff --git a/util/string-util.c b/util/string-util.c index e4bea21..db01b4b 100644 --- a/util/string-util.c +++ b/util/string-util.c @@ -96,3 +96,54 @@ 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_strdup (ctx, pos + 1); + int closed = 0; + /* Find the closing quote and un-double doubled internal + * quotes. */ + for (pos = *term_out = out; *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 { + *term_out = talloc_strdup (ctx, pos); + /* Check for text after the boolean term. */ + while (*pos > ' ' && *pos != ')') + ++pos; + if (*pos) + goto FAIL; + } + return 0; + + FAIL: + talloc_free (*prefix_out); + talloc_free (*term_out); + return 1; +} diff --git a/util/string-util.h b/util/string-util.h index 7475e2c..aff2d65 100644 --- a/util/string-util.h +++ b/util/string-util.h @@ -28,4 +28,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). + */ +int +parse_boolean_term (void *ctx, const char *str, + char **prefix_out, char **term_out); + #endif -- 1.7.10.4