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 parse_tag_line (void *ctx, char *line,
38 tag_op_list_t *tag_ops)
43 tag_parse_status_t ret = TAG_PARSE_SUCCESS;
47 line_for_error = talloc_strdup (ctx, line);
48 if (line_for_error == NULL) {
49 fprintf (stderr, "Error: out of memory\n");
50 return TAG_PARSE_OUT_OF_MEMORY;
53 /* remove leading space */
54 while (*tok == ' ' || *tok == '\t')
57 /* Skip empty and comment lines. */
58 if (*tok == '\0' || *tok == '#') {
59 ret = TAG_PARSE_SKIPPED;
63 tag_op_list_reset (tag_ops);
66 while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {
67 notmuch_bool_t remove;
70 /* Optional explicit end of tags marker. */
71 if (tok_len == 2 && strncmp (tok, "--", tok_len) == 0) {
72 tok = strtok_len (tok + tok_len, " ", &tok_len);
74 ret = line_error (TAG_PARSE_INVALID, line_for_error,
75 "no query string after --");
81 /* Implicit end of tags. */
82 if (*tok != '-' && *tok != '+')
85 /* If tag is terminated by NUL, there's no query string. */
86 if (*(tok + tok_len) == '\0') {
87 ret = line_error (TAG_PARSE_INVALID, line_for_error,
92 /* Terminate, and start next token after terminator. */
93 *(tok + tok_len++) = '\0';
95 remove = (*tok == '-');
98 /* Maybe refuse empty tags. */
99 if (! (flags & TAG_FLAG_BE_GENEROUS) && *tag == '\0') {
100 ret = line_error (TAG_PARSE_INVALID, line_for_error,
106 if (hex_decode_inplace (tag) != HEX_SUCCESS) {
107 ret = line_error (TAG_PARSE_INVALID, line_for_error,
108 "hex decoding of tag %s failed", tag);
112 if (tag_op_list_append (tag_ops, tag, remove)) {
113 ret = line_error (TAG_PARSE_OUT_OF_MEMORY, line_for_error,
120 /* use a different error message for testing */
121 ret = line_error (TAG_PARSE_INVALID, line_for_error,
122 "missing query string");
126 /* tok now points to the query string */
127 if (hex_decode_inplace (tok) != HEX_SUCCESS) {
128 ret = line_error (TAG_PARSE_INVALID, line_for_error,
129 "hex decoding of query %s failed", tok);
136 talloc_free (line_for_error);
141 message_error (notmuch_message_t *message,
142 notmuch_status_t status,
143 const char *format, ...)
147 va_start (va_args, format);
149 vfprintf (stderr, format, va_args);
150 fprintf (stderr, "Message-ID: %s\n", notmuch_message_get_message_id (message));
151 fprintf (stderr, "Status: %s\n", notmuch_status_to_string (status));
155 makes_changes (notmuch_message_t *message,
162 notmuch_tags_t *tags;
163 notmuch_bool_t changes = FALSE;
165 /* First, do we delete an existing tag? */
167 for (tags = notmuch_message_get_tags (message);
168 ! changes && notmuch_tags_valid (tags);
169 notmuch_tags_move_to_next (tags)) {
170 const char *cur_tag = notmuch_tags_get (tags);
171 int last_op = (flags & TAG_FLAG_REMOVE_ALL) ? -1 : 0;
173 /* scan backwards to get last operation */
177 if (strcmp (cur_tag, list->ops[i].tag) == 0) {
178 last_op = list->ops[i].remove ? -1 : 1;
183 changes = (last_op == -1);
185 notmuch_tags_destroy (tags);
190 /* Now check for adding new tags */
191 for (i = 0; i < list->count; i++) {
192 notmuch_bool_t exists = FALSE;
194 if (list->ops[i].remove)
197 for (tags = notmuch_message_get_tags (message);
198 notmuch_tags_valid (tags);
199 notmuch_tags_move_to_next (tags)) {
200 const char *cur_tag = notmuch_tags_get (tags);
201 if (strcmp (cur_tag, list->ops[i].tag) == 0) {
206 notmuch_tags_destroy (tags);
208 /* the following test is conservative,
209 * in the sense it ignores cases like +foo ... -foo
210 * but this is OK from a correctness point of view
220 tag_op_list_apply (notmuch_message_t *message,
225 notmuch_status_t status = 0;
226 tag_operation_t *tag_ops = list->ops;
228 if (! (flags & TAG_FLAG_PRE_OPTIMIZED) && ! makes_changes (message, list, flags))
229 return NOTMUCH_STATUS_SUCCESS;
231 status = notmuch_message_freeze (message);
233 message_error (message, status, "freezing message");
237 if (flags & TAG_FLAG_REMOVE_ALL) {
238 status = notmuch_message_remove_all_tags (message);
240 message_error (message, status, "removing all tags");
245 for (i = 0; i < list->count; i++) {
246 if (tag_ops[i].remove) {
247 status = notmuch_message_remove_tag (message, tag_ops[i].tag);
249 message_error (message, status, "removing tag %s", tag_ops[i].tag);
253 status = notmuch_message_add_tag (message, tag_ops[i].tag);
255 message_error (message, status, "adding tag %s", tag_ops[i].tag);
262 status = notmuch_message_thaw (message);
264 message_error (message, status, "thawing message");
269 if (flags & TAG_FLAG_MAILDIR_SYNC) {
270 status = notmuch_message_tags_to_maildir_flags (message);
272 message_error (message, status, "synching tags to maildir");
277 return NOTMUCH_STATUS_SUCCESS;
282 /* Array of tagging operations (add or remove. Size will be increased
286 tag_op_list_create (void *ctx)
290 list = talloc (ctx, tag_op_list_t);
294 list->size = TAG_OP_LIST_INITIAL_SIZE;
297 list->ops = talloc_array (list, tag_operation_t, list->size);
298 if (list->ops == NULL)
306 tag_op_list_append (tag_op_list_t *list,
308 notmuch_bool_t remove)
310 /* Make room if current array is full. This should be a fairly
311 * rare case, considering the initial array size.
314 if (list->count == list->size) {
316 list->ops = talloc_realloc (list, list->ops, tag_operation_t,
318 if (list->ops == NULL) {
319 fprintf (stderr, "Out of memory.\n");
324 /* add the new operation */
326 list->ops[list->count].tag = tag;
327 list->ops[list->count].remove = remove;
333 * Is the i'th tag operation a remove?
337 tag_op_list_isremove (const tag_op_list_t *list, size_t i)
339 assert (i < list->count);
340 return list->ops[i].remove;
344 * Reset a list to contain no operations
348 tag_op_list_reset (tag_op_list_t *list)
354 * Return the number of operations in a list
358 tag_op_list_size (const tag_op_list_t *list)
364 * return the i'th tag in the list
368 tag_op_list_tag (const tag_op_list_t *list, size_t i)
370 assert (i < list->count);
371 return list->ops[i].tag;