--- /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 0C0DA431FC0\r
+ for <notmuch@notmuchmail.org>; Sun, 30 Dec 2012 22:42:54 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 1.151\r
+X-Spam-Level: *\r
+X-Spam-Status: No, score=1.151 tagged_above=-999 required=5\r
+ tests=[FUZZY_AMBIEN=1.851, 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 L6Tz7jduiYti for <notmuch@notmuchmail.org>;\r
+ Sun, 30 Dec 2012 22:42:52 -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 906D1431FBC\r
+ for <notmuch@notmuchmail.org>; Sun, 30 Dec 2012 22:42:52 -0800 (PST)\r
+X-AuditID: 1209190f-b7f016d000000e07-b1-50e133ec16aa\r
+Received: from mailhub-auth-2.mit.edu ( [18.7.62.36])\r
+ by dmz-mailsec-scanner-4.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id 7C.5C.03591.CE331E05; Mon, 31 Dec 2012 01:42:52 -0500 (EST)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+ by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id qBV6goLM027841; \r
+ Mon, 31 Dec 2012 01:42:50 -0500\r
+Received: from drake.dyndns.org (c-76-21-105-205.hsd1.ca.comcast.net\r
+ [76.21.105.205]) (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 qBV6gk1b009879\r
+ (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+ Mon, 31 Dec 2012 01:42:48 -0500 (EST)\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+ (envelope-from <amdragon@mit.edu>)\r
+ id 1TpZ5B-0000if-Sr; Mon, 31 Dec 2012 01:42:45 -0500\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v4 1/5] util: Factor out boolean term quoting routine\r
+Date: Mon, 31 Dec 2012 01:42:38 -0500\r
+Message-Id: <1356936162-2589-2-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <1356936162-2589-1-git-send-email-amdragon@mit.edu>\r
+References: <1356936162-2589-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFrrFIsWRmVeSWpSXmKPExsUixG6novvG+GGAQfNJYYsbrd2MFk3TnS1W\r
+ z+WxuH5zJrPFm5XzWB1YPXbOusvucfjrQhaPW/dfs3s8W3WL2WPLoffMAaxRXDYpqTmZZalF\r
+ +nYJXBlzr65lL9hjVHF772KWBsa3ql2MnBwSAiYSu94sZYewxSQu3FvP1sXIxSEksI9RYuuU\r
+ XawQzgZGiW/b7zFBOBeZJJbMaGaHcOYySpw71sAI0s8moCGxbf9yMFtEQFpi593ZYO3MAk2M\r
+ Etc2zWUDSQgLuErsm9jLCmKzCKhK3Gq+ARTn4OAVsJdY9KoQ4g5Fie5nE8DKOQUcJHY+m8UC\r
+ YgsBlezee55xAiP/AkaGVYyyKblVurmJmTnFqcm6xcmJeXmpRbomermZJXqpKaWbGEFByCnJ\r
+ v4Px20GlQ4wCHIxKPLwcTA8DhFgTy4orcw8xSnIwKYnyKgFDWIgvKT+lMiOxOCO+qDQntfgQ\r
+ owQHs5II71stoBxvSmJlVWpRPkxKmoNFSZz3aspNfyGB9MSS1OzU1ILUIpisDAeHkgTvBiOg\r
+ RsGi1PTUirTMnBKENBMHJ8hwHqDhH0FqeIsLEnOLM9Mh8qcYFaXEeZ+DJARAEhmleXC9sCTx\r
+ ilEc6BVh3vMgVTzABAPX/QpoMBPQYC2GByCDSxIRUlINjAvvM36+d2BaeOPttb9jn2zsWeSr\r
+ YmX0J2CCbtK6b68nL7jduNpdyHrZl/us9W97DnU+8w/v09kRMzX2Nfcc5qs7RAuzPpSopbUX\r
+ hUScibFcumtyro/Lkcwp+z5/sjpVV2IZ775ArnzRh5aw4IP7SvqeHGAVft0nbBE8K8Lkq9fJ\r
+ puO37htvYlBiKc5INNRiLipOBACC+jl47QIAAA==\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: Mon, 31 Dec 2012 06:42:54 -0000\r
+\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+\r
+This is now a generic boolean term quoting function. It performs\r
+minimal quoting to produce user-friendly queries.\r
+\r
+This could live in tag-util as well, but it is really nothing specific\r
+to tags (although the conventions are specific to Xapian).\r
+\r
+The API is changed from "caller-allocates" to "readline-like". The\r
+scan for max tag length is pushed down into the quoting routine.\r
+Furthermore, this now combines the term prefix with the quoted term;\r
+arguably this is just as easy to do in the caller, but this will\r
+nicely parallel the boolean term parsing function to be introduced\r
+shortly.\r
+\r
+This is an amalgamation of code written by David Bremner and myself.\r
+---\r
+ notmuch-tag.c | 48 ++++++++++++---------------------------\r
+ util/string-util.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ util/string-util.h | 14 ++++++++++++\r
+ 3 files changed, 92 insertions(+), 34 deletions(-)\r
+\r
+diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+index 88d559b..fc9d43a 100644\r
+--- a/notmuch-tag.c\r
++++ b/notmuch-tag.c\r
+@@ -19,6 +19,7 @@\r
+ */\r
+ \r
+ #include "notmuch-client.h"\r
++#include "string-util.h"\r
+ \r
+ static volatile sig_atomic_t interrupted;\r
+ \r
+@@ -35,25 +36,6 @@ handle_sigint (unused (int sig))\r
+ interrupted = 1;\r
+ }\r
+ \r
+-static char *\r
+-_escape_tag (char *buf, const char *tag)\r
+-{\r
+- const char *in = tag;\r
+- char *out = buf;\r
+-\r
+- /* Boolean terms surrounded by double quotes can contain any\r
+- * character. Double quotes are quoted by doubling them. */\r
+- *out++ = '"';\r
+- while (*in) {\r
+- if (*in == '"')\r
+- *out++ = '"';\r
+- *out++ = *in++;\r
+- }\r
+- *out++ = '"';\r
+- *out = 0;\r
+- return buf;\r
+-}\r
+-\r
+ typedef struct {\r
+ const char *tag;\r
+ notmuch_bool_t remove;\r
+@@ -71,25 +53,16 @@ _optimize_tag_query (void *ctx, const char *orig_query_string,\r
+ * parenthesize and the exclusion part of the query must not use\r
+ * the '-' operator (though the NOT operator is fine). */\r
+ \r
+- char *escaped, *query_string;\r
++ char *escaped = NULL;\r
++ size_t escaped_len = 0;\r
++ char *query_string;\r
+ const char *join = "";\r
+- int i;\r
+- unsigned int max_tag_len = 0;\r
++ size_t i;\r
+ \r
+ /* Don't optimize if there are no tag changes. */\r
+ if (tag_ops[0].tag == NULL)\r
+ return talloc_strdup (ctx, orig_query_string);\r
+ \r
+- /* Allocate a buffer for escaping tags. This is large enough to\r
+- * hold a fully escaped tag with every character doubled plus\r
+- * enclosing quotes and a NUL. */\r
+- for (i = 0; tag_ops[i].tag; i++)\r
+- if (strlen (tag_ops[i].tag) > max_tag_len)\r
+- max_tag_len = strlen (tag_ops[i].tag);\r
+- escaped = talloc_array (ctx, char, max_tag_len * 2 + 3);\r
+- if (! escaped)\r
+- return NULL;\r
+-\r
+ /* Build the new query string */\r
+ if (strcmp (orig_query_string, "*") == 0)\r
+ query_string = talloc_strdup (ctx, "(");\r
+@@ -97,10 +70,17 @@ _optimize_tag_query (void *ctx, const char *orig_query_string,\r
+ query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);\r
+ \r
+ for (i = 0; tag_ops[i].tag && query_string; i++) {\r
++ /* XXX in case of OOM, query_string will be deallocated when\r
++ * ctx is, which might be at shutdown */\r
++ if (make_boolean_term (ctx,\r
++ "tag", tag_ops[i].tag,\r
++ &escaped, &escaped_len))\r
++ return NULL;\r
++\r
+ query_string = talloc_asprintf_append_buffer (\r
+- query_string, "%s%stag:%s", join,\r
++ query_string, "%s%s%s", join,\r
+ tag_ops[i].remove ? "" : "not ",\r
+- _escape_tag (escaped, tag_ops[i].tag));\r
++ escaped);\r
+ join = " or ";\r
+ }\r
+ \r
+diff --git a/util/string-util.c b/util/string-util.c\r
+index 44f8cd3..e4bea21 100644\r
+--- a/util/string-util.c\r
++++ b/util/string-util.c\r
+@@ -20,6 +20,7 @@\r
+ \r
+ \r
+ #include "string-util.h"\r
++#include "talloc.h"\r
+ \r
+ char *\r
+ strtok_len (char *s, const char *delim, size_t *len)\r
+@@ -32,3 +33,66 @@ strtok_len (char *s, const char *delim, size_t *len)\r
+ \r
+ return *len ? s : NULL;\r
+ }\r
++\r
++int\r
++make_boolean_term (void *ctx, const char *prefix, const char *term,\r
++ char **buf, size_t *len)\r
++{\r
++ const char *in;\r
++ char *out;\r
++ size_t needed = 3;\r
++ int need_quoting = 0;\r
++\r
++ /* Do we need quoting? To be paranoid, we quote anything\r
++ * containing a quote, even though it only matters at the\r
++ * beginning, and anything containing non-ASCII text. */\r
++ for (in = term; *in && !need_quoting; in++)\r
++ if (*in <= ' ' || *in == ')' || *in == '"' || (unsigned char)*in > 127)\r
++ need_quoting = 1;\r
++\r
++ if (need_quoting)\r
++ for (in = term; *in; in++)\r
++ needed += (*in == '"') ? 2 : 1;\r
++ else\r
++ needed = strlen (term) + 1;\r
++\r
++ /* Reserve space for the prefix */\r
++ if (prefix)\r
++ needed += strlen (prefix) + 1;\r
++\r
++ if ((*buf == NULL) || (needed > *len)) {\r
++ *len = 2 * needed;\r
++ *buf = talloc_realloc (ctx, *buf, char, *len);\r
++ }\r
++\r
++ if (! *buf)\r
++ return 1;\r
++\r
++ out = *buf;\r
++\r
++ /* Copy in the prefix */\r
++ if (prefix) {\r
++ strcpy (out, prefix);\r
++ out += strlen (prefix);\r
++ *out++ = ':';\r
++ }\r
++\r
++ if (! need_quoting) {\r
++ strcpy (out, term);\r
++ return 0;\r
++ }\r
++\r
++ /* Quote term by enclosing it in double quotes and doubling any\r
++ * internal double quotes. */\r
++ *out++ = '"';\r
++ in = term;\r
++ while (*in) {\r
++ if (*in == '"')\r
++ *out++ = '"';\r
++ *out++ = *in++;\r
++ }\r
++ *out++ = '"';\r
++ *out = '\0';\r
++\r
++ return 0;\r
++}\r
+diff --git a/util/string-util.h b/util/string-util.h\r
+index ac7676c..b8844a3 100644\r
+--- a/util/string-util.h\r
++++ b/util/string-util.h\r
+@@ -19,4 +19,18 @@\r
+ \r
+ char *strtok_len (char *s, const char *delim, size_t *len);\r
+ \r
++/* Construct a boolean term query with the specified prefix (e.g.,\r
++ * "id") and search term, quoting term as necessary. Specifically, if\r
++ * term contains any non-printable ASCII characters, non-ASCII\r
++ * characters, close parenthesis or double quotes, it will be enclosed\r
++ * in double quotes and any internal double quotes will be doubled\r
++ * (e.g. a"b -> "a""b"). The result will be a valid notmuch query and\r
++ * can be parsed by parse_boolean_term.\r
++ *\r
++ * Output is into buf; it may be talloc_realloced.\r
++ * Return: 0 on success, non-zero on memory allocation failure.\r
++ */\r
++int make_boolean_term (void *talloc_ctx, const char *prefix, const char *term,\r
++ char **buf, size_t *len);\r
++\r
+ #endif\r
+-- \r
+1.7.10.4\r
+\r