[Patch v5 04/11] tag-util.[ch]: New files for common tagging routines
authordavid <david@tethera.net>
Sat, 8 Dec 2012 22:56:54 +0000 (18:56 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:51:51 +0000 (09:51 -0800)
bd/8c55191e30b69a0a6c641b97f9c2171b4824aa [new file with mode: 0644]

diff --git a/bd/8c55191e30b69a0a6c641b97f9c2171b4824aa b/bd/8c55191e30b69a0a6c641b97f9c2171b4824aa
new file mode 100644 (file)
index 0000000..2558188
--- /dev/null
@@ -0,0 +1,537 @@
+Return-Path: <bremner@tethera.net>\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 50EAF431FC2\r
+       for <notmuch@notmuchmail.org>; Sat,  8 Dec 2012 14:57:54 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+       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 I7wXgAni6h5I for <notmuch@notmuchmail.org>;\r
+       Sat,  8 Dec 2012 14:57:51 -0800 (PST)\r
+Received: from tesseract.cs.unb.ca (tesseract.cs.unb.ca [131.202.240.238])\r
+       (using TLSv1 with cipher AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id F0D61429E3E\r
+       for <notmuch@notmuchmail.org>; Sat,  8 Dec 2012 14:57:42 -0800 (PST)\r
+Received: from fctnnbsc30w-142167090129.dhcp-dynamic.fibreop.nb.bellaliant.net\r
+       ([142.167.90.129] helo=zancas.localnet)\r
+       by tesseract.cs.unb.ca with esmtpsa\r
+       (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72)\r
+       (envelope-from <bremner@tethera.net>)\r
+       id 1ThTL3-00066g-If; Sat, 08 Dec 2012 18:57:42 -0400\r
+Received: from bremner by zancas.localnet with local (Exim 4.80)\r
+       (envelope-from <bremner@tethera.net>)\r
+       id 1ThTKy-0000qc-44; Sat, 08 Dec 2012 18:57:36 -0400\r
+From: david@tethera.net\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v5 04/11] tag-util.[ch]: New files for common tagging routines\r
+Date: Sat,  8 Dec 2012 18:56:54 -0400\r
+Message-Id: <1355007421-3069-5-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <1355007421-3069-1-git-send-email-david@tethera.net>\r
+References: <1355007421-3069-1-git-send-email-david@tethera.net>\r
+X-Spam_bar: -\r
+Cc: David Bremner <bremner@debian.org>\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: Sat, 08 Dec 2012 22:57:54 -0000\r
+\r
+From: David Bremner <bremner@debian.org>\r
+\r
+These are meant to be shared between notmuch-tag and notmuch-restore.\r
+\r
+The bulk of the routines implement a "tag operation list" abstract\r
+data type act as a structured representation of a set of tag\r
+operations (typically coming from a single tag command or line of\r
+input).\r
+---\r
+ Makefile.local |    1 +\r
+ tag-util.c     |  305 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ tag-util.h     |  135 +++++++++++++++++++++++++\r
+ 3 files changed, 441 insertions(+)\r
+ create mode 100644 tag-util.c\r
+ create mode 100644 tag-util.h\r
+\r
+diff --git a/Makefile.local b/Makefile.local\r
+index 0db1713..c274f07 100644\r
+--- a/Makefile.local\r
++++ b/Makefile.local\r
+@@ -275,6 +275,7 @@ notmuch_client_srcs =              \\r
+       query-string.c          \\r
+       mime-node.c             \\r
+       crypto.c                \\r
++      tag-util.c\r
\r
+ notmuch_client_modules = $(notmuch_client_srcs:.c=.o)\r
\r
+diff --git a/tag-util.c b/tag-util.c\r
+new file mode 100644\r
+index 0000000..164fdf0\r
+--- /dev/null\r
++++ b/tag-util.c\r
+@@ -0,0 +1,305 @@\r
++#include <assert.h>\r
++#include "string-util.h"\r
++#include "tag-util.h"\r
++#include "hex-escape.h"\r
++\r
++#define TAG_OP_LIST_INITIAL_SIZE 10\r
++\r
++struct _tag_operation_t {\r
++    const char *tag;\r
++    notmuch_bool_t remove;\r
++};\r
++\r
++struct _tag_op_list_t {\r
++    tag_operation_t *ops;\r
++    size_t count;\r
++    size_t size;\r
++};\r
++\r
++static int\r
++line_error (tag_parse_status_t status,\r
++          const char *line,\r
++          const char *format, ...)\r
++{\r
++    va_list va_args;\r
++\r
++    va_start (va_args, format);\r
++\r
++    fprintf (stderr, status < 0 ? "Error: " : "Warning: ");\r
++    vfprintf (stderr, format, va_args);\r
++    fprintf (stderr, " [%s]\n", line);\r
++    return status;\r
++}\r
++\r
++int\r
++parse_tag_line (void *ctx, char *line,\r
++              tag_op_flag_t flags,\r
++              char **query_string,\r
++              tag_op_list_t *tag_ops)\r
++{\r
++    char *tok = line;\r
++    size_t tok_len = 0;\r
++    char *line_for_error;\r
++    int ret = 0;\r
++\r
++    chomp_newline (line);\r
++\r
++    line_for_error = talloc_strdup (ctx, line);\r
++    if (line_for_error == NULL) {\r
++      fprintf (stderr, "Error: out of memory\n");\r
++      return -1;\r
++    }\r
++\r
++    /* remove leading space */\r
++    while (*tok == ' ' || *tok == '\t')\r
++      tok++;\r
++\r
++    /* Skip empty and comment lines. */\r
++    if (*tok == '\0' || *tok == '#') {\r
++      ret = 2;\r
++      goto DONE;\r
++    }\r
++\r
++    tag_op_list_reset (tag_ops);\r
++\r
++    /* Parse tags. */\r
++    while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {\r
++      notmuch_bool_t remove;\r
++      char *tag;\r
++\r
++      /* Optional explicit end of tags marker. */\r
++      if (tok_len == 2 && strncmp (tok, "--", tok_len) == 0) {\r
++          tok = strtok_len (tok + tok_len, " ", &tok_len);\r
++          if (tok == NULL) {\r
++              ret = line_error (TAG_PARSE_INVALID, line_for_error,\r
++                                "no query string after --");\r
++              goto DONE;\r
++          }\r
++          break;\r
++      }\r
++\r
++      /* Implicit end of tags. */\r
++      if (*tok != '-' && *tok != '+')\r
++          break;\r
++\r
++      /* If tag is terminated by NUL, there's no query string. */\r
++      if (*(tok + tok_len) == '\0') {\r
++          ret = line_error (TAG_PARSE_INVALID, line_for_error,\r
++                            "no query string");\r
++          goto DONE;\r
++      }\r
++\r
++      /* Terminate, and start next token after terminator. */\r
++      *(tok + tok_len++) = '\0';\r
++\r
++      remove = (*tok == '-');\r
++      tag = tok + 1;\r
++\r
++      /* Maybe refuse empty tags. */\r
++      if (! (flags & TAG_FLAG_BE_GENEROUS) && *tag == '\0') {\r
++          ret = line_error (TAG_PARSE_INVALID, line_for_error,\r
++                            "empty tag");\r
++          goto DONE;\r
++      }\r
++\r
++      /* Decode tag. */\r
++      if (hex_decode_inplace (tag) != HEX_SUCCESS) {\r
++          ret = line_error (TAG_PARSE_INVALID, line_for_error,\r
++                            "hex decoding of tag %s failed", tag);\r
++          goto DONE;\r
++      }\r
++\r
++      if (tag_op_list_append (ctx, tag_ops, tag, remove)) {\r
++          ret = line_error (TAG_PARSE_OUT_OF_MEMORY, line_for_error,\r
++                            "aborting");\r
++          goto DONE;\r
++      }\r
++    }\r
++\r
++    if (tok == NULL) {\r
++      /* use a different error message for testing */\r
++      ret = line_error (TAG_PARSE_INVALID, line_for_error,\r
++                        "missing query string");\r
++      goto DONE;\r
++    }\r
++\r
++    /* tok now points to the query string */\r
++    if (hex_decode_inplace (tok) != HEX_SUCCESS) {\r
++      ret = line_error (TAG_PARSE_INVALID, line_for_error,\r
++                        "hex decoding of query %s failed", tok);\r
++      goto DONE;\r
++    }\r
++\r
++    *query_string = tok;\r
++\r
++  DONE:\r
++    talloc_free (line_for_error);\r
++    return ret;\r
++}\r
++\r
++static inline void\r
++message_error (notmuch_message_t *message,\r
++             notmuch_status_t status,\r
++             const char *format, ...)\r
++{\r
++    va_list va_args;\r
++\r
++    va_start (va_args, format);\r
++\r
++    vfprintf (stderr, format, va_args);\r
++    fprintf (stderr, "Message-ID: %s\n", notmuch_message_get_message_id (message));\r
++    fprintf (stderr, "Status: %s\n", notmuch_status_to_string (status));\r
++}\r
++\r
++notmuch_status_t\r
++tag_op_list_apply (notmuch_message_t *message,\r
++                 tag_op_list_t *list,\r
++                 tag_op_flag_t flags)\r
++{\r
++    size_t i;\r
++    notmuch_status_t status = 0;\r
++    tag_operation_t *tag_ops = list->ops;\r
++\r
++    status = notmuch_message_freeze (message);\r
++    if (status) {\r
++      message_error (message, status, "freezing message");\r
++      return status;\r
++    }\r
++\r
++    if (flags & TAG_FLAG_REMOVE_ALL) {\r
++      status = notmuch_message_remove_all_tags (message);\r
++      if (status) {\r
++          message_error (message, status, "removing all tags");\r
++          return status;\r
++      }\r
++    }\r
++\r
++    for (i = 0; i < list->count; i++) {\r
++      if (tag_ops[i].remove) {\r
++          status = notmuch_message_remove_tag (message, tag_ops[i].tag);\r
++          if (status) {\r
++              message_error (message, status, "removing tag %s", tag_ops[i].tag);\r
++              return status;\r
++          }\r
++      } else {\r
++          status = notmuch_message_add_tag (message, tag_ops[i].tag);\r
++          if (status) {\r
++              message_error (message, status, "adding tag %s", tag_ops[i].tag);\r
++              return status;\r
++          }\r
++\r
++      }\r
++    }\r
++\r
++    status = notmuch_message_thaw (message);\r
++    if (status) {\r
++      message_error (message, status, "thawing message");\r
++      return status;\r
++    }\r
++\r
++\r
++    if (flags & TAG_FLAG_MAILDIR_SYNC) {\r
++      status = notmuch_message_tags_to_maildir_flags (message);\r
++      if (status) {\r
++          message_error (message, status, "synching tags to maildir");\r
++          return status;\r
++      }\r
++    }\r
++\r
++    return NOTMUCH_STATUS_SUCCESS;\r
++\r
++}\r
++\r
++\r
++/* Array of tagging operations (add or remove.  Size will be increased\r
++ * as necessary. */\r
++\r
++tag_op_list_t *\r
++tag_op_list_create (void *ctx)\r
++{\r
++    tag_op_list_t *list;\r
++\r
++    list = talloc (ctx, tag_op_list_t);\r
++    if (list == NULL)\r
++      return NULL;\r
++\r
++    list->size = TAG_OP_LIST_INITIAL_SIZE;\r
++    list->count = 0;\r
++\r
++    list->ops = talloc_array (ctx, tag_operation_t, list->size);\r
++    if (list->ops == NULL)\r
++      return NULL;\r
++\r
++    return list;\r
++}\r
++\r
++\r
++int\r
++tag_op_list_append (void *ctx,\r
++                  tag_op_list_t *list,\r
++                  const char *tag,\r
++                  notmuch_bool_t remove)\r
++{\r
++    /* Make room if current array is full.  This should be a fairly\r
++     * rare case, considering the initial array size.\r
++     */\r
++\r
++    if (list->count == list->size) {\r
++      list->size *= 2;\r
++      list->ops = talloc_realloc (ctx, list->ops, tag_operation_t,\r
++                                  list->size);\r
++      if (list->ops == NULL) {\r
++          fprintf (stderr, "Out of memory.\n");\r
++          return 1;\r
++      }\r
++    }\r
++\r
++    /* add the new operation */\r
++\r
++    list->ops[list->count].tag = tag;\r
++    list->ops[list->count].remove = remove;\r
++    list->count++;\r
++    return 0;\r
++}\r
++\r
++/*\r
++ *   Is the i'th tag operation a remove?\r
++ */\r
++\r
++notmuch_bool_t\r
++tag_op_list_isremove (const tag_op_list_t *list, size_t i)\r
++{\r
++    assert (i < list->count);\r
++    return list->ops[i].remove;\r
++}\r
++\r
++/*\r
++ * Reset a list to contain no operations\r
++ */\r
++\r
++void\r
++tag_op_list_reset (tag_op_list_t *list)\r
++{\r
++    list->count = 0;\r
++}\r
++\r
++/*\r
++ * Return the number of operations in a list\r
++ */\r
++\r
++size_t\r
++tag_op_list_size (const tag_op_list_t *list)\r
++{\r
++    return list->count;\r
++}\r
++\r
++/*\r
++ *   return the i'th tag in the list\r
++ */\r
++\r
++const char *\r
++tag_op_list_tag (const tag_op_list_t *list, size_t i)\r
++{\r
++    assert (i < list->count);\r
++    return list->ops[i].tag;\r
++}\r
+diff --git a/tag-util.h b/tag-util.h\r
+new file mode 100644\r
+index 0000000..99b0fa0\r
+--- /dev/null\r
++++ b/tag-util.h\r
+@@ -0,0 +1,135 @@\r
++#ifndef _TAG_UTIL_H\r
++#define _TAG_UTIL_H\r
++\r
++#include "notmuch-client.h"\r
++\r
++typedef struct _tag_operation_t tag_operation_t;\r
++typedef struct _tag_op_list_t tag_op_list_t;\r
++\r
++/* Use powers of 2 */\r
++typedef enum {\r
++    TAG_FLAG_NONE = 0,\r
++\r
++    /* Operations are synced to maildir, if possible.\r
++     */\r
++    TAG_FLAG_MAILDIR_SYNC = (1 << 0),\r
++\r
++    /* Remove all tags from message before applying list.\r
++     */\r
++    TAG_FLAG_REMOVE_ALL = (1 << 1),\r
++\r
++    /* Don't try to avoid database operations. Useful when we\r
++     * know that message passed needs these operations.\r
++     */\r
++    TAG_FLAG_PRE_OPTIMIZED = (1 << 2),\r
++\r
++    /* Accept strange tags that might be user error;\r
++     * intended for use by notmuch-restore.\r
++     */\r
++    TAG_FLAG_BE_GENEROUS = (1 << 3)\r
++\r
++} tag_op_flag_t;\r
++\r
++/* These should obey the convention that fatal errors are negative,\r
++ * skipped lines are positive.\r
++ */\r
++typedef enum {\r
++    TAG_PARSE_OUT_OF_MEMORY = -1,\r
++\r
++    /* Line parsed successfuly. */\r
++    TAG_PARSE_SUCCESS = 0,\r
++\r
++    /* Line has a syntax error */\r
++    TAG_PARSE_INVALID = 1,\r
++\r
++    /* Line was blank or a comment */\r
++    TAG_PARSE_SKIPPED = 2\r
++\r
++} tag_parse_status_t;\r
++\r
++/* Parse a string of the following format:\r
++ *\r
++ * +<tag>|-<tag> [...] [--] <search-terms>\r
++ *\r
++ * Each line is interpreted similarly to "notmuch tag" command line\r
++ * arguments. The delimiter is one or more spaces ' '. Any characters\r
++ * in <tag> and <search-terms> MAY be hex encoded with %NN where NN is\r
++ * the hexadecimal value of the character. Any ' ' and '%' characters\r
++ * in <tag> and <search-terms> MUST be hex encoded (using %20 and %25,\r
++ * respectively). Any characters that are not part of <tag> or\r
++ * <search-terms> MUST NOT be hex encoded.\r
++ *\r
++ * Leading and trailing space ' ' is ignored. Empty lines and lines\r
++ * beginning with '#' are ignored.\r
++ *\r
++ *\r
++ * Output Parameters:\r
++ *    ops     contains a list of tag operations\r
++ *    query_str the search terms.\r
++ */\r
++tag_parse_status_t\r
++parse_tag_line (void *ctx, char *line,\r
++              tag_op_flag_t flags,\r
++              char **query_str, tag_op_list_t *ops);\r
++\r
++/*\r
++ * Create an empty list of tag operations\r
++ *\r
++ * ctx is passed to talloc\r
++ */\r
++\r
++tag_op_list_t *\r
++tag_op_list_create (void *ctx);\r
++\r
++/*\r
++ * Add a tag operation (delete iff remove == TRUE) to a list.\r
++ * The list is expanded as necessary.\r
++ */\r
++\r
++int\r
++tag_op_list_append (void *ctx,\r
++                  tag_op_list_t *list,\r
++                  const char *tag,\r
++                  notmuch_bool_t remove);\r
++\r
++/*\r
++ * Apply a list of tag operations, in order, to a given message.\r
++ *\r
++ * Flags can be bitwise ORed; see enum above for possibilies.\r
++ */\r
++\r
++notmuch_status_t\r
++tag_op_list_apply (notmuch_message_t *message,\r
++                 tag_op_list_t *tag_ops,\r
++                 tag_op_flag_t flags);\r
++\r
++/*\r
++ * Return the number of operations in a list\r
++ */\r
++\r
++size_t\r
++tag_op_list_size (const tag_op_list_t *list);\r
++\r
++/*\r
++ * Reset a list to contain no operations\r
++ */\r
++\r
++void\r
++tag_op_list_reset (tag_op_list_t *list);\r
++\r
++\r
++/*\r
++ *   return the i'th tag in the list\r
++ */\r
++\r
++const char *\r
++tag_op_list_tag (const tag_op_list_t *list, size_t i);\r
++\r
++/*\r
++ *   Is the i'th tag operation a remove?\r
++ */\r
++\r
++notmuch_bool_t\r
++tag_op_list_isremove (const tag_op_list_t *list, size_t i);\r
++\r
++#endif\r
+-- \r
+1.7.10.4\r
+\r