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);
35 * Test tags for some forbidden cases.
38 * explanatory message otherwise.
42 illegal_tag (const char *tag, notmuch_bool_t remove)
45 if (*tag == '\0' && ! remove)
46 return "empty tag forbidden";
48 /* This disallows adding tags starting with "-", in particular the
49 * non-removable tag "-" and enables notmuch tag to take long
50 * options more easily.
53 if (*tag == '-' && ! remove)
54 return "tag starting with '-' forbidden";
60 parse_tag_line (void *ctx, char *line,
63 tag_op_list_t *tag_ops)
68 tag_parse_status_t ret = TAG_PARSE_SUCCESS;
72 line_for_error = talloc_strdup (ctx, line);
73 if (line_for_error == NULL) {
74 fprintf (stderr, "Error: out of memory\n");
75 return TAG_PARSE_OUT_OF_MEMORY;
78 /* remove leading space */
79 while (*tok == ' ' || *tok == '\t')
82 /* Skip empty and comment lines. */
83 if (*tok == '\0' || *tok == '#') {
84 ret = TAG_PARSE_SKIPPED;
88 tag_op_list_reset (tag_ops);
91 while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {
92 notmuch_bool_t remove;
95 /* Optional explicit end of tags marker. */
96 if (tok_len == 2 && strncmp (tok, "--", tok_len) == 0) {
97 tok = strtok_len (tok + tok_len, " ", &tok_len);
99 ret = line_error (TAG_PARSE_INVALID, line_for_error,
100 "no query string after --");
106 /* Implicit end of tags. */
107 if (*tok != '-' && *tok != '+')
110 /* If tag is terminated by NUL, there's no query string. */
111 if (*(tok + tok_len) == '\0') {
112 ret = line_error (TAG_PARSE_INVALID, line_for_error,
117 /* Terminate, and start next token after terminator. */
118 *(tok + tok_len++) = '\0';
120 remove = (*tok == '-');
123 /* Maybe refuse illegal tags. */
124 if (! (flags & TAG_FLAG_BE_GENEROUS)) {
125 const char *msg = illegal_tag (tag, remove);
127 ret = line_error (TAG_PARSE_INVALID, line_for_error, msg);
133 if (hex_decode_inplace (tag) != HEX_SUCCESS) {
134 ret = line_error (TAG_PARSE_INVALID, line_for_error,
135 "hex decoding of tag %s failed", tag);
139 if (tag_op_list_append (tag_ops, tag, remove)) {
140 ret = line_error (TAG_PARSE_OUT_OF_MEMORY, line_for_error,
147 /* use a different error message for testing */
148 ret = line_error (TAG_PARSE_INVALID, line_for_error,
149 "missing query string");
153 /* tok now points to the query string */
157 talloc_free (line_for_error);
162 parse_tag_command_line (void *ctx, int argc, char **argv,
163 char **query_str, tag_op_list_t *tag_ops)
168 for (i = 0; i < argc; i++) {
169 if (strcmp (argv[i], "--") == 0) {
174 if (argv[i][0] != '+' && argv[i][0] != '-')
177 notmuch_bool_t is_remove = argv[i][0] == '-';
180 msg = illegal_tag (argv[i] + 1, is_remove);
182 fprintf (stderr, "Error: %s", msg);
183 return TAG_PARSE_INVALID;
186 tag_op_list_append (tag_ops, argv[i] + 1, is_remove);
189 *query_str = query_string_from_args (ctx, argc - i, &argv[i]);
191 if (*query_str == NULL) {
192 fprintf (stderr, "Out of memory.\n");
193 return TAG_PARSE_OUT_OF_MEMORY;
196 return TAG_PARSE_SUCCESS;
201 message_error (notmuch_message_t *message,
202 notmuch_status_t status,
203 const char *format, ...)
207 va_start (va_args, format);
209 vfprintf (stderr, format, va_args);
210 fprintf (stderr, "Message-ID: %s\n", notmuch_message_get_message_id (message));
211 fprintf (stderr, "Status: %s\n", notmuch_status_to_string (status));
215 makes_changes (notmuch_message_t *message,
222 notmuch_tags_t *tags;
223 notmuch_bool_t changes = FALSE;
225 /* First, do we delete an existing tag? */
227 for (tags = notmuch_message_get_tags (message);
228 ! changes && notmuch_tags_valid (tags);
229 notmuch_tags_move_to_next (tags)) {
230 const char *cur_tag = notmuch_tags_get (tags);
231 int last_op = (flags & TAG_FLAG_REMOVE_ALL) ? -1 : 0;
233 /* scan backwards to get last operation */
237 if (strcmp (cur_tag, list->ops[i].tag) == 0) {
238 last_op = list->ops[i].remove ? -1 : 1;
243 changes = (last_op == -1);
245 notmuch_tags_destroy (tags);
250 /* Now check for adding new tags */
251 for (i = 0; i < list->count; i++) {
252 notmuch_bool_t exists = FALSE;
254 if (list->ops[i].remove)
257 for (tags = notmuch_message_get_tags (message);
258 notmuch_tags_valid (tags);
259 notmuch_tags_move_to_next (tags)) {
260 const char *cur_tag = notmuch_tags_get (tags);
261 if (strcmp (cur_tag, list->ops[i].tag) == 0) {
266 notmuch_tags_destroy (tags);
268 /* the following test is conservative,
269 * in the sense it ignores cases like +foo ... -foo
270 * but this is OK from a correctness point of view
280 tag_op_list_apply (notmuch_message_t *message,
285 notmuch_status_t status = 0;
286 tag_operation_t *tag_ops = list->ops;
288 if (! (flags & TAG_FLAG_PRE_OPTIMIZED) && ! makes_changes (message, list, flags))
289 return NOTMUCH_STATUS_SUCCESS;
291 status = notmuch_message_freeze (message);
293 message_error (message, status, "freezing message");
297 if (flags & TAG_FLAG_REMOVE_ALL) {
298 status = notmuch_message_remove_all_tags (message);
300 message_error (message, status, "removing all tags");
305 for (i = 0; i < list->count; i++) {
306 if (tag_ops[i].remove) {
307 status = notmuch_message_remove_tag (message, tag_ops[i].tag);
309 message_error (message, status, "removing tag %s", tag_ops[i].tag);
313 status = notmuch_message_add_tag (message, tag_ops[i].tag);
315 message_error (message, status, "adding tag %s", tag_ops[i].tag);
322 status = notmuch_message_thaw (message);
324 message_error (message, status, "thawing message");
329 if (flags & TAG_FLAG_MAILDIR_SYNC) {
330 status = notmuch_message_tags_to_maildir_flags (message);
332 message_error (message, status, "synching tags to maildir");
337 return NOTMUCH_STATUS_SUCCESS;
342 /* Array of tagging operations (add or remove. Size will be increased
346 tag_op_list_create (void *ctx)
350 list = talloc (ctx, tag_op_list_t);
354 list->size = TAG_OP_LIST_INITIAL_SIZE;
357 list->ops = talloc_array (list, tag_operation_t, list->size);
358 if (list->ops == NULL)
366 tag_op_list_append (tag_op_list_t *list,
368 notmuch_bool_t remove)
370 /* Make room if current array is full. This should be a fairly
371 * rare case, considering the initial array size.
374 if (list->count == list->size) {
376 list->ops = talloc_realloc (list, list->ops, tag_operation_t,
378 if (list->ops == NULL) {
379 fprintf (stderr, "Out of memory.\n");
384 /* add the new operation */
386 list->ops[list->count].tag = tag;
387 list->ops[list->count].remove = remove;
393 * Is the i'th tag operation a remove?
397 tag_op_list_isremove (const tag_op_list_t *list, size_t i)
399 assert (i < list->count);
400 return list->ops[i].remove;
404 * Reset a list to contain no operations
408 tag_op_list_reset (tag_op_list_t *list)
414 * Return the number of operations in a list
418 tag_op_list_size (const tag_op_list_t *list)
424 * return the i'th tag in the list
428 tag_op_list_tag (const tag_op_list_t *list, size_t i)
430 assert (i < list->count);
431 return list->ops[i].tag;