[PATCH 6/8] cli: add support for batch tagging operations to "notmuch tag"
Jani Nikula
jani at nikula.org
Sat Mar 31 15:17:26 PDT 2012
Add support for batch tagging operations through stdin to "notmuch
tag". This can be enabled with the new --stdin command line option to
"notmuch new". The input must consist of lines of the format:
T +<tag>|-<tag> [...] [--] <search-terms>
Each line is interpreted similarly to "notmuch tag" command line
arguments. The delimiter is one or more spaces ' '. Any characters in
<tag> and <search-terms> MAY be hex encoded with %NN where NN is the
hexadecimal ASCII value of the character. Any ' ' and '%' characters
in <tag> and <search-terms> MUST be hex encoded (using %20 and %25,
respectively). Any characters that are not part of <tag> or
<search-terms> MUST NOT be hex encoded.
Leading and trailing space ' ' is ignored. Empty lines and lines
beginning with '#' are ignored.
Signed-off-by: Jani Nikula <jani at nikula.org>
---
notmuch-tag.c | 244 +++++++++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 213 insertions(+), 31 deletions(-)
diff --git a/notmuch-tag.c b/notmuch-tag.c
index 05feed3..32c7167 100644
--- a/notmuch-tag.c
+++ b/notmuch-tag.c
@@ -19,6 +19,7 @@
*/
#include "notmuch-client.h"
+#include "hex-escape.h"
static volatile sig_atomic_t interrupted;
@@ -167,17 +168,181 @@ tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,
return interrupted;
}
+/* like strtok(3), but without state, and doesn't modify s. usage pattern:
+ *
+ * const char *tok = input;
+ * const char *delim = " \t";
+ * size_t tok_len = 0;
+ *
+ * while ((tok = strtok_len (tok + tok_len, delim, &tok_len)) != NULL) {
+ * // do stuff with string tok of length tok_len
+ * }
+ */
+static
+char *strtok_len(char *s, const char *delim, size_t *len)
+{
+ /* skip initial delims */
+ s += strspn (s, delim);
+
+ /* length of token */
+ *len = strcspn (s, delim);
+
+ return *len ? s : NULL;
+}
+
+/* Tag messages according to 'input', which must consist of lines of
+ * the format:
+ *
+ * T +<tag>|-<tag> [...] [--] <search-terms>
+ *
+ * Each line is interpreted similarly to "notmuch tag" command line
+ * arguments. The delimiter is one or more spaces ' '. Any characters
+ * in <tag> and <search-terms> MAY be hex encoded with %NN where NN is
+ * the hexadecimal ASCII value of the character. Any ' ' and '%'
+ * characters in <tag> and <search-terms> MUST be hex encoded (using
+ * %20 and %25, respectively). Any characters that are not part of
+ * <tag> or <search-terms> MUST NOT be hex encoded.
+ *
+ * Leading and trailing space ' ' is ignored. Empty lines and lines
+ * beginning with '#' are ignored.
+ */
+static int
+tag_file (void *ctx, notmuch_database_t *notmuch, FILE *input,
+ notmuch_bool_t synchronize_flags)
+{
+ char *line = NULL;
+ size_t line_size;
+ ssize_t line_len;
+ tag_operation_t *tag_ops;
+ int tag_ops_array_size = 10;
+ int ret = 0;
+
+ /* Array of tagging operations (add or remove), terminated with an
+ * empty element. Size will be increased as necessary. */
+ tag_ops = talloc_array (ctx, tag_operation_t, tag_ops_array_size);
+ if (tag_ops == NULL) {
+ fprintf (stderr, "Out of memory.\n");
+ return 1;
+ }
+
+ while ((line_len = getline (&line, &line_size, input)) != -1 &&
+ !interrupted) {
+ char *tok;
+ size_t tok_len;
+ int tag_ops_count = 0;
+
+ chomp_newline (line);
+
+ tok = strtok_len (line, " ", &tok_len);
+
+ /* Skip empty and comment lines. */
+ if (tok == NULL || *tok == '#')
+ continue;
+
+ /* T for tagging is the only recognized action for now. */
+ if (strncmp (tok, "T", tok_len) != 0) {
+ fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",
+ line);
+ continue;
+ }
+
+ /* Parse tags. */
+ while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {
+ notmuch_bool_t remove;
+ char *tag;
+
+ /* Optional explicit end of tags marker. */
+ if (strncmp (tok, "--", tok_len) == 0) {
+ tok = strtok_len (tok + tok_len, " ", &tok_len);
+ break;
+ }
+
+ /* Implicit end of tags. */
+ if (*tok != '-' && *tok != '+')
+ break;
+
+ /* If tag is terminated by NUL, there's no query string. */
+ if (*(tok + tok_len) == '\0') {
+ tok = NULL;
+ break;
+ }
+
+ /* Terminate, and start next token after terminator. */
+ *(tok + tok_len++) = '\0';
+
+ remove = (*tok == '-');
+ tag = tok + 1;
+
+ /* Refuse empty tags. */
+ if (*tag == '\0') {
+ tok = NULL;
+ break;
+ }
+
+ /* Decode tag. */
+ if (hex_decode_inplace (tag) != HEX_SUCCESS) {
+ tok = NULL;
+ break;
+ }
+
+ tag_ops[tag_ops_count].tag = tag;
+ tag_ops[tag_ops_count].remove = remove;
+ tag_ops_count++;
+
+ /* Make room for terminating empty element and potential
+ * new tags, if necessary. This should be a fairly rare
+ * case, considering the initial array size. */
+ if (tag_ops_count == tag_ops_array_size) {
+ tag_ops_array_size *= 2;
+ tag_ops = talloc_realloc (ctx, tag_ops, tag_operation_t,
+ tag_ops_array_size);
+ if (tag_ops == NULL) {
+ fprintf (stderr, "Out of memory.\n");
+ return 1;
+ }
+ }
+ }
+
+ if (tok == NULL || tag_ops_count == 0) {
+ /* FIXME: line has been modified! */
+ fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",
+ line);
+ continue;
+ }
+
+ tag_ops[tag_ops_count].tag = NULL;
+
+ /* tok now points to the query string */
+ if (hex_decode_inplace (tok) != HEX_SUCCESS) {
+ /* FIXME: line has been modified! */
+ fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",
+ line);
+ continue;
+ }
+
+ ret = tag_query (ctx, notmuch, tok, tag_ops, synchronize_flags);
+ if (ret)
+ break;
+ }
+
+ if (line)
+ free (line);
+
+ return ret || interrupted;
+}
+
int
notmuch_tag_command (void *ctx, int argc, char *argv[])
{
- tag_operation_t *tag_ops;
+ tag_operation_t *tag_ops = NULL;
int tag_ops_count = 0;
- char *query_string;
+ char *query_string = NULL;
notmuch_config_t *config;
notmuch_database_t *notmuch;
struct sigaction action;
notmuch_bool_t synchronize_flags;
- int i;
+ notmuch_bool_t use_stdin = FALSE;
+ int i, opt_index;
int ret;
/* Setup our handler for SIGINT */
@@ -187,42 +352,56 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])
action.sa_flags = SA_RESTART;
sigaction (SIGINT, &action, NULL);
- argc--; argv++; /* skip subcommand argument */
+ notmuch_opt_desc_t options[] = {
+ { NOTMUCH_OPT_BOOLEAN, &use_stdin, "stdin", 0, 0 },
+ { 0, 0, 0, 0, 0 }
+ };
- /* Array of tagging operations (add or remove), terminated with an
- * empty element. */
- tag_ops = talloc_array (ctx, tag_operation_t, argc + 1);
- if (tag_ops == NULL) {
- fprintf (stderr, "Out of memory.\n");
+ opt_index = parse_arguments (argc, argv, options, 1);
+ if (opt_index < 0)
return 1;
- }
- for (i = 0; i < argc; i++) {
- if (strcmp (argv[i], "--") == 0) {
- i++;
- break;
+ if (use_stdin) {
+ if (opt_index != argc) {
+ fprintf (stderr, "Can't specify both cmdline and stdin!\n");
+ return 1;
}
- if (argv[i][0] == '+' || argv[i][0] == '-') {
- tag_ops[tag_ops_count].tag = argv[i] + 1;
- tag_ops[tag_ops_count].remove = (argv[i][0] == '-');
- tag_ops_count++;
- } else {
- break;
+ } else {
+ /* Array of tagging operations (add or remove), terminated with an
+ * empty element. */
+ tag_ops = talloc_array (ctx, tag_operation_t, argc - opt_index + 1);
+ if (tag_ops == NULL) {
+ fprintf (stderr, "Out of memory.\n");
+ return 1;
}
- }
- tag_ops[tag_ops_count].tag = NULL;
+ for (i = opt_index; i < argc; i++) {
+ if (strcmp (argv[i], "--") == 0) {
+ i++;
+ break;
+ }
+ if (argv[i][0] == '+' || argv[i][0] == '-') {
+ tag_ops[tag_ops_count].tag = argv[i] + 1;
+ tag_ops[tag_ops_count].remove = (argv[i][0] == '-');
+ tag_ops_count++;
+ } else {
+ break;
+ }
+ }
- if (tag_ops_count == 0) {
- fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
- return 1;
- }
+ tag_ops[tag_ops_count].tag = NULL;
- query_string = query_string_from_args (ctx, argc - i, &argv[i]);
+ if (tag_ops_count == 0) {
+ fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
+ return 1;
+ }
- if (*query_string == '\0') {
- fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");
- return 1;
+ query_string = query_string_from_args (ctx, argc - i, &argv[i]);
+
+ if (*query_string == '\0') {
+ fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");
+ return 1;
+ }
}
config = notmuch_config_open (ctx, NULL, NULL);
@@ -236,7 +415,10 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])
synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
- ret = tag_query (ctx, notmuch, query_string, tag_ops, synchronize_flags);
+ if (use_stdin)
+ ret = tag_file (ctx, notmuch, stdin, synchronize_flags);
+ else
+ ret = tag_query (ctx, notmuch, query_string, tag_ops, synchronize_flags);
notmuch_database_close (notmuch);
--
1.7.5.4
More information about the notmuch
mailing list