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 7E023431FC0 for ; Fri, 28 Dec 2012 10:26:49 -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 H2azlYEVBf24 for ; Fri, 28 Dec 2012 10:26:48 -0800 (PST) Received: from dmz-mailsec-scanner-7.mit.edu (DMZ-MAILSEC-SCANNER-7.MIT.EDU [18.7.68.36]) by olra.theworths.org (Postfix) with ESMTP id 9A148431FB6 for ; Fri, 28 Dec 2012 10:26:48 -0800 (PST) X-AuditID: 12074424-b7f4e6d0000004ca-30-50dde4677ac2 Received: from mailhub-auth-3.mit.edu ( [18.9.21.43]) by dmz-mailsec-scanner-7.mit.edu (Symantec Messaging Gateway) with SMTP id 5F.44.01226.764EDD05; Fri, 28 Dec 2012 13:26:47 -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 qBSIQk1U001811; Fri, 28 Dec 2012 13:26:46 -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 qBSIQf4O012712 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); Fri, 28 Dec 2012 13:26:43 -0500 (EST) Received: from amthrax by drake.dyndns.org with local (Exim 4.77) (envelope-from ) id 1Toedj-0000sS-Rn; Fri, 28 Dec 2012 13:26:39 -0500 From: Austin Clements To: notmuch@notmuchmail.org Subject: [PATCH v3 2/5] util: Function to parse boolean term queries Date: Fri, 28 Dec 2012 13:26:26 -0500 Message-Id: <1356719189-2837-3-git-send-email-amdragon@mit.edu> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1356719189-2837-1-git-send-email-amdragon@mit.edu> References: <1356719189-2837-1-git-send-email-amdragon@mit.edu> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrPIsWRmVeSWpSXmKPExsUixCmqrZv+5G6AwYtPhhY3WrsZLZqmO1us nstjcf3mTGYHFo+ds+6ye9y6/5rd49mqW8weWw69Zw5gieKySUnNySxLLdK3S+DKmPTgC1vB V9GKzreXGBsYrwl0MXJySAiYSDy+eZkJwhaTuHBvPVsXIxeHkMA+RonGaXOZIJwNjBKd57tY IJyLTBL31j5kB2kREpjLKHFouhOIzSagIbFt/3JGEFtEQFpi593ZrCA2s0CexMNHW8HqhQVc JP7M/AFWwyKgKvF8wUkWEJtXwF5i7rUvbBBnKEp0P5sAZnMKOEi8mnKODWKXvcSpGctZJzDy L2BkWMUom5JbpZubmJlTnJqsW5ycmJeXWqRrrpebWaKXmlK6iREUbuwuKjsYmw8pHWIU4GBU 4uE9cftugBBrYllxZe4hRkkOJiVR3uUPgEJ8SfkplRmJxRnxRaU5qcWHGCU4mJVEePuagXK8 KYmVValF+TApaQ4WJXHe6yk3/YUE0hNLUrNTUwtSi2CyMhwcShK87o+BGgWLUtNTK9Iyc0oQ 0kwcnCDDeYCGR4DU8BYXJOYWZ6ZD5E8xKkqJ80aCJARAEhmleXC9sHTwilEc6BVhXjWQKh5g KoHrfgU0mAlosDXPHZDBJYkIKakGRrUMryVHLRI3uRfVtfLNvK27I4TdXKFyg/a74MBpl38G lO0vSln2hf+F68/8TS8LagLipCrPH17SytwstN7z47dfr45k2TJs3bHAlufW58itIt9TV33a liTIu8dnSYRiboHPv+CmjTlTZhiqHGh7y6P43eSBut8lFS3JVMlpfdcKeX3PX0+vVGIpzkg0 1GIuKk4EAJN2+KfiAgAA 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: Fri, 28 Dec 2012 18:26:49 -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 | 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; + + 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). + */ +int +parse_boolean_term (void *ctx, const char *str, + char **prefix_out, char **term_out); + #endif -- 1.7.10.4