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)
47 line_for_error = talloc_strdup (ctx, line);
48 if (line_for_error == NULL) {
49 fprintf (stderr, "Error: out of memory\n");
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 (ctx, 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 tag_op_list_apply (notmuch_message_t *message,
160 notmuch_status_t status = 0;
161 tag_operation_t *tag_ops = list->ops;
163 status = notmuch_message_freeze (message);
165 message_error (message, status, "freezing message");
169 if (flags & TAG_FLAG_REMOVE_ALL) {
170 status = notmuch_message_remove_all_tags (message);
172 message_error (message, status, "removing all tags");
177 for (i = 0; i < list->count; i++) {
178 if (tag_ops[i].remove) {
179 status = notmuch_message_remove_tag (message, tag_ops[i].tag);
181 message_error (message, status, "removing tag %s", tag_ops[i].tag);
185 status = notmuch_message_add_tag (message, tag_ops[i].tag);
187 message_error (message, status, "adding tag %s", tag_ops[i].tag);
194 status = notmuch_message_thaw (message);
196 message_error (message, status, "thawing message");
201 if (flags & TAG_FLAG_MAILDIR_SYNC) {
202 status = notmuch_message_tags_to_maildir_flags (message);
204 message_error (message, status, "synching tags to maildir");
209 return NOTMUCH_STATUS_SUCCESS;
214 /* Array of tagging operations (add or remove. Size will be increased
218 tag_op_list_create (void *ctx)
222 list = talloc (ctx, tag_op_list_t);
226 list->size = TAG_OP_LIST_INITIAL_SIZE;
229 list->ops = talloc_array (ctx, tag_operation_t, list->size);
230 if (list->ops == NULL)
238 tag_op_list_append (void *ctx,
241 notmuch_bool_t remove)
243 /* Make room if current array is full. This should be a fairly
244 * rare case, considering the initial array size.
247 if (list->count == list->size) {
249 list->ops = talloc_realloc (ctx, list->ops, tag_operation_t,
251 if (list->ops == NULL) {
252 fprintf (stderr, "Out of memory.\n");
257 /* add the new operation */
259 list->ops[list->count].tag = tag;
260 list->ops[list->count].remove = remove;
266 * Is the i'th tag operation a remove?
270 tag_op_list_isremove (const tag_op_list_t *list, size_t i)
272 assert (i < list->count);
273 return list->ops[i].remove;
277 * Reset a list to contain no operations
281 tag_op_list_reset (tag_op_list_t *list)
287 * Return the number of operations in a list
291 tag_op_list_size (const tag_op_list_t *list)
297 * return the i'th tag in the list
301 tag_op_list_tag (const tag_op_list_t *list, size_t i)
303 assert (i < list->count);
304 return list->ops[i].tag;