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 8B3B1431FBC for ; Sun, 30 Dec 2012 22:42:54 -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 OnjwGyRlmNiZ for ; Sun, 30 Dec 2012 22:42:54 -0800 (PST) Received: from dmz-mailsec-scanner-2.mit.edu (DMZ-MAILSEC-SCANNER-2.MIT.EDU [18.9.25.13]) by olra.theworths.org (Postfix) with ESMTP id 6B59D431FB6 for ; Sun, 30 Dec 2012 22:42:53 -0800 (PST) X-AuditID: 1209190d-b7efd6d000001a82-14-50e133eccceb Received: from mailhub-auth-3.mit.edu ( [18.9.21.43]) by dmz-mailsec-scanner-2.mit.edu (Symantec Messaging Gateway) with SMTP id E1.2C.06786.CE331E05; Mon, 31 Dec 2012 01:42:52 -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 qBV6goKp030829; Mon, 31 Dec 2012 01:42:50 -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 qBV6gkP2009880 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); Mon, 31 Dec 2012 01:42:49 -0500 (EST) Received: from amthrax by drake.dyndns.org with local (Exim 4.77) (envelope-from ) id 1TpZ5B-0000ih-WC; Mon, 31 Dec 2012 01:42:45 -0500 From: Austin Clements To: notmuch@notmuchmail.org Subject: [PATCH v4 2/5] util: Function to parse boolean term queries Date: Mon, 31 Dec 2012 01:42:39 -0500 Message-Id: <1356936162-2589-3-git-send-email-amdragon@mit.edu> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1356936162-2589-1-git-send-email-amdragon@mit.edu> References: <1356936162-2589-1-git-send-email-amdragon@mit.edu> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrJIsWRmVeSWpSXmKPExsUixCmqrfvG+GGAwfxVlhY3WrsZLZqmO1us nstjcf3mTGaLNyvnsTqweuycdZfd4/DXhSwet+6/Zvd4tuoWs8eWQ++ZA1ijuGxSUnMyy1KL 9O0SuDJWNU5iL9gpXfH15RemBsbvIl2MHBwSAiYSn+dHdjFyApliEhfurWcDsYUE9jFKbOio 6WLkArI3MEqsnzmLEcK5yCRx4M4MNghnLqPEvjVPGEFa2AQ0JLbtXw5miwhIS+y8O5sVpIhZ oIlR4tqmuWBzhQVcJM58ngNWxCKgKnH+6hwmEJtXwF5iyqJGNog7FCW6n00AszkFHCR2PpvF AnGTvcTuvecZJzDyL2BkWMUom5JbpZubmJlTnJqsW5ycmJeXWqRrpJebWaKXmlK6iREcgpK8 OxjfHVQ6xCjAwajEw8vB9DBAiDWxrLgy9xCjJAeTkiivEjCAhfiS8lMqMxKLM+KLSnNSiw8x SnAwK4nwvtUCyvGmJFZWpRblw6SkOViUxHmvpNz0FxJITyxJzU5NLUgtgsnKcHAoSfBqgQwV LEpNT61Iy8wpQUgzcXCCDOcBGv7RCGR4cUFibnFmOkT+FKOilDivPEizAEgiozQPrheWIl4x igO9IszLB1LFA0wvcN2vgAYzAQ3WYngAMrgkESEl1cDozn1ZTqHzUea2rbEZR44IZyecPaBR /OGSoeusGTHr54RuK7da1XPOxVOhsudZnmBNq/qqU/dnhJQ4JQrsviH3evMCQ+0Fz3lnr+kN +fJRTZs7tet5pvXk+q1em2MOeoS/W70gMDhY6sPng2Exau35v6eKrZP3+vU6MOLlt4M9u/ZW P7jW4yipxFKckWioxVxUnAgAeel3YuwCAAA= Cc: tomi.ollila@iki.fi 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: Mon, 31 Dec 2012 06:42:54 -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 | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ util/string-util.h | 15 ++++++++++++ 2 files changed, 82 insertions(+) diff --git a/util/string-util.c b/util/string-util.c index e4bea21..52c7781 100644 --- a/util/string-util.c +++ b/util/string-util.c @@ -22,6 +22,8 @@ #include "string-util.h" #include "talloc.h" +#include + char * strtok_len (char *s, const char *delim, size_t *len) { @@ -96,3 +98,68 @@ make_boolean_term (void *ctx, const char *prefix, const char *term, return 0; } + +static const char* +skip_space (const char *str) +{ + while (*str && isspace (*str)) + ++str; + return str; +} + +int +parse_boolean_term (void *ctx, const char *str, + char **prefix_out, char **term_out) +{ + *prefix_out = *term_out = NULL; + + /* Parse prefix */ + str = skip_space (str); + 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; + pos = skip_space (pos); + 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 (*skip_space (pos)) + goto FAIL; + /* No trailing text; dup the string so the caller can free + * it. */ + *term_out = talloc_strndup (ctx, start, pos - 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..8b9fe50 100644 --- a/util/string-util.h +++ b/util/string-util.h @@ -33,4 +33,19 @@ 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 consisting of a prefix, a colon, and a + * term that may be quoted as described for make_boolean_term. If the + * term is not quoted, then it ends at the first whitespace or close + * parenthesis. str may containing leading or trailing whitespace, + * but anything else is considered a parse error. This is compatible + * with anything produced by make_boolean_term, and supports a subset + * of the quoting styles supported by Xapian (and hence notmuch). + * *prefix_out and *term_out will be talloc'd with context ctx. + * + * Return: 0 on success, non-zero on parse error. + */ +int +parse_boolean_term (void *ctx, const char *str, + char **prefix_out, char **term_out); + #endif -- 1.7.10.4