[PATCH v4 2/5] util: Function to parse boolean term queries
authorAustin Clements <amdragon@MIT.EDU>
Mon, 31 Dec 2012 06:42:39 +0000 (01:42 +1900)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:52:53 +0000 (09:52 -0800)
8c/35a494b6dfc8665cc41d6732b671b119448836 [new file with mode: 0644]

diff --git a/8c/35a494b6dfc8665cc41d6732b671b119448836 b/8c/35a494b6dfc8665cc41d6732b671b119448836
new file mode 100644 (file)
index 0000000..e18ba22
--- /dev/null
@@ -0,0 +1,191 @@
+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 8B3B1431FBC\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: -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 OnjwGyRlmNiZ for <notmuch@notmuchmail.org>;\r
+       Sun, 30 Dec 2012 22:42:54 -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 6B59D431FB6\r
+       for <notmuch@notmuchmail.org>; Sun, 30 Dec 2012 22:42:53 -0800 (PST)\r
+X-AuditID: 1209190d-b7efd6d000001a82-14-50e133eccceb\r
+Received: from mailhub-auth-3.mit.edu ( [18.9.21.43])\r
+       by dmz-mailsec-scanner-2.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id E1.2C.06786.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-3.mit.edu (8.13.8/8.9.2) with ESMTP id qBV6goKp030829; \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 qBV6gkP2009880\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Mon, 31 Dec 2012 01:42:49 -0500 (EST)\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1TpZ5B-0000ih-WC; Mon, 31 Dec 2012 01:42:45 -0500\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v4 2/5] util: Function to parse boolean term queries\r
+Date: Mon, 31 Dec 2012 01:42:39 -0500\r
+Message-Id: <1356936162-2589-3-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+NgFrrJIsWRmVeSWpSXmKPExsUixCmqrfvG+GGAwfxVlhY3WrsZLZqmO1us\r
+       nstjcf3mTGaLNyvnsTqweuycdZfd4/DXhSwet+6/Zvd4tuoWs8eWQ++ZA1ijuGxSUnMyy1KL\r
+       9O0SuDJWNU5iL9gpXfH15RemBsbvIl2MHBwSAiYSn+dHdjFyApliEhfurWcDsYUE9jFKbOio\r
+       6WLkArI3MEqsnzmLEcK5yCRx4M4MNghnLqPEvjVPGEFa2AQ0JLbtXw5miwhIS+y8O5sVpIhZ\r
+       oIlR4tqmuWBzhQVcJM58ngNWxCKgKnH+6hwmEJtXwF5iyqJGNog7FCW6n00AszkFHCR2PpvF\r
+       AnGTvcTuvecZJzDyL2BkWMUom5JbpZubmJlTnJqsW5ycmJeXWqRrpJebWaKXmlK6iREcgpK8\r
+       OxjfHVQ6xCjAwajEw8vB9DBAiDWxrLgy9xCjJAeTkiivEjCAhfiS8lMqMxKLM+KLSnNSiw8x\r
+       SnAwK4nwvtUCyvGmJFZWpRblw6SkOViUxHmvpNz0FxJITyxJzU5NLUgtgsnKcHAoSfBqgQwV\r
+       LEpNT61Iy8wpQUgzcXCCDOcBGv7RCGR4cUFibnFmOkT+FKOilDivPEizAEgiozQPrheWIl4x\r
+       igO9IszLB1LFA0wvcN2vgAYzAQ3WYngAMrgkESEl1cDozn1ZTqHzUea2rbEZR44IZyecPaBR\r
+       /OGSoeusGTHr54RuK7da1XPOxVOhsudZnmBNq/qqU/dnhJQ4JQrsviH3evMCQ+0Fz3lnr+kN\r
+       +fJRTZs7tet5pvXk+q1em2MOeoS/W70gMDhY6sPng2Exau35v6eKrZP3+vU6MOLlt4M9u/ZW\r
+       P7jW4yipxFKckWioxVxUnAgAeel3YuwCAAA=\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
+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 |   67 ++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ util/string-util.h |   15 ++++++++++++\r
+ 2 files changed, 82 insertions(+)\r
+\r
+diff --git a/util/string-util.c b/util/string-util.c\r
+index e4bea21..52c7781 100644\r
+--- a/util/string-util.c\r
++++ b/util/string-util.c\r
+@@ -22,6 +22,8 @@\r
+ #include "string-util.h"\r
+ #include "talloc.h"\r
\r
++#include <ctype.h>\r
++\r
+ char *\r
+ strtok_len (char *s, const char *delim, size_t *len)\r
+ {\r
+@@ -96,3 +98,68 @@ 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 (*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
++    *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
++    ++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
++      *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 (*pos > ' ' && *pos != ')')\r
++          ++pos;\r
++      if (*skip_space (pos))\r
++          goto FAIL;\r
++      /* No trailing text; dup the string so the caller can free\r
++       * it. */\r
++      *term_out = talloc_strndup (ctx, start, pos - start);\r
++    }\r
++    return 0;\r
++\r
++ FAIL:\r
++    talloc_free (*prefix_out);\r
++    talloc_free (*term_out);\r
++    return 1;\r
++}\r
+diff --git a/util/string-util.h b/util/string-util.h\r
+index b8844a3..8b9fe50 100644\r
+--- a/util/string-util.h\r
++++ b/util/string-util.h\r
+@@ -33,4 +33,19 @@ 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, non-zero on parse error.\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