[PATCH v2 1/5] util: Factor out boolean term quoting routine
authorAustin Clements <amdragon@MIT.EDU>
Wed, 26 Dec 2012 03:48:39 +0000 (22:48 +1900)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:52:48 +0000 (09:52 -0800)
2d/e340d7ea81d9a633f313c32ed730a590a56716 [new file with mode: 0644]

diff --git a/2d/e340d7ea81d9a633f313c32ed730a590a56716 b/2d/e340d7ea81d9a633f313c32ed730a590a56716
new file mode 100644 (file)
index 0000000..c95af8d
--- /dev/null
@@ -0,0 +1,282 @@
+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 1A706431FB6\r
+       for <notmuch@notmuchmail.org>; Tue, 25 Dec 2012 19:49:09 -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 VSbalYhC53QM for <notmuch@notmuchmail.org>;\r
+       Tue, 25 Dec 2012 19:49:03 -0800 (PST)\r
+Received: from dmz-mailsec-scanner-2.mit.edu (DMZ-MAILSEC-SCANNER-2.MIT.EDU\r
+       [18.9.25.13])\r
+       by olra.theworths.org (Postfix) with ESMTP id 64052431FBC\r
+       for <notmuch@notmuchmail.org>; Tue, 25 Dec 2012 19:48:59 -0800 (PST)\r
+X-AuditID: 1209190d-b7efd6d000001a82-6f-50da73aa80f0\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+       by dmz-mailsec-scanner-2.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id 50.41.06786.AA37AD05; Tue, 25 Dec 2012 22:48:58 -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 qBQ3mvD0019979; \r
+       Tue, 25 Dec 2012 22:48:57 -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 qBQ3mrI0013181\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Tue, 25 Dec 2012 22:48:55 -0500 (EST)\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1Tnhz8-0002yw-Kh; Tue, 25 Dec 2012 22:48:50 -0500\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 1/5] util: Factor out boolean term quoting routine\r
+Date: Tue, 25 Dec 2012 22:48:39 -0500\r
+Message-Id: <1356493723-11085-2-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <1356493723-11085-1-git-send-email-amdragon@mit.edu>\r
+References: <1356493723-11085-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFjrNIsWRmVeSWpSXmKPExsUixG6nrruq+FaAwc+tChY3WrsZLZqmO1us\r
+       nstjcf3mTGYHFo+ds+6ye9y6/5rd49mqW8weWw69Zw5gieKySUnNySxLLdK3S+DKWLsls+CJ\r
+       fkXP3R9MDYxXlbsYOTkkBEwk3qxczAxhi0lcuLeerYuRi0NIYB+jxL+lK9lBEkICGxglbr0v\r
+       hUhcZJI48eIdK4Qzl1Fiw72TrCBVbAIaEtv2L2cEsUUEpCV23p0NFmcWyJN4+Ggr0CQODmEB\r
+       V4l7H0RATBYBVYmGtfIgFbwCDhL/Du5igjhCUaL72QQ2kBJOAUeJnQ/qIE5wkJj48DzTBEb+\r
+       BYwMqxhlU3KrdHMTM3OKU5N1i5MT8/JSi3SN9HIzS/RSU0o3MYLCjFOSdwfju4NKhxgFOBiV\r
+       eHg3fL8ZIMSaWFZcmXuIUZKDSUmUd3vBrQAhvqT8lMqMxOKM+KLSnNTiQ4wSHMxKIrzOH4HK\r
+       eVMSK6tSi/JhUtIcLErivFdSbvoLCaQnlqRmp6YWpBbBZGU4OJQkeMuKgIYKFqWmp1akZeaU\r
+       IKSZODhBhvMADU8GqeEtLkjMLc5Mh8ifYlSUEucNAkkIgCQySvPgemFp4BWjONArwryuIFU8\r
+       wBQC1/0KaDAT0OBYvhsgg0sSEVJSDYyqx+8VJHZdcmdx8rW99z43+dvW1/WRNyqPBjkcZggr\r
+       z17ku6Q2syvrt8rfL5U7rR+eb+ue8e2Qb4+SjHdSq9dUvbRtwkx14XyyHtO/MQYzcy0yPpaw\r
+       Pfb26s7ZAYzf3vNMTTH+9omd/cIkHvWl5vE/c7/NeHzJLd2oxLm5RzTNRfXzb80bf5VYijMS\r
+       DbWYi4oTASXkZR/eAgAA\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: Wed, 26 Dec 2012 03:49:10 -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 |    9 ++++++++\r
+ 3 files changed, 87 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..7475e2c 100644\r
+--- a/util/string-util.h\r
++++ b/util/string-util.h\r
+@@ -19,4 +19,13 @@\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.\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