--- /dev/null
+Return-Path: <amdragon@mit.edu>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 40FAE431FBF\r
+ for <notmuch@notmuchmail.org>; Sun, 6 Jan 2013 12:23:13 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id 3jVUUvFPj73g for <notmuch@notmuchmail.org>;\r
+ Sun, 6 Jan 2013 12:23:12 -0800 (PST)\r
+Received: from dmz-mailsec-scanner-4.mit.edu (DMZ-MAILSEC-SCANNER-4.MIT.EDU\r
+ [18.9.25.15])\r
+ by olra.theworths.org (Postfix) with ESMTP id 33D81431FAF\r
+ for <notmuch@notmuchmail.org>; Sun, 6 Jan 2013 12:23:12 -0800 (PST)\r
+X-AuditID: 1209190f-b7f016d000000e07-8b-50e9dd2f154d\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+ by dmz-mailsec-scanner-4.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id 4A.C9.03591.F2DD9E05; Sun, 6 Jan 2013 15:23:11 -0500 (EST)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+ by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id r06KNAJM012719; \r
+ Sun, 6 Jan 2013 15:23:10 -0500\r
+Received: from drake.dyndns.org (a069.catapulsion.net [70.36.81.69])\r
+ (authenticated bits=0)\r
+ (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+ by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id r06KMs19020343\r
+ (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+ Sun, 6 Jan 2013 15:23:02 -0500 (EST)\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+ (envelope-from <amdragon@mit.edu>)\r
+ id 1Trwk7-0007YF-G3; Sun, 06 Jan 2013 15:22:51 -0500\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v5 3/6] util: Function to parse boolean term queries\r
+Date: Sun, 6 Jan 2013 15:22:39 -0500\r
+Message-Id: <1357503762-28759-4-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <1357503762-28759-1-git-send-email-amdragon@mit.edu>\r
+References: <1357503762-28759-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFrrBIsWRmVeSWpSXmKPExsUixG6nrqt/92WAwbGZShY3WrsZLZqmO1us\r
+ nstjcf3mTGaLNyvnsTqweuycdZfd4/DXhSwet+6/Zvd4tuoWs8eWQ++ZA1ijuGxSUnMyy1KL\r
+ 9O0SuDL2tQUX9MlVPHpyjLGB8ZpYFyMnh4SAicTyb/fZIGwxiQv31oPZQgL7GCXOTBPoYuQC\r
+ stczSlz8u4wZwtnPJPHu+F5WCGcuo0TXvBYmkBY2AQ2JbfuXM4LYIgLSEjvvzgYrYhZoYpS4\r
+ tmku2FxhAReJ5sOL2EFsFgFViSNdE8DivAIOEnM7vjBC3KEo0f0MJM7BwSngKPFxnizESQ4S\r
+ d3bcZ5/AyL+AkWEVo2xKbpVubmJmTnFqsm5xcmJeXmqRrolebmaJXmpK6SZGUABySvLvYPx2\r
+ UOkQowAHoxIP74WdLwKEWBPLiitzDzFKcjApifLuvvgyQIgvKT+lMiOxOCO+qDQntfgQowQH\r
+ s5II775jQDnelMTKqtSifJiUNAeLkjjv1ZSb/kIC6YklqdmpqQWpRTBZGQ4OJQne+7eBGgWL\r
+ UtNTK9Iyc0oQ0kwcnCDDeYCGvwSp4S0uSMwtzkyHyJ9iVJQS510JkhAASWSU5sH1whLEK0Zx\r
+ oFeEebeCVPEAkwtc9yugwUxAg1MfPwcZXJKIkJJqYOTWC9hd397YNanbTbLPY7aOqtKlYo0o\r
+ yYdZOprFTvotG3KPzV+a95VJI/QOu2q7q8i5nuMsH76fVgktndS+dk7mqT7GaC/lYynl+3Mv\r
+ Lb27Um7JX43QmI7QBp37HxVfN1UVHNmvzD6baZLl75QfdwJzJOInvb87c+Utrl2T5duX3C/5\r
+ 99F1kRJLcUaioRZzUXEiAAmuOkDrAgAA\r
+Cc: tomi.ollila@iki.fi\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sun, 06 Jan 2013 20:23:13 -0000\r
+\r
+This parses the subset of Xapian's boolean term quoting rules that are\r
+used by make_boolean_term. This is provided as a generic string\r
+utility, but will be used shortly in notmuch restore to parse and\r
+optimize for ID queries.\r
+---\r
+ util/string-util.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ util/string-util.h | 16 ++++++++++\r
+ 2 files changed, 98 insertions(+)\r
+\r
+diff --git a/util/string-util.c b/util/string-util.c\r
+index 7a71049..aba9aa8 100644\r
+--- a/util/string-util.c\r
++++ b/util/string-util.c\r
+@@ -22,6 +22,7 @@\r
+ #include "string-util.h"\r
+ #include "talloc.h"\r
+ \r
++#include <ctype.h>\r
+ #include <errno.h>\r
+ \r
+ char *\r
+@@ -107,3 +108,84 @@ make_boolean_term (void *ctx, const char *prefix, const char *term,\r
+ \r
+ return 0;\r
+ }\r
++\r
++static const char*\r
++skip_space (const char *str)\r
++{\r
++ while (*str && isspace ((unsigned char) *str))\r
++ ++str;\r
++ return str;\r
++}\r
++\r
++int\r
++parse_boolean_term (void *ctx, const char *str,\r
++ char **prefix_out, char **term_out)\r
++{\r
++ int err = EINVAL;\r
++ *prefix_out = *term_out = NULL;\r
++\r
++ /* Parse prefix */\r
++ str = skip_space (str);\r
++ const char *pos = strchr (str, ':');\r
++ if (! pos)\r
++ goto FAIL;\r
++ *prefix_out = talloc_strndup (ctx, str, pos - str);\r
++ if (! *prefix_out) {\r
++ err = ENOMEM;\r
++ goto FAIL;\r
++ }\r
++ ++pos;\r
++\r
++ /* Implement de-quoting compatible with make_boolean_term. */\r
++ if (*pos == '"') {\r
++ char *out = talloc_array (ctx, char, strlen (pos));\r
++ int closed = 0;\r
++ if (! out) {\r
++ err = ENOMEM;\r
++ goto FAIL;\r
++ }\r
++ *term_out = out;\r
++ /* Skip the opening quote, find the closing quote, and\r
++ * un-double doubled internal quotes. */\r
++ for (++pos; *pos; ) {\r
++ if (*pos == '"') {\r
++ ++pos;\r
++ if (*pos != '"') {\r
++ /* Found the closing quote. */\r
++ closed = 1;\r
++ pos = skip_space (pos);\r
++ break;\r
++ }\r
++ }\r
++ *out++ = *pos++;\r
++ }\r
++ /* Did the term terminate without a closing quote or is there\r
++ * trailing text after the closing quote? */\r
++ if (!closed || *pos)\r
++ goto FAIL;\r
++ *out = '\0';\r
++ } else {\r
++ const char *start = pos;\r
++ /* Check for text after the boolean term. */\r
++ while (! is_unquoted_terminator (*pos))\r
++ ++pos;\r
++ if (*skip_space (pos)) {\r
++ err = EINVAL;\r
++ goto FAIL;\r
++ }\r
++ /* No trailing text; dup the string so the caller can free\r
++ * it. */\r
++ *term_out = talloc_strndup (ctx, start, pos - start);\r
++ if (! *term_out) {\r
++ err = ENOMEM;\r
++ goto FAIL;\r
++ }\r
++ }\r
++ return 0;\r
++\r
++ FAIL:\r
++ talloc_free (*prefix_out);\r
++ talloc_free (*term_out);\r
++ errno = err;\r
++ return -1;\r
++}\r
+diff --git a/util/string-util.h b/util/string-util.h\r
+index 719c276..0194607 100644\r
+--- a/util/string-util.h\r
++++ b/util/string-util.h\r
+@@ -34,4 +34,20 @@ char *strtok_len (char *s, const char *delim, size_t *len);\r
+ int make_boolean_term (void *talloc_ctx, const char *prefix, const char *term,\r
+ char **buf, size_t *len);\r
+ \r
++/* Parse a boolean term query consisting of a prefix, a colon, and a\r
++ * term that may be quoted as described for make_boolean_term. If the\r
++ * term is not quoted, then it ends at the first whitespace or close\r
++ * parenthesis. str may containing leading or trailing whitespace,\r
++ * but anything else is considered a parse error. This is compatible\r
++ * with anything produced by make_boolean_term, and supports a subset\r
++ * of the quoting styles supported by Xapian (and hence notmuch).\r
++ * *prefix_out and *term_out will be talloc'd with context ctx.\r
++ *\r
++ * Return: 0 on success, -1 on error. errno will be set to EINVAL if\r
++ * there is a parse error or ENOMEM if there is an allocation failure.\r
++ */\r
++int\r
++parse_boolean_term (void *ctx, const char *str,\r
++ char **prefix_out, char **term_out);\r
++\r
+ #endif\r
+-- \r
+1.7.10.4\r
+\r