--- /dev/null
+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 BCFC942116A\r
+ for <notmuch@notmuchmail.org>; Sat, 31 Mar 2012 15:18:14 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\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 2r-5R4GUH8Mm for <notmuch@notmuchmail.org>;\r
+ Sat, 31 Mar 2012 15:18:11 -0700 (PDT)\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 E82C5421178\r
+ for <notmuch@notmuchmail.org>; Sat, 31 Mar 2012 15:17:57 -0700 (PDT)\r
+Received: by mail-bk0-f53.google.com with SMTP id j4so1547751bkw.26\r
+ for <notmuch@notmuchmail.org>; Sat, 31 Mar 2012 15:17:57 -0700 (PDT)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=google.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references\r
+ :in-reply-to:references:x-gm-message-state;\r
+ bh=/UaCCC+hBf9uZWlbGQ8AotmRIGrXBkZdjJA2/ym6AuY=;\r
+ b=XWdvm3ozTPsWfqK9WjIoxIKHzhfV1IZ+1z8Nr6y8UMBRuDgSGt0swW4tV01qfueWKJ\r
+ b8ntEZoDfud5CpBiWpsBziiTQrcyT2IAH6tewqjdDZJQFZ6sxkdS+IWsxthdxCeKKCkz\r
+ RzUWNOkSbZ4q5Nz+B8fEpEJVUggJCqxHms61rIw2d8x80L7tLNiET29zoR+8hoHzc+Zw\r
+ OQaa0+Y0tG1etDvGbyxv1M7ejpzDzl6NlvoeB9MTUUDeyjUV6JXWKBCjsb9aRJyUx0ao\r
+ D/RIV6MlOzbf+47DgDSDcqRDsbaa9VqUiQCISho51E75Ga2g4PLD3lJI5mGu0mCnuIB5\r
+ +pOg==\r
+Received: by 10.204.133.204 with SMTP id g12mr1385964bkt.64.1333232277504;\r
+ Sat, 31 Mar 2012 15:17:57 -0700 (PDT)\r
+Received: from localhost (dsl-hkibrasgw4-fe50f800-253.dhcp.inet.fi.\r
+ [84.248.80.253]) by mx.google.com with ESMTPS id\r
+ zx16sm28521136bkb.13.2012.03.31.15.17.55\r
+ (version=SSLv3 cipher=OTHER); Sat, 31 Mar 2012 15:17:56 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 6/8] cli: add support for batch tagging operations to "notmuch\r
+ tag"\r
+Date: Sun, 1 Apr 2012 01:17:26 +0300\r
+Message-Id:\r
+ <f360a40bed50208d146aee8b06946b1b8315e818.1333231401.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <cover.1333231401.git.jani@nikula.org>\r
+References: <cover.1333231401.git.jani@nikula.org>\r
+In-Reply-To: <cover.1333231401.git.jani@nikula.org>\r
+References: <cover.1333231401.git.jani@nikula.org>\r
+X-Gm-Message-State:\r
+ ALoCoQkhctEMCFHoQJ8ESesmTYqwPfFbxYrWe0t3innvzxFR9k4AloFJpYCd7fnDhdmxfcpZ5aoG\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, 31 Mar 2012 22:18:15 -0000\r
+\r
+Add support for batch tagging operations through stdin to "notmuch\r
+tag". This can be enabled with the new --stdin command line option to\r
+"notmuch new". The input must consist of lines of the format:\r
+\r
+T +<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 in\r
+<tag> and <search-terms> MAY be hex encoded with %NN where NN is the\r
+hexadecimal ASCII 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
+Signed-off-by: Jani Nikula <jani@nikula.org>\r
+---\r
+ notmuch-tag.c | 244 +++++++++++++++++++++++++++++++++++++++++++++++++-------\r
+ 1 files changed, 213 insertions(+), 31 deletions(-)\r
+\r
+diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+index 05feed3..32c7167 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 "hex-escape.h"\r
+ \r
+ static volatile sig_atomic_t interrupted;\r
+ \r
+@@ -167,17 +168,181 @@ tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,\r
+ return interrupted;\r
+ }\r
+ \r
++/* like strtok(3), but without state, and doesn't modify s. usage pattern:\r
++ *\r
++ * const char *tok = input;\r
++ * const char *delim = " \t";\r
++ * size_t tok_len = 0;\r
++ *\r
++ * while ((tok = strtok_len (tok + tok_len, delim, &tok_len)) != NULL) {\r
++ * // do stuff with string tok of length tok_len\r
++ * }\r
++ */\r
++static\r
++char *strtok_len(char *s, const char *delim, size_t *len)\r
++{\r
++ /* skip initial delims */\r
++ s += strspn (s, delim);\r
++\r
++ /* length of token */\r
++ *len = strcspn (s, delim);\r
++\r
++ return *len ? s : NULL;\r
++}\r
++\r
++/* Tag messages according to 'input', which must consist of lines of\r
++ * the format:\r
++ *\r
++ * T +<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 ASCII value of the character. Any ' ' and '%'\r
++ * characters in <tag> and <search-terms> MUST be hex encoded (using\r
++ * %20 and %25, respectively). Any characters that are not part of\r
++ * <tag> or <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
++static int\r
++tag_file (void *ctx, notmuch_database_t *notmuch, FILE *input,\r
++ notmuch_bool_t synchronize_flags)\r
++{\r
++ char *line = NULL;\r
++ size_t line_size;\r
++ ssize_t line_len;\r
++ tag_operation_t *tag_ops;\r
++ int tag_ops_array_size = 10;\r
++ int ret = 0;\r
++\r
++ /* Array of tagging operations (add or remove), terminated with an\r
++ * empty element. Size will be increased as necessary. */\r
++ tag_ops = talloc_array (ctx, tag_operation_t, tag_ops_array_size);\r
++ if (tag_ops == NULL) {\r
++ fprintf (stderr, "Out of memory.\n");\r
++ return 1;\r
++ }\r
++\r
++ while ((line_len = getline (&line, &line_size, input)) != -1 &&\r
++ !interrupted) {\r
++ char *tok;\r
++ size_t tok_len;\r
++ int tag_ops_count = 0;\r
++\r
++ chomp_newline (line);\r
++\r
++ tok = strtok_len (line, " ", &tok_len);\r
++\r
++ /* Skip empty and comment lines. */\r
++ if (tok == NULL || *tok == '#')\r
++ continue;\r
++\r
++ /* T for tagging is the only recognized action for now. */\r
++ if (strncmp (tok, "T", tok_len) != 0) {\r
++ fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",\r
++ line);\r
++ continue;\r
++ }\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 (strncmp (tok, "--", tok_len) == 0) {\r
++ tok = strtok_len (tok + tok_len, " ", &tok_len);\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
++ tok = NULL;\r
++ break;\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
++ /* Refuse empty tags. */\r
++ if (*tag == '\0') {\r
++ tok = NULL;\r
++ break;\r
++ }\r
++\r
++ /* Decode tag. */\r
++ if (hex_decode_inplace (tag) != HEX_SUCCESS) {\r
++ tok = NULL;\r
++ break;\r
++ }\r
++\r
++ tag_ops[tag_ops_count].tag = tag;\r
++ tag_ops[tag_ops_count].remove = remove;\r
++ tag_ops_count++;\r
++\r
++ /* Make room for terminating empty element and potential\r
++ * new tags, if necessary. This should be a fairly rare\r
++ * case, considering the initial array size. */\r
++ if (tag_ops_count == tag_ops_array_size) {\r
++ tag_ops_array_size *= 2;\r
++ tag_ops = talloc_realloc (ctx, tag_ops, tag_operation_t,\r
++ tag_ops_array_size);\r
++ if (tag_ops == NULL) {\r
++ fprintf (stderr, "Out of memory.\n");\r
++ return 1;\r
++ }\r
++ }\r
++ }\r
++\r
++ if (tok == NULL || tag_ops_count == 0) {\r
++ /* FIXME: line has been modified! */\r
++ fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",\r
++ line);\r
++ continue;\r
++ }\r
++\r
++ tag_ops[tag_ops_count].tag = NULL;\r
++\r
++ /* tok now points to the query string */\r
++ if (hex_decode_inplace (tok) != HEX_SUCCESS) {\r
++ /* FIXME: line has been modified! */\r
++ fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",\r
++ line);\r
++ continue;\r
++ }\r
++\r
++ ret = tag_query (ctx, notmuch, tok, tag_ops, synchronize_flags);\r
++ if (ret)\r
++ break;\r
++ }\r
++\r
++ if (line)\r
++ free (line);\r
++\r
++ return ret || interrupted;\r
++}\r
++\r
+ int\r
+ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+ {\r
+- tag_operation_t *tag_ops;\r
++ tag_operation_t *tag_ops = NULL;\r
+ int tag_ops_count = 0;\r
+- char *query_string;\r
++ char *query_string = NULL;\r
+ notmuch_config_t *config;\r
+ notmuch_database_t *notmuch;\r
+ struct sigaction action;\r
+ notmuch_bool_t synchronize_flags;\r
+- int i;\r
++ notmuch_bool_t use_stdin = FALSE;\r
++ int i, opt_index;\r
+ int ret;\r
+ \r
+ /* Setup our handler for SIGINT */\r
+@@ -187,42 +352,56 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+ action.sa_flags = SA_RESTART;\r
+ sigaction (SIGINT, &action, NULL);\r
+ \r
+- argc--; argv++; /* skip subcommand argument */\r
++ notmuch_opt_desc_t options[] = {\r
++ { NOTMUCH_OPT_BOOLEAN, &use_stdin, "stdin", 0, 0 },\r
++ { 0, 0, 0, 0, 0 }\r
++ };\r
+ \r
+- /* Array of tagging operations (add or remove), terminated with an\r
+- * empty element. */\r
+- tag_ops = talloc_array (ctx, tag_operation_t, argc + 1);\r
+- if (tag_ops == NULL) {\r
+- fprintf (stderr, "Out of memory.\n");\r
++ opt_index = parse_arguments (argc, argv, options, 1);\r
++ if (opt_index < 0)\r
+ return 1;\r
+- }\r
+ \r
+- for (i = 0; i < argc; i++) {\r
+- if (strcmp (argv[i], "--") == 0) {\r
+- i++;\r
+- break;\r
++ if (use_stdin) {\r
++ if (opt_index != argc) {\r
++ fprintf (stderr, "Can't specify both cmdline and stdin!\n");\r
++ return 1;\r
+ }\r
+- if (argv[i][0] == '+' || argv[i][0] == '-') {\r
+- tag_ops[tag_ops_count].tag = argv[i] + 1;\r
+- tag_ops[tag_ops_count].remove = (argv[i][0] == '-');\r
+- tag_ops_count++;\r
+- } else {\r
+- break;\r
++ } else {\r
++ /* Array of tagging operations (add or remove), terminated with an\r
++ * empty element. */\r
++ tag_ops = talloc_array (ctx, tag_operation_t, argc - opt_index + 1);\r
++ if (tag_ops == NULL) {\r
++ fprintf (stderr, "Out of memory.\n");\r
++ return 1;\r
+ }\r
+- }\r
+ \r
+- tag_ops[tag_ops_count].tag = NULL;\r
++ for (i = opt_index; i < argc; i++) {\r
++ if (strcmp (argv[i], "--") == 0) {\r
++ i++;\r
++ break;\r
++ }\r
++ if (argv[i][0] == '+' || argv[i][0] == '-') {\r
++ tag_ops[tag_ops_count].tag = argv[i] + 1;\r
++ tag_ops[tag_ops_count].remove = (argv[i][0] == '-');\r
++ tag_ops_count++;\r
++ } else {\r
++ break;\r
++ }\r
++ }\r
+ \r
+- if (tag_ops_count == 0) {\r
+- fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");\r
+- return 1;\r
+- }\r
++ tag_ops[tag_ops_count].tag = NULL;\r
+ \r
+- query_string = query_string_from_args (ctx, argc - i, &argv[i]);\r
++ if (tag_ops_count == 0) {\r
++ fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");\r
++ return 1;\r
++ }\r
+ \r
+- if (*query_string == '\0') {\r
+- fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");\r
+- return 1;\r
++ query_string = query_string_from_args (ctx, argc - i, &argv[i]);\r
++\r
++ if (*query_string == '\0') {\r
++ fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");\r
++ return 1;\r
++ }\r
+ }\r
+ \r
+ config = notmuch_config_open (ctx, NULL, NULL);\r
+@@ -236,7 +415,10 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+ \r
+ synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
+ \r
+- ret = tag_query (ctx, notmuch, query_string, tag_ops, synchronize_flags);\r
++ if (use_stdin)\r
++ ret = tag_file (ctx, notmuch, stdin, synchronize_flags);\r
++ else\r
++ ret = tag_query (ctx, notmuch, query_string, tag_ops, synchronize_flags);\r
+ \r
+ notmuch_database_close (notmuch);\r
+ \r
+-- \r
+1.7.5.4\r
+\r