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 */
130 talloc_free (line_for_error);
135 message_error (notmuch_message_t *message,
136 notmuch_status_t status,
137 const char *format, ...)
141 va_start (va_args, format);
143 vfprintf (stderr, format, va_args);
144 fprintf (stderr, "Message-ID: %s\n", notmuch_message_get_message_id (message));
145 fprintf (stderr, "Status: %s\n", notmuch_status_to_string (status));
149 makes_changes (notmuch_message_t *message,
156 notmuch_tags_t *tags;
157 notmuch_bool_t changes = FALSE;
159 /* First, do we delete an existing tag? */
161 for (tags = notmuch_message_get_tags (message);
162 ! changes && notmuch_tags_valid (tags);
163 notmuch_tags_move_to_next (tags)) {
164 const char *cur_tag = notmuch_tags_get (tags);
165 int last_op = (flags & TAG_FLAG_REMOVE_ALL) ? -1 : 0;
167 /* scan backwards to get last operation */
171 if (strcmp (cur_tag, list->ops[i].tag) == 0) {
172 last_op = list->ops[i].remove ? -1 : 1;
177 changes = (last_op == -1);
179 notmuch_tags_destroy (tags);
184 /* Now check for adding new tags */
185 for (i = 0; i < list->count; i++) {
186 notmuch_bool_t exists = FALSE;
188 if (list->ops[i].remove)
191 for (tags = notmuch_message_get_tags (message);
192 notmuch_tags_valid (tags);
193 notmuch_tags_move_to_next (tags)) {
194 const char *cur_tag = notmuch_tags_get (tags);
195 if (strcmp (cur_tag, list->ops[i].tag) == 0) {
200 notmuch_tags_destroy (tags);
202 /* the following test is conservative,
203 * in the sense it ignores cases like +foo ... -foo
204 * but this is OK from a correctness point of view
214 tag_op_list_apply (notmuch_message_t *message,
219 notmuch_status_t status = 0;
220 tag_operation_t *tag_ops = list->ops;
222 if (! (flags & TAG_FLAG_PRE_OPTIMIZED) && ! makes_changes (message, list, flags))
223 return NOTMUCH_STATUS_SUCCESS;
225 status = notmuch_message_freeze (message);
227 message_error (message, status, "freezing message");
231 if (flags & TAG_FLAG_REMOVE_ALL) {
232 status = notmuch_message_remove_all_tags (message);
234 message_error (message, status, "removing all tags");
239 for (i = 0; i < list->count; i++) {
240 if (tag_ops[i].remove) {
241 status = notmuch_message_remove_tag (message, tag_ops[i].tag);
243 message_error (message, status, "removing tag %s", tag_ops[i].tag);
247 status = notmuch_message_add_tag (message, tag_ops[i].tag);
249 message_error (message, status, "adding tag %s", tag_ops[i].tag);
256 status = notmuch_message_thaw (message);
258 message_error (message, status, "thawing message");
263 if (flags & TAG_FLAG_MAILDIR_SYNC) {
264 status = notmuch_message_tags_to_maildir_flags (message);
266 message_error (message, status, "synching tags to maildir");
271 return NOTMUCH_STATUS_SUCCESS;
276 /* Array of tagging operations (add or remove. Size will be increased
280 tag_op_list_create (void *ctx)
284 list = talloc (ctx, tag_op_list_t);
288 list->size = TAG_OP_LIST_INITIAL_SIZE;
291 list->ops = talloc_array (list, tag_operation_t, list->size);
292 if (list->ops == NULL)
300 tag_op_list_append (tag_op_list_t *list,
302 notmuch_bool_t remove)
304 /* Make room if current array is full. This should be a fairly
305 * rare case, considering the initial array size.
308 if (list->count == list->size) {
310 list->ops = talloc_realloc (list, list->ops, tag_operation_t,
312 if (list->ops == NULL) {
313 fprintf (stderr, "Out of memory.\n");
318 /* add the new operation */
320 list->ops[list->count].tag = tag;
321 list->ops[list->count].remove = remove;
327 * Is the i'th tag operation a remove?
331 tag_op_list_isremove (const tag_op_list_t *list, size_t i)
333 assert (i < list->count);
334 return list->ops[i].remove;
338 * Reset a list to contain no operations
342 tag_op_list_reset (tag_op_list_t *list)
348 * Return the number of operations in a list
352 tag_op_list_size (const tag_op_list_t *list)
358 * return the i'th tag in the list
362 tag_op_list_tag (const tag_op_list_t *list, size_t i)
364 assert (i < list->count);
365 return list->ops[i].tag;