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 tag_op_list_reset (tag_ops);
170 for (i = 0; i < argc; i++) {
171 if (strcmp (argv[i], "--") == 0) {
176 if (argv[i][0] != '+' && argv[i][0] != '-')
179 notmuch_bool_t is_remove = argv[i][0] == '-';
182 msg = illegal_tag (argv[i] + 1, is_remove);
184 fprintf (stderr, "Error: %s", msg);
185 return TAG_PARSE_INVALID;
188 tag_op_list_append (tag_ops, argv[i] + 1, is_remove);
191 *query_str = query_string_from_args (ctx, argc - i, &argv[i]);
193 if (*query_str == NULL || **query_str == '\0') {
194 fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");
195 return TAG_PARSE_INVALID;
198 return TAG_PARSE_SUCCESS;
203 message_error (notmuch_message_t *message,
204 notmuch_status_t status,
205 const char *format, ...)
209 va_start (va_args, format);
211 vfprintf (stderr, format, va_args);
212 fprintf (stderr, "Message-ID: %s\n", notmuch_message_get_message_id (message));
213 fprintf (stderr, "Status: %s\n", notmuch_status_to_string (status));
217 makes_changes (notmuch_message_t *message,
224 notmuch_tags_t *tags;
225 notmuch_bool_t changes = FALSE;
227 /* First, do we delete an existing tag? */
229 for (tags = notmuch_message_get_tags (message);
230 ! changes && notmuch_tags_valid (tags);
231 notmuch_tags_move_to_next (tags)) {
232 const char *cur_tag = notmuch_tags_get (tags);
233 int last_op = (flags & TAG_FLAG_REMOVE_ALL) ? -1 : 0;
235 /* scan backwards to get last operation */
239 if (strcmp (cur_tag, list->ops[i].tag) == 0) {
240 last_op = list->ops[i].remove ? -1 : 1;
245 changes = (last_op == -1);
247 notmuch_tags_destroy (tags);
252 /* Now check for adding new tags */
253 for (i = 0; i < list->count; i++) {
254 notmuch_bool_t exists = FALSE;
256 if (list->ops[i].remove)
259 for (tags = notmuch_message_get_tags (message);
260 notmuch_tags_valid (tags);
261 notmuch_tags_move_to_next (tags)) {
262 const char *cur_tag = notmuch_tags_get (tags);
263 if (strcmp (cur_tag, list->ops[i].tag) == 0) {
268 notmuch_tags_destroy (tags);
270 /* the following test is conservative,
271 * in the sense it ignores cases like +foo ... -foo
272 * but this is OK from a correctness point of view
282 tag_op_list_apply (notmuch_message_t *message,
287 notmuch_status_t status = 0;
288 tag_operation_t *tag_ops = list->ops;
290 if (! (flags & TAG_FLAG_PRE_OPTIMIZED) && ! makes_changes (message, list, flags))
291 return NOTMUCH_STATUS_SUCCESS;
293 status = notmuch_message_freeze (message);
295 message_error (message, status, "freezing message");
299 if (flags & TAG_FLAG_REMOVE_ALL) {
300 status = notmuch_message_remove_all_tags (message);
302 message_error (message, status, "removing all tags");
307 for (i = 0; i < list->count; i++) {
308 if (tag_ops[i].remove) {
309 status = notmuch_message_remove_tag (message, tag_ops[i].tag);
311 message_error (message, status, "removing tag %s", tag_ops[i].tag);
315 status = notmuch_message_add_tag (message, tag_ops[i].tag);
317 message_error (message, status, "adding tag %s", tag_ops[i].tag);
324 status = notmuch_message_thaw (message);
326 message_error (message, status, "thawing message");
331 if (flags & TAG_FLAG_MAILDIR_SYNC) {
332 status = notmuch_message_tags_to_maildir_flags (message);
334 message_error (message, status, "synching tags to maildir");
339 return NOTMUCH_STATUS_SUCCESS;
344 /* Array of tagging operations (add or remove. Size will be increased
348 tag_op_list_create (void *ctx)
352 list = talloc (ctx, tag_op_list_t);
356 list->size = TAG_OP_LIST_INITIAL_SIZE;
359 list->ops = talloc_array (list, tag_operation_t, list->size);
360 if (list->ops == NULL)
368 tag_op_list_append (tag_op_list_t *list,
370 notmuch_bool_t remove)
372 /* Make room if current array is full. This should be a fairly
373 * rare case, considering the initial array size.
376 if (list->count == list->size) {
378 list->ops = talloc_realloc (list, list->ops, tag_operation_t,
380 if (list->ops == NULL) {
381 fprintf (stderr, "Out of memory.\n");
386 /* add the new operation */
388 list->ops[list->count].tag = tag;
389 list->ops[list->count].remove = remove;
395 * Is the i'th tag operation a remove?
399 tag_op_list_isremove (const tag_op_list_t *list, size_t i)
401 assert (i < list->count);
402 return list->ops[i].remove;
406 * Reset a list to contain no operations
410 tag_op_list_reset (tag_op_list_t *list)
416 * Return the number of operations in a list
420 tag_op_list_size (const tag_op_list_t *list)
426 * return the i'th tag in the list
430 tag_op_list_tag (const tag_op_list_t *list, size_t i)
432 assert (i < list->count);
433 return list->ops[i].tag;