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 illegal_tag (const char *tag, notmuch_bool_t remove)
38 if (*tag == '\0' && ! remove)
39 return "empty tag forbidden";
41 /* This disallows adding tags starting with "-", in particular the
42 * non-removable tag "-" and enables notmuch tag to take long
43 * options more easily.
46 if (*tag == '-' && ! remove)
47 return "tag starting with '-' forbidden";
53 parse_tag_line (void *ctx, char *line,
56 tag_op_list_t *tag_ops)
61 tag_parse_status_t ret = TAG_PARSE_SUCCESS;
65 line_for_error = talloc_strdup (ctx, line);
66 if (line_for_error == NULL) {
67 fprintf (stderr, "Error: out of memory\n");
68 return TAG_PARSE_OUT_OF_MEMORY;
71 /* remove leading space */
72 while (*tok == ' ' || *tok == '\t')
75 /* Skip empty and comment lines. */
76 if (*tok == '\0' || *tok == '#') {
77 ret = TAG_PARSE_SKIPPED;
81 tag_op_list_reset (tag_ops);
84 while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {
85 notmuch_bool_t remove;
88 /* Optional explicit end of tags marker. */
89 if (tok_len == 2 && strncmp (tok, "--", tok_len) == 0) {
90 tok = strtok_len (tok + tok_len, " ", &tok_len);
92 ret = line_error (TAG_PARSE_INVALID, line_for_error,
93 "no query string after --");
99 /* Implicit end of tags. */
100 if (*tok != '-' && *tok != '+')
103 /* If tag is terminated by NUL, there's no query string. */
104 if (*(tok + tok_len) == '\0') {
105 ret = line_error (TAG_PARSE_INVALID, line_for_error,
110 /* Terminate, and start next token after terminator. */
111 *(tok + tok_len++) = '\0';
113 remove = (*tok == '-');
116 /* Maybe refuse illegal tags. */
117 if (! (flags & TAG_FLAG_BE_GENEROUS)) {
118 const char *msg = illegal_tag (tag, remove);
120 ret = line_error (TAG_PARSE_INVALID, line_for_error, msg);
126 if (hex_decode_inplace (tag) != HEX_SUCCESS) {
127 ret = line_error (TAG_PARSE_INVALID, line_for_error,
128 "hex decoding of tag %s failed", tag);
132 if (tag_op_list_append (tag_ops, tag, remove)) {
133 ret = line_error (TAG_PARSE_OUT_OF_MEMORY, line_for_error,
140 /* use a different error message for testing */
141 ret = line_error (TAG_PARSE_INVALID, line_for_error,
142 "missing query string");
146 /* tok now points to the query string */
150 talloc_free (line_for_error);
155 parse_tag_command_line (void *ctx, int argc, char **argv,
156 char **query_str, tag_op_list_t *tag_ops)
161 for (i = 0; i < argc; i++) {
162 if (strcmp (argv[i], "--") == 0) {
167 if (argv[i][0] != '+' && argv[i][0] != '-')
170 notmuch_bool_t is_remove = argv[i][0] == '-';
173 msg = illegal_tag (argv[i] + 1, is_remove);
175 fprintf (stderr, "Error: %s\n", msg);
176 return TAG_PARSE_INVALID;
179 tag_op_list_append (tag_ops, argv[i] + 1, is_remove);
182 *query_str = query_string_from_args (ctx, argc - i, &argv[i]);
184 if (*query_str == NULL) {
185 fprintf (stderr, "Out of memory.\n");
186 return TAG_PARSE_OUT_OF_MEMORY;
189 return TAG_PARSE_SUCCESS;
194 message_error (notmuch_message_t *message,
195 notmuch_status_t status,
196 const char *format, ...)
200 va_start (va_args, format);
202 vfprintf (stderr, format, va_args);
203 fprintf (stderr, "Message-ID: %s\n", notmuch_message_get_message_id (message));
204 fprintf (stderr, "Status: %s\n", notmuch_status_to_string (status));
208 makes_changes (notmuch_message_t *message,
215 notmuch_tags_t *tags;
216 notmuch_bool_t changes = FALSE;
218 /* First, do we delete an existing tag? */
220 for (tags = notmuch_message_get_tags (message);
221 ! changes && notmuch_tags_valid (tags);
222 notmuch_tags_move_to_next (tags)) {
223 const char *cur_tag = notmuch_tags_get (tags);
224 int last_op = (flags & TAG_FLAG_REMOVE_ALL) ? -1 : 0;
226 /* scan backwards to get last operation */
230 if (strcmp (cur_tag, list->ops[i].tag) == 0) {
231 last_op = list->ops[i].remove ? -1 : 1;
236 changes = (last_op == -1);
238 notmuch_tags_destroy (tags);
243 /* Now check for adding new tags */
244 for (i = 0; i < list->count; i++) {
245 notmuch_bool_t exists = FALSE;
247 if (list->ops[i].remove)
250 for (tags = notmuch_message_get_tags (message);
251 notmuch_tags_valid (tags);
252 notmuch_tags_move_to_next (tags)) {
253 const char *cur_tag = notmuch_tags_get (tags);
254 if (strcmp (cur_tag, list->ops[i].tag) == 0) {
259 notmuch_tags_destroy (tags);
261 /* the following test is conservative,
262 * in the sense it ignores cases like +foo ... -foo
263 * but this is OK from a correctness point of view
273 tag_op_list_apply (notmuch_message_t *message,
278 notmuch_status_t status = 0;
279 tag_operation_t *tag_ops = list->ops;
281 if (! (flags & TAG_FLAG_PRE_OPTIMIZED) && ! makes_changes (message, list, flags))
282 return NOTMUCH_STATUS_SUCCESS;
284 status = notmuch_message_freeze (message);
286 message_error (message, status, "freezing message");
290 if (flags & TAG_FLAG_REMOVE_ALL) {
291 status = notmuch_message_remove_all_tags (message);
293 message_error (message, status, "removing all tags");
298 for (i = 0; i < list->count; i++) {
299 if (tag_ops[i].remove) {
300 status = notmuch_message_remove_tag (message, tag_ops[i].tag);
302 message_error (message, status, "removing tag %s", tag_ops[i].tag);
306 status = notmuch_message_add_tag (message, tag_ops[i].tag);
308 message_error (message, status, "adding tag %s", tag_ops[i].tag);
315 status = notmuch_message_thaw (message);
317 message_error (message, status, "thawing message");
322 if (flags & TAG_FLAG_MAILDIR_SYNC) {
323 status = notmuch_message_tags_to_maildir_flags (message);
325 message_error (message, status, "synching tags to maildir");
330 return NOTMUCH_STATUS_SUCCESS;
335 /* Array of tagging operations (add or remove. Size will be increased
339 tag_op_list_create (void *ctx)
343 list = talloc (ctx, tag_op_list_t);
347 list->size = TAG_OP_LIST_INITIAL_SIZE;
350 list->ops = talloc_array (list, tag_operation_t, list->size);
351 if (list->ops == NULL)
359 tag_op_list_append (tag_op_list_t *list,
361 notmuch_bool_t remove)
363 /* Make room if current array is full. This should be a fairly
364 * rare case, considering the initial array size.
367 if (list->count == list->size) {
369 list->ops = talloc_realloc (list, list->ops, tag_operation_t,
371 if (list->ops == NULL) {
372 fprintf (stderr, "Out of memory.\n");
377 /* add the new operation */
379 list->ops[list->count].tag = tag;
380 list->ops[list->count].remove = remove;
386 * Is the i'th tag operation a remove?
390 tag_op_list_isremove (const tag_op_list_t *list, size_t i)
392 assert (i < list->count);
393 return list->ops[i].remove;
397 * Reset a list to contain no operations
401 tag_op_list_reset (tag_op_list_t *list)
407 * Return the number of operations in a list
411 tag_op_list_size (const tag_op_list_t *list)
417 * return the i'th tag in the list
421 tag_op_list_tag (const tag_op_list_t *list, size_t i)
423 assert (i < list->count);
424 return list->ops[i].tag;