2 #include "string-util.h"
4 #include "hex-escape.h"
6 #define TAG_OP_LIST_INITIAL_SIZE 10
8 struct _tag_operation_t {
10 notmuch_bool_t remove;
13 struct _tag_op_list_t {
19 static tag_parse_status_t
20 line_error (tag_parse_status_t status,
22 const char *format, ...)
26 va_start (va_args, format);
28 fprintf (stderr, status < 0 ? "Error: " : "Warning: ");
29 vfprintf (stderr, format, va_args);
30 fprintf (stderr, " [%s]\n", line);
38 illegal_tag (const char *tag, notmuch_bool_t remove)
40 if (*tag == '\0' && ! remove)
41 return "empty tag forbidden";
43 /* This disallows adding tags starting with "-", in particular the
44 * non-removable tag "-" and enables notmuch tag to take long
45 * options more easily.
48 if (*tag == '-' && ! remove)
49 return "tag starting with '-' forbidden";
55 parse_tag_line (void *ctx, char *line,
58 tag_op_list_t *tag_ops)
63 tag_parse_status_t ret = TAG_PARSE_SUCCESS;
67 line_for_error = talloc_strdup (ctx, line);
68 if (line_for_error == NULL) {
69 fprintf (stderr, "Error: out of memory\n");
70 return TAG_PARSE_OUT_OF_MEMORY;
73 /* remove leading space */
74 while (*tok == ' ' || *tok == '\t')
77 /* Skip empty and comment lines. */
78 if (*tok == '\0' || *tok == '#') {
79 ret = TAG_PARSE_SKIPPED;
83 tag_op_list_reset (tag_ops);
86 while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {
87 notmuch_bool_t remove;
90 /* Optional explicit end of tags marker. */
91 if (tok_len == 2 && strncmp (tok, "--", tok_len) == 0) {
92 tok = strtok_len (tok + tok_len, " ", &tok_len);
94 ret = line_error (TAG_PARSE_INVALID, line_for_error,
95 "no query string after --");
101 /* Implicit end of tags. */
102 if (*tok != '-' && *tok != '+')
105 /* If tag is terminated by NUL, there's no query string. */
106 if (*(tok + tok_len) == '\0') {
107 ret = line_error (TAG_PARSE_INVALID, line_for_error,
112 /* Terminate, and start next token after terminator. */
113 *(tok + tok_len++) = '\0';
115 remove = (*tok == '-');
118 /* Maybe refuse illegal tags. */
119 if (! (flags & TAG_FLAG_BE_GENEROUS)) {
120 const char *msg = illegal_tag (tag, remove);
122 ret = line_error (TAG_PARSE_INVALID, line_for_error, msg);
128 if (hex_decode_inplace (tag) != HEX_SUCCESS) {
129 ret = line_error (TAG_PARSE_INVALID, line_for_error,
130 "hex decoding of tag %s failed", tag);
134 if (tag_op_list_append (tag_ops, tag, remove)) {
135 ret = line_error (TAG_PARSE_OUT_OF_MEMORY, line_for_error,
142 /* use a different error message for testing */
143 ret = line_error (TAG_PARSE_INVALID, line_for_error,
144 "missing query string");
148 /* tok now points to the query string */
152 talloc_free (line_for_error);
157 parse_tag_command_line (void *ctx, int argc, char **argv,
158 char **query_str, tag_op_list_t *tag_ops)
162 for (i = 0; i < argc; i++) {
163 if (strcmp (argv[i], "--") == 0) {
168 if (argv[i][0] != '+' && argv[i][0] != '-')
171 notmuch_bool_t is_remove = argv[i][0] == '-';
174 msg = illegal_tag (argv[i] + 1, is_remove);
176 fprintf (stderr, "Error: %s\n", msg);
177 return TAG_PARSE_INVALID;
180 tag_op_list_append (tag_ops, argv[i] + 1, is_remove);
183 *query_str = query_string_from_args (ctx, argc - i, &argv[i]);
185 if (*query_str == NULL) {
186 fprintf (stderr, "Out of memory.\n");
187 return TAG_PARSE_OUT_OF_MEMORY;
190 return TAG_PARSE_SUCCESS;
195 message_error (notmuch_message_t *message,
196 notmuch_status_t status,
197 const char *format, ...)
201 va_start (va_args, format);
203 vfprintf (stderr, format, va_args);
204 fprintf (stderr, "Message-ID: %s\n", notmuch_message_get_message_id (message));
205 fprintf (stderr, "Status: %s\n", notmuch_status_to_string (status));
211 makes_changes (notmuch_message_t *message,
217 notmuch_tags_t *tags;
218 notmuch_bool_t changes = FALSE;
220 /* First, do we delete an existing tag? */
222 for (tags = notmuch_message_get_tags (message);
223 ! changes && notmuch_tags_valid (tags);
224 notmuch_tags_move_to_next (tags)) {
225 const char *cur_tag = notmuch_tags_get (tags);
226 int last_op = (flags & TAG_FLAG_REMOVE_ALL) ? -1 : 0;
228 /* scan backwards to get last operation */
232 if (strcmp (cur_tag, list->ops[i].tag) == 0) {
233 last_op = list->ops[i].remove ? -1 : 1;
238 changes = (last_op == -1);
240 notmuch_tags_destroy (tags);
245 /* Now check for adding new tags */
246 for (i = 0; i < list->count; i++) {
247 notmuch_bool_t exists = FALSE;
249 if (list->ops[i].remove)
252 for (tags = notmuch_message_get_tags (message);
253 notmuch_tags_valid (tags);
254 notmuch_tags_move_to_next (tags)) {
255 const char *cur_tag = notmuch_tags_get (tags);
256 if (strcmp (cur_tag, list->ops[i].tag) == 0) {
261 notmuch_tags_destroy (tags);
263 /* the following test is conservative,
264 * in the sense it ignores cases like +foo ... -foo
265 * but this is OK from a correctness point of view
275 tag_op_list_apply (notmuch_message_t *message,
280 notmuch_status_t status = 0;
281 tag_operation_t *tag_ops = list->ops;
283 if (! (flags & TAG_FLAG_PRE_OPTIMIZED) && ! makes_changes (message, list, flags))
284 return NOTMUCH_STATUS_SUCCESS;
286 status = notmuch_message_freeze (message);
288 message_error (message, status, "freezing message");
292 if (flags & TAG_FLAG_REMOVE_ALL) {
293 status = notmuch_message_remove_all_tags (message);
295 message_error (message, status, "removing all tags");
300 for (i = 0; i < list->count; i++) {
301 if (tag_ops[i].remove) {
302 status = notmuch_message_remove_tag (message, tag_ops[i].tag);
304 message_error (message, status, "removing tag %s", tag_ops[i].tag);
308 status = notmuch_message_add_tag (message, tag_ops[i].tag);
310 message_error (message, status, "adding tag %s", tag_ops[i].tag);
317 status = notmuch_message_thaw (message);
319 message_error (message, status, "thawing message");
324 if (flags & TAG_FLAG_MAILDIR_SYNC) {
325 status = notmuch_message_tags_to_maildir_flags (message);
327 message_error (message, status, "synching tags to maildir");
332 return NOTMUCH_STATUS_SUCCESS;
337 /* Array of tagging operations (add or remove. Size will be increased
341 tag_op_list_create (void *ctx)
345 list = talloc (ctx, tag_op_list_t);
349 list->size = TAG_OP_LIST_INITIAL_SIZE;
352 list->ops = talloc_array (list, tag_operation_t, list->size);
353 if (list->ops == NULL)
361 tag_op_list_append (tag_op_list_t *list,
363 notmuch_bool_t remove)
365 /* Make room if current array is full. This should be a fairly
366 * rare case, considering the initial array size.
369 if (list->count == list->size) {
371 list->ops = talloc_realloc (list, list->ops, tag_operation_t,
373 if (list->ops == NULL) {
374 fprintf (stderr, "Out of memory.\n");
379 /* add the new operation */
381 list->ops[list->count].tag = tag;
382 list->ops[list->count].remove = remove;
388 * Is the i'th tag operation a remove?
392 tag_op_list_isremove (const tag_op_list_t *list, size_t i)
394 assert (i < list->count);
395 return list->ops[i].remove;
399 * Reset a list to contain no operations
403 tag_op_list_reset (tag_op_list_t *list)
409 * Return the number of operations in a list
413 tag_op_list_size (const tag_op_list_t *list)
419 * return the i'th tag in the list
423 tag_op_list_tag (const tag_op_list_t *list, size_t i)
425 assert (i < list->count);
426 return list->ops[i].tag;