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 the non-removable tag "-" and
49 * enables notmuch tag to take long options more easily.
52 if (*tag == '-' && ! remove)
53 return "tag starting with '-' forbidden";
59 parse_tag_line (void *ctx, char *line,
62 tag_op_list_t *tag_ops)
67 tag_parse_status_t ret = TAG_PARSE_SUCCESS;
71 line_for_error = talloc_strdup (ctx, line);
72 if (line_for_error == NULL) {
73 fprintf (stderr, "Error: out of memory\n");
74 return TAG_PARSE_OUT_OF_MEMORY;
77 /* remove leading space */
78 while (*tok == ' ' || *tok == '\t')
81 /* Skip empty and comment lines. */
82 if (*tok == '\0' || *tok == '#') {
83 ret = TAG_PARSE_SKIPPED;
87 tag_op_list_reset (tag_ops);
90 while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {
91 notmuch_bool_t remove;
94 /* Optional explicit end of tags marker. */
95 if (tok_len == 2 && strncmp (tok, "--", tok_len) == 0) {
96 tok = strtok_len (tok + tok_len, " ", &tok_len);
98 ret = line_error (TAG_PARSE_INVALID, line_for_error,
99 "no query string after --");
105 /* Implicit end of tags. */
106 if (*tok != '-' && *tok != '+')
109 /* If tag is terminated by NUL, there's no query string. */
110 if (*(tok + tok_len) == '\0') {
111 ret = line_error (TAG_PARSE_INVALID, line_for_error,
116 /* Terminate, and start next token after terminator. */
117 *(tok + tok_len++) = '\0';
119 remove = (*tok == '-');
122 /* Maybe refuse illegal tags. */
123 if (! (flags & TAG_FLAG_BE_GENEROUS)) {
124 const char *msg = illegal_tag (tag, remove);
126 ret = line_error (TAG_PARSE_INVALID, line_for_error, msg);
132 if (hex_decode_inplace (tag) != HEX_SUCCESS) {
133 ret = line_error (TAG_PARSE_INVALID, line_for_error,
134 "hex decoding of tag %s failed", tag);
138 if (tag_op_list_append (tag_ops, tag, remove)) {
139 ret = line_error (TAG_PARSE_OUT_OF_MEMORY, line_for_error,
146 /* use a different error message for testing */
147 ret = line_error (TAG_PARSE_INVALID, line_for_error,
148 "missing query string");
152 /* tok now points to the query string */
156 talloc_free (line_for_error);
161 message_error (notmuch_message_t *message,
162 notmuch_status_t status,
163 const char *format, ...)
167 va_start (va_args, format);
169 vfprintf (stderr, format, va_args);
170 fprintf (stderr, "Message-ID: %s\n", notmuch_message_get_message_id (message));
171 fprintf (stderr, "Status: %s\n", notmuch_status_to_string (status));
175 makes_changes (notmuch_message_t *message,
182 notmuch_tags_t *tags;
183 notmuch_bool_t changes = FALSE;
185 /* First, do we delete an existing tag? */
187 for (tags = notmuch_message_get_tags (message);
188 ! changes && notmuch_tags_valid (tags);
189 notmuch_tags_move_to_next (tags)) {
190 const char *cur_tag = notmuch_tags_get (tags);
191 int last_op = (flags & TAG_FLAG_REMOVE_ALL) ? -1 : 0;
193 /* scan backwards to get last operation */
197 if (strcmp (cur_tag, list->ops[i].tag) == 0) {
198 last_op = list->ops[i].remove ? -1 : 1;
203 changes = (last_op == -1);
205 notmuch_tags_destroy (tags);
210 /* Now check for adding new tags */
211 for (i = 0; i < list->count; i++) {
212 notmuch_bool_t exists = FALSE;
214 if (list->ops[i].remove)
217 for (tags = notmuch_message_get_tags (message);
218 notmuch_tags_valid (tags);
219 notmuch_tags_move_to_next (tags)) {
220 const char *cur_tag = notmuch_tags_get (tags);
221 if (strcmp (cur_tag, list->ops[i].tag) == 0) {
226 notmuch_tags_destroy (tags);
228 /* the following test is conservative,
229 * in the sense it ignores cases like +foo ... -foo
230 * but this is OK from a correctness point of view
240 tag_op_list_apply (notmuch_message_t *message,
245 notmuch_status_t status = 0;
246 tag_operation_t *tag_ops = list->ops;
248 if (! (flags & TAG_FLAG_PRE_OPTIMIZED) && ! makes_changes (message, list, flags))
249 return NOTMUCH_STATUS_SUCCESS;
251 status = notmuch_message_freeze (message);
253 message_error (message, status, "freezing message");
257 if (flags & TAG_FLAG_REMOVE_ALL) {
258 status = notmuch_message_remove_all_tags (message);
260 message_error (message, status, "removing all tags");
265 for (i = 0; i < list->count; i++) {
266 if (tag_ops[i].remove) {
267 status = notmuch_message_remove_tag (message, tag_ops[i].tag);
269 message_error (message, status, "removing tag %s", tag_ops[i].tag);
273 status = notmuch_message_add_tag (message, tag_ops[i].tag);
275 message_error (message, status, "adding tag %s", tag_ops[i].tag);
282 status = notmuch_message_thaw (message);
284 message_error (message, status, "thawing message");
289 if (flags & TAG_FLAG_MAILDIR_SYNC) {
290 status = notmuch_message_tags_to_maildir_flags (message);
292 message_error (message, status, "synching tags to maildir");
297 return NOTMUCH_STATUS_SUCCESS;
302 /* Array of tagging operations (add or remove. Size will be increased
306 tag_op_list_create (void *ctx)
310 list = talloc (ctx, tag_op_list_t);
314 list->size = TAG_OP_LIST_INITIAL_SIZE;
317 list->ops = talloc_array (list, tag_operation_t, list->size);
318 if (list->ops == NULL)
326 tag_op_list_append (tag_op_list_t *list,
328 notmuch_bool_t remove)
330 /* Make room if current array is full. This should be a fairly
331 * rare case, considering the initial array size.
334 if (list->count == list->size) {
336 list->ops = talloc_realloc (list, list->ops, tag_operation_t,
338 if (list->ops == NULL) {
339 fprintf (stderr, "Out of memory.\n");
344 /* add the new operation */
346 list->ops[list->count].tag = tag;
347 list->ops[list->count].remove = remove;
353 * Is the i'th tag operation a remove?
357 tag_op_list_isremove (const tag_op_list_t *list, size_t i)
359 assert (i < list->count);
360 return list->ops[i].remove;
364 * Reset a list to contain no operations
368 tag_op_list_reset (tag_op_list_t *list)
374 * Return the number of operations in a list
378 tag_op_list_size (const tag_op_list_t *list)
384 * return the i'th tag in the list
388 tag_op_list_tag (const tag_op_list_t *list, size_t i)
390 assert (i < list->count);
391 return list->ops[i].tag;