Re: [PATCH v4 1/5] util: Factor out boolean term quoting routine
authorJani Nikula <jani@nikula.org>
Thu, 3 Jan 2013 16:48:54 +0000 (17:48 +0100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:52:54 +0000 (09:52 -0800)
70/d75bfb7f6ab495e069ceb617047fff104cd9f6 [new file with mode: 0644]

diff --git a/70/d75bfb7f6ab495e069ceb617047fff104cd9f6 b/70/d75bfb7f6ab495e069ceb617047fff104cd9f6
new file mode 100644 (file)
index 0000000..d254d09
--- /dev/null
@@ -0,0 +1,290 @@
+Return-Path: <jani@nikula.org>\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 4EE11431FAF\r
+       for <notmuch@notmuchmail.org>; Thu,  3 Jan 2013 08:49:07 -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 yDceBnu96li7 for <notmuch@notmuchmail.org>;\r
+       Thu,  3 Jan 2013 08:49:06 -0800 (PST)\r
+Received: from mail-bk0-f53.google.com (mail-bk0-f53.google.com\r
+       [209.85.214.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 3409C431FAE\r
+       for <notmuch@notmuchmail.org>; Thu,  3 Jan 2013 08:49:06 -0800 (PST)\r
+Received: by mail-bk0-f53.google.com with SMTP id j5so6793968bkw.12\r
+       for <notmuch@notmuchmail.org>; Thu, 03 Jan 2013 08:49:03 -0800 (PST)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+       d=google.com; s=20120113;\r
+       h=x-received:from:to:cc:subject:in-reply-to:references:user-agent\r
+       :date:message-id:mime-version:content-type:x-gm-message-state;\r
+       bh=JT+aeu6kHcmbBMCWAjct13X/5hbEsPFm4KVnD83yHpo=;\r
+       b=fnvnRcQm4q6rI7sPXgfoKJU69tQqGMeWWHCVpFkCSCtiMeEcjAG8Lyl9My/ZMwhdvf\r
+       taIocJJ24meA2X86tYhHfOquDA3pQUh2J91xGpO30h8SJfiCFIEppgFH0bgGAS+JwGAe\r
+       6U9BewuJRFWnXpGgNNYYCfxpscU68WnYYnoyH71SXNke6FBTuC768A8QdGZyRQrqUtPe\r
+       FIyoARDD+8uBrVDCiXUl8jOhul3r5yktqK0N4d4641x9i1c2IbLeNhvZekM2dDqJLh4v\r
+       i0vw0pyqyaxOE8B+Q0JGrHAMfSR0zIsLQaSJovUVBjcWhpj5yJKaSFy6TtZvlIL9Fi3e\r
+       9+bA==\r
+X-Received: by 10.204.3.220 with SMTP id 28mr23780642bko.50.1357231743438;\r
+       Thu, 03 Jan 2013 08:49:03 -0800 (PST)\r
+Received: from localhost ([2001:4b98:dc0:43:216:3eff:fe1b:25f3])\r
+       by mx.google.com with ESMTPS id o7sm34593411bkv.13.2013.01.03.08.49.00\r
+       (version=SSLv3 cipher=OTHER); Thu, 03 Jan 2013 08:49:02 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: Austin Clements <amdragon@MIT.EDU>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH v4 1/5] util: Factor out boolean term quoting routine\r
+In-Reply-To: <1356936162-2589-2-git-send-email-amdragon@mit.edu>\r
+References: <1356936162-2589-1-git-send-email-amdragon@mit.edu>\r
+       <1356936162-2589-2-git-send-email-amdragon@mit.edu>\r
+User-Agent: Notmuch/0.14+235~gdaf492b (http://notmuchmail.org) Emacs/23.2.1\r
+       (x86_64-pc-linux-gnu)\r
+Date: Thu, 03 Jan 2013 17:48:54 +0100\r
+Message-ID: <87y5gagqkp.fsf@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Gm-Message-State:\r
+ ALoCoQns7ri9as/bFmXiFxhT85GCq5nfW3PAwWJgOze5h1Lcy31e/6iRV9pgzjdMmCNys/Fm8UL+\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: Thu, 03 Jan 2013 16:49:07 -0000\r
+\r
+On Mon, 31 Dec 2012, Austin Clements <amdragon@MIT.EDU> wrote:\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
+\r
+Should that be *in >= 127?\r
+\r
+Otherwise LGTM.\r
+\r
+Jani.\r
+\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