]> git.notmuchmail.org Git - notmuch/blob - notmuch-search.c
cli: fix clang build warnings
[notmuch] / notmuch-search.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22 #include "sprinter.h"
23 #include "string-util.h"
24
25 typedef enum {
26     /* Search command */
27     OUTPUT_SUMMARY      = 1 << 0,
28     OUTPUT_THREADS      = 1 << 1,
29     OUTPUT_MESSAGES     = 1 << 2,
30     OUTPUT_FILES        = 1 << 3,
31     OUTPUT_TAGS         = 1 << 4,
32
33     /* Address command */
34     OUTPUT_SENDER       = 1 << 5,
35     OUTPUT_RECIPIENTS   = 1 << 6,
36     OUTPUT_COUNT        = 1 << 7,
37 } output_t;
38
39 typedef enum {
40     NOTMUCH_FORMAT_JSON,
41     NOTMUCH_FORMAT_TEXT,
42     NOTMUCH_FORMAT_TEXT0,
43     NOTMUCH_FORMAT_SEXP
44 } format_sel_t;
45
46 typedef struct {
47     notmuch_database_t *notmuch;
48     format_sel_t format_sel;
49     sprinter_t *format;
50     notmuch_exclude_t exclude;
51     notmuch_query_t *query;
52     notmuch_sort_t sort;
53     output_t output;
54     int offset;
55     int limit;
56     int dupe;
57     GHashTable *addresses;
58 } search_context_t;
59
60 typedef struct {
61     const char *name;
62     const char *addr;
63     int count;
64 } mailbox_t;
65
66 /* Return two stable query strings that identify exactly the matched
67  * and unmatched messages currently in thread.  If there are no
68  * matched or unmatched messages, the returned buffers will be
69  * NULL. */
70 static int
71 get_thread_query (notmuch_thread_t *thread,
72                   char **matched_out, char **unmatched_out)
73 {
74     notmuch_messages_t *messages;
75     char *escaped = NULL;
76     size_t escaped_len = 0;
77
78     *matched_out = *unmatched_out = NULL;
79
80     for (messages = notmuch_thread_get_messages (thread);
81          notmuch_messages_valid (messages);
82          notmuch_messages_move_to_next (messages))
83     {
84         notmuch_message_t *message = notmuch_messages_get (messages);
85         const char *mid = notmuch_message_get_message_id (message);
86         /* Determine which query buffer to extend */
87         char **buf = notmuch_message_get_flag (
88             message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;
89         /* Add this message's id: query.  Since "id" is an exclusive
90          * prefix, it is implicitly 'or'd together, so we only need to
91          * join queries with a space. */
92         if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
93             return -1;
94         if (*buf)
95             *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
96         else
97             *buf = talloc_strdup (thread, escaped);
98         if (!*buf)
99             return -1;
100     }
101     talloc_free (escaped);
102     return 0;
103 }
104
105 static int
106 do_search_threads (search_context_t *ctx)
107 {
108     notmuch_thread_t *thread;
109     notmuch_threads_t *threads;
110     notmuch_tags_t *tags;
111     sprinter_t *format = ctx->format;
112     time_t date;
113     int i;
114
115     if (ctx->offset < 0) {
116         ctx->offset += notmuch_query_count_threads (ctx->query);
117         if (ctx->offset < 0)
118             ctx->offset = 0;
119     }
120
121     threads = notmuch_query_search_threads (ctx->query);
122     if (threads == NULL)
123         return 1;
124
125     format->begin_list (format);
126
127     for (i = 0;
128          notmuch_threads_valid (threads) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
129          notmuch_threads_move_to_next (threads), i++)
130     {
131         thread = notmuch_threads_get (threads);
132
133         if (i < ctx->offset) {
134             notmuch_thread_destroy (thread);
135             continue;
136         }
137
138         if (ctx->output == OUTPUT_THREADS) {
139             format->set_prefix (format, "thread");
140             format->string (format,
141                             notmuch_thread_get_thread_id (thread));
142             format->separator (format);
143         } else { /* output == OUTPUT_SUMMARY */
144             void *ctx_quote = talloc_new (thread);
145             const char *authors = notmuch_thread_get_authors (thread);
146             const char *subject = notmuch_thread_get_subject (thread);
147             const char *thread_id = notmuch_thread_get_thread_id (thread);
148             int matched = notmuch_thread_get_matched_messages (thread);
149             int total = notmuch_thread_get_total_messages (thread);
150             const char *relative_date = NULL;
151             notmuch_bool_t first_tag = TRUE;
152
153             format->begin_map (format);
154
155             if (ctx->sort == NOTMUCH_SORT_OLDEST_FIRST)
156                 date = notmuch_thread_get_oldest_date (thread);
157             else
158                 date = notmuch_thread_get_newest_date (thread);
159
160             relative_date = notmuch_time_relative_date (ctx_quote, date);
161
162             if (format->is_text_printer) {
163                 /* Special case for the text formatter */
164                 printf ("thread:%s %12s [%d/%d] %s; %s (",
165                         thread_id,
166                         relative_date,
167                         matched,
168                         total,
169                         sanitize_string (ctx_quote, authors),
170                         sanitize_string (ctx_quote, subject));
171             } else { /* Structured Output */
172                 format->map_key (format, "thread");
173                 format->string (format, thread_id);
174                 format->map_key (format, "timestamp");
175                 format->integer (format, date);
176                 format->map_key (format, "date_relative");
177                 format->string (format, relative_date);
178                 format->map_key (format, "matched");
179                 format->integer (format, matched);
180                 format->map_key (format, "total");
181                 format->integer (format, total);
182                 format->map_key (format, "authors");
183                 format->string (format, authors);
184                 format->map_key (format, "subject");
185                 format->string (format, subject);
186                 if (notmuch_format_version >= 2) {
187                     char *matched_query, *unmatched_query;
188                     if (get_thread_query (thread, &matched_query,
189                                           &unmatched_query) < 0) {
190                         fprintf (stderr, "Out of memory\n");
191                         return 1;
192                     }
193                     format->map_key (format, "query");
194                     format->begin_list (format);
195                     if (matched_query)
196                         format->string (format, matched_query);
197                     else
198                         format->null (format);
199                     if (unmatched_query)
200                         format->string (format, unmatched_query);
201                     else
202                         format->null (format);
203                     format->end (format);
204                 }
205             }
206
207             talloc_free (ctx_quote);
208
209             format->map_key (format, "tags");
210             format->begin_list (format);
211
212             for (tags = notmuch_thread_get_tags (thread);
213                  notmuch_tags_valid (tags);
214                  notmuch_tags_move_to_next (tags))
215             {
216                 const char *tag = notmuch_tags_get (tags);
217
218                 if (format->is_text_printer) {
219                   /* Special case for the text formatter */
220                     if (first_tag)
221                         first_tag = FALSE;
222                     else
223                         fputc (' ', stdout);
224                     fputs (tag, stdout);
225                 } else { /* Structured Output */
226                     format->string (format, tag);
227                 }
228             }
229
230             if (format->is_text_printer)
231                 printf (")");
232
233             format->end (format);
234             format->end (format);
235             format->separator (format);
236         }
237
238         notmuch_thread_destroy (thread);
239     }
240
241     format->end (format);
242
243     return 0;
244 }
245
246 /* Returns TRUE iff name and addr is duplicate. If not, stores the
247  * name/addr pair in order to detect subsequent duplicates. */
248 static notmuch_bool_t
249 is_duplicate (const search_context_t *ctx, const char *name, const char *addr)
250 {
251     notmuch_bool_t duplicate;
252     char *key;
253     mailbox_t *mailbox;
254
255     key = talloc_asprintf (ctx->format, "%s <%s>", name, addr);
256     if (! key)
257         return FALSE;
258
259     duplicate = g_hash_table_lookup_extended (ctx->addresses, key, NULL, (gpointer)&mailbox);
260
261     if (! duplicate) {
262         mailbox = talloc (ctx->format, mailbox_t);
263         mailbox->name = talloc_strdup (mailbox, name);
264         mailbox->addr = talloc_strdup (mailbox, addr);
265         mailbox->count = 1;
266         g_hash_table_insert (ctx->addresses, key, mailbox);
267     } else {
268         mailbox->count++;
269         talloc_free (key);
270     }
271
272     return duplicate;
273 }
274
275 static void
276 print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)
277 {
278     const char *name = mailbox->name;
279     const char *addr = mailbox->addr;
280     int count = mailbox->count;
281     sprinter_t *format = ctx->format;
282     InternetAddress *ia = internet_address_mailbox_new (name, addr);
283     char *name_addr;
284
285     /* name_addr has the name part quoted if necessary. Compare
286      * 'John Doe <john@doe.com>' vs. '"Doe, John" <john@doe.com>' */
287     name_addr = internet_address_to_string (ia, FALSE);
288
289     if (format->is_text_printer) {
290         if (count > 0) {
291             format->integer (format, count);
292             format->string (format, "\t");
293         }
294         format->string (format, name_addr);
295         format->separator (format);
296     } else {
297         format->begin_map (format);
298         format->map_key (format, "name");
299         format->string (format, name);
300         format->map_key (format, "address");
301         format->string (format, addr);
302         format->map_key (format, "name-addr");
303         format->string (format, name_addr);
304         if (count > 0) {
305             format->map_key (format, "count");
306             format->integer (format, count);
307         }
308         format->end (format);
309         format->separator (format);
310     }
311
312     g_object_unref (ia);
313     g_free (name_addr);
314 }
315
316 /* Print or prepare for printing addresses from InternetAddressList. */
317 static void
318 process_address_list (const search_context_t *ctx,
319                       InternetAddressList *list)
320 {
321     InternetAddress *address;
322     int i;
323
324     for (i = 0; i < internet_address_list_length (list); i++) {
325         address = internet_address_list_get_address (list, i);
326         if (INTERNET_ADDRESS_IS_GROUP (address)) {
327             InternetAddressGroup *group;
328             InternetAddressList *group_list;
329
330             group = INTERNET_ADDRESS_GROUP (address);
331             group_list = internet_address_group_get_members (group);
332             if (group_list == NULL)
333                 continue;
334
335             process_address_list (ctx, group_list);
336         } else {
337             InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
338             mailbox_t mbx = {
339                 .name = internet_address_get_name (address),
340                 .addr = internet_address_mailbox_get_addr (mailbox),
341                 .count = 0,
342             };
343
344             if (is_duplicate (ctx, mbx.name, mbx.addr))
345                 continue;
346
347             if (ctx->output & OUTPUT_COUNT)
348                 continue;
349
350             print_mailbox (ctx, &mbx);
351         }
352     }
353 }
354
355 /* Print or prepare for printing addresses from a message header. */
356 static void
357 process_address_header (const search_context_t *ctx, const char *value)
358 {
359     InternetAddressList *list;
360
361     if (value == NULL)
362         return;
363
364     list = internet_address_list_parse_string (value);
365     if (list == NULL)
366         return;
367
368     process_address_list (ctx, list);
369
370     g_object_unref (list);
371 }
372
373 /* Destructor for talloc-allocated GHashTable keys and values. */
374 static void
375 _talloc_free_for_g_hash (void *ptr)
376 {
377     talloc_free (ptr);
378 }
379
380 static void
381 print_hash_value (unused (gpointer key), gpointer value, gpointer user_data)
382 {
383     const mailbox_t *mailbox = value;
384     search_context_t *ctx = user_data;
385
386     print_mailbox (ctx, mailbox);
387 }
388
389 static int
390 _count_filenames (notmuch_message_t *message)
391 {
392     notmuch_filenames_t *filenames;
393     int i = 0;
394
395     filenames = notmuch_message_get_filenames (message);
396
397     while (notmuch_filenames_valid (filenames)) {
398         notmuch_filenames_move_to_next (filenames);
399         i++;
400     }
401
402     notmuch_filenames_destroy (filenames);
403
404     return i;
405 }
406
407 static int
408 do_search_messages (search_context_t *ctx)
409 {
410     notmuch_message_t *message;
411     notmuch_messages_t *messages;
412     notmuch_filenames_t *filenames;
413     sprinter_t *format = ctx->format;
414     int i;
415
416     if (ctx->offset < 0) {
417         ctx->offset += notmuch_query_count_messages (ctx->query);
418         if (ctx->offset < 0)
419             ctx->offset = 0;
420     }
421
422     messages = notmuch_query_search_messages (ctx->query);
423     if (messages == NULL)
424         return 1;
425
426     format->begin_list (format);
427
428     for (i = 0;
429          notmuch_messages_valid (messages) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
430          notmuch_messages_move_to_next (messages), i++)
431     {
432         if (i < ctx->offset)
433             continue;
434
435         message = notmuch_messages_get (messages);
436
437         if (ctx->output == OUTPUT_FILES) {
438             int j;
439             filenames = notmuch_message_get_filenames (message);
440
441             for (j = 1;
442                  notmuch_filenames_valid (filenames);
443                  notmuch_filenames_move_to_next (filenames), j++)
444             {
445                 if (ctx->dupe < 0 || ctx->dupe == j) {
446                     format->string (format, notmuch_filenames_get (filenames));
447                     format->separator (format);
448                 }
449             }
450             
451             notmuch_filenames_destroy( filenames );
452
453         } else if (ctx->output == OUTPUT_MESSAGES) {
454             /* special case 1 for speed */
455             if (ctx->dupe <= 1 || ctx->dupe <= _count_filenames (message)) {
456                 format->set_prefix (format, "id");
457                 format->string (format,
458                                 notmuch_message_get_message_id (message));
459                 format->separator (format);
460             }
461         } else {
462             if (ctx->output & OUTPUT_SENDER) {
463                 const char *addrs;
464
465                 addrs = notmuch_message_get_header (message, "from");
466                 process_address_header (ctx, addrs);
467             }
468
469             if (ctx->output & OUTPUT_RECIPIENTS) {
470                 const char *hdrs[] = { "to", "cc", "bcc" };
471                 const char *addrs;
472                 size_t j;
473
474                 for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
475                     addrs = notmuch_message_get_header (message, hdrs[j]);
476                     process_address_header (ctx, addrs);
477                 }
478             }
479         }
480
481         notmuch_message_destroy (message);
482     }
483
484     if (ctx->addresses && ctx->output & OUTPUT_COUNT)
485         g_hash_table_foreach (ctx->addresses, print_hash_value, ctx);
486
487     notmuch_messages_destroy (messages);
488
489     format->end (format);
490
491     return 0;
492 }
493
494 static int
495 do_search_tags (const search_context_t *ctx)
496 {
497     notmuch_messages_t *messages = NULL;
498     notmuch_tags_t *tags;
499     const char *tag;
500     sprinter_t *format = ctx->format;
501     notmuch_query_t *query = ctx->query;
502     notmuch_database_t *notmuch = ctx->notmuch;
503
504     /* should the following only special case if no excluded terms
505      * specified? */
506
507     /* Special-case query of "*" for better performance. */
508     if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
509         tags = notmuch_database_get_all_tags (notmuch);
510     } else {
511         messages = notmuch_query_search_messages (query);
512         if (messages == NULL)
513             return 1;
514
515         tags = notmuch_messages_collect_tags (messages);
516     }
517     if (tags == NULL)
518         return 1;
519
520     format->begin_list (format);
521
522     for (;
523          notmuch_tags_valid (tags);
524          notmuch_tags_move_to_next (tags))
525     {
526         tag = notmuch_tags_get (tags);
527
528         format->string (format, tag);
529         format->separator (format);
530
531     }
532
533     notmuch_tags_destroy (tags);
534
535     if (messages)
536         notmuch_messages_destroy (messages);
537
538     format->end (format);
539
540     return 0;
541 }
542
543 static int
544 _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int argc, char *argv[])
545 {
546     char *query_str;
547     unsigned int i;
548
549     switch (ctx->format_sel) {
550     case NOTMUCH_FORMAT_TEXT:
551         ctx->format = sprinter_text_create (config, stdout);
552         break;
553     case NOTMUCH_FORMAT_TEXT0:
554         if (ctx->output == OUTPUT_SUMMARY) {
555             fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
556             return EXIT_FAILURE;
557         }
558         ctx->format = sprinter_text0_create (config, stdout);
559         break;
560     case NOTMUCH_FORMAT_JSON:
561         ctx->format = sprinter_json_create (config, stdout);
562         break;
563     case NOTMUCH_FORMAT_SEXP:
564         ctx->format = sprinter_sexp_create (config, stdout);
565         break;
566     default:
567         /* this should never happen */
568         INTERNAL_ERROR("no output format selected");
569     }
570
571     notmuch_exit_if_unsupported_format ();
572
573     if (notmuch_database_open (notmuch_config_get_database_path (config),
574                                NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch))
575         return EXIT_FAILURE;
576
577     query_str = query_string_from_args (ctx->notmuch, argc, argv);
578     if (query_str == NULL) {
579         fprintf (stderr, "Out of memory.\n");
580         return EXIT_FAILURE;
581     }
582     if (*query_str == '\0') {
583         fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
584         return EXIT_FAILURE;
585     }
586
587     ctx->query = notmuch_query_create (ctx->notmuch, query_str);
588     if (ctx->query == NULL) {
589         fprintf (stderr, "Out of memory\n");
590         return EXIT_FAILURE;
591     }
592
593     notmuch_query_set_sort (ctx->query, ctx->sort);
594
595     if (ctx->exclude == NOTMUCH_EXCLUDE_FLAG && ctx->output != OUTPUT_SUMMARY) {
596         /* If we are not doing summary output there is nowhere to
597          * print the excluded flag so fall back on including the
598          * excluded messages. */
599         fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
600         ctx->exclude = NOTMUCH_EXCLUDE_FALSE;
601     }
602
603     if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {
604         const char **search_exclude_tags;
605         size_t search_exclude_tags_length;
606
607         search_exclude_tags = notmuch_config_get_search_exclude_tags
608             (config, &search_exclude_tags_length);
609         for (i = 0; i < search_exclude_tags_length; i++)
610             notmuch_query_add_tag_exclude (ctx->query, search_exclude_tags[i]);
611         notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);
612     }
613
614     return 0;
615 }
616
617 static void
618 _notmuch_search_cleanup (search_context_t *ctx)
619 {
620     notmuch_query_destroy (ctx->query);
621     notmuch_database_destroy (ctx->notmuch);
622
623     talloc_free (ctx->format);
624 }
625
626 static search_context_t search_context = {
627     .format_sel = NOTMUCH_FORMAT_TEXT,
628     .exclude = NOTMUCH_EXCLUDE_TRUE,
629     .sort = NOTMUCH_SORT_NEWEST_FIRST,
630     .output = 0,
631     .offset = 0,
632     .limit = -1, /* unlimited */
633     .dupe = -1,
634 };
635
636 static const notmuch_opt_desc_t common_options[] = {
637     { NOTMUCH_OPT_KEYWORD, &search_context.sort, "sort", 's',
638       (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
639                               { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
640                               { 0, 0 } } },
641     { NOTMUCH_OPT_KEYWORD, &search_context.format_sel, "format", 'f',
642       (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
643                               { "sexp", NOTMUCH_FORMAT_SEXP },
644                               { "text", NOTMUCH_FORMAT_TEXT },
645                               { "text0", NOTMUCH_FORMAT_TEXT0 },
646                               { 0, 0 } } },
647     { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 },
648     { 0, 0, 0, 0, 0 }
649 };
650
651 int
652 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
653 {
654     search_context_t *ctx = &search_context;
655     int opt_index, ret;
656
657     notmuch_opt_desc_t options[] = {
658         { NOTMUCH_OPT_KEYWORD, &ctx->output, "output", 'o',
659           (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
660                                   { "threads", OUTPUT_THREADS },
661                                   { "messages", OUTPUT_MESSAGES },
662                                   { "files", OUTPUT_FILES },
663                                   { "tags", OUTPUT_TAGS },
664                                   { 0, 0 } } },
665         { NOTMUCH_OPT_KEYWORD, &ctx->exclude, "exclude", 'x',
666           (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
667                                   { "false", NOTMUCH_EXCLUDE_FALSE },
668                                   { "flag", NOTMUCH_EXCLUDE_FLAG },
669                                   { "all", NOTMUCH_EXCLUDE_ALL },
670                                   { 0, 0 } } },
671         { NOTMUCH_OPT_INT, &ctx->offset, "offset", 'O', 0 },
672         { NOTMUCH_OPT_INT, &ctx->limit, "limit", 'L', 0  },
673         { NOTMUCH_OPT_INT, &ctx->dupe, "duplicate", 'D', 0  },
674         { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },
675         { 0, 0, 0, 0, 0 }
676     };
677
678     ctx->output = OUTPUT_SUMMARY;
679     opt_index = parse_arguments (argc, argv, options, 1);
680     if (opt_index < 0)
681         return EXIT_FAILURE;
682
683     if (ctx->output != OUTPUT_FILES && ctx->output != OUTPUT_MESSAGES &&
684         ctx->dupe != -1) {
685         fprintf (stderr, "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");
686         return EXIT_FAILURE;
687     }
688
689     if (_notmuch_search_prepare (ctx, config,
690                                  argc - opt_index, argv + opt_index))
691         return EXIT_FAILURE;
692
693     switch (ctx->output) {
694     case OUTPUT_SUMMARY:
695     case OUTPUT_THREADS:
696         ret = do_search_threads (ctx);
697         break;
698     case OUTPUT_MESSAGES:
699     case OUTPUT_FILES:
700         ret = do_search_messages (ctx);
701         break;
702     case OUTPUT_TAGS:
703         ret = do_search_tags (ctx);
704         break;
705     default:
706         INTERNAL_ERROR ("Unexpected output");
707     }
708
709     _notmuch_search_cleanup (ctx);
710
711     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
712 }
713
714 int
715 notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
716 {
717     search_context_t *ctx = &search_context;
718     int opt_index, ret;
719
720     notmuch_opt_desc_t options[] = {
721         { NOTMUCH_OPT_KEYWORD_FLAGS, &ctx->output, "output", 'o',
722           (notmuch_keyword_t []){ { "sender", OUTPUT_SENDER },
723                                   { "recipients", OUTPUT_RECIPIENTS },
724                                   { "count", OUTPUT_COUNT },
725                                   { 0, 0 } } },
726         { NOTMUCH_OPT_KEYWORD, &ctx->exclude, "exclude", 'x',
727           (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
728                                   { "false", NOTMUCH_EXCLUDE_FALSE },
729                                   { 0, 0 } } },
730         { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },
731         { 0, 0, 0, 0, 0 }
732     };
733
734     opt_index = parse_arguments (argc, argv, options, 1);
735     if (opt_index < 0)
736         return EXIT_FAILURE;
737
738     if (! (ctx->output & (OUTPUT_SENDER | OUTPUT_RECIPIENTS)))
739         ctx->output |= OUTPUT_SENDER;
740
741     if (_notmuch_search_prepare (ctx, config,
742                                  argc - opt_index, argv + opt_index))
743         return EXIT_FAILURE;
744
745     ctx->addresses = g_hash_table_new_full (g_str_hash, g_str_equal,
746                                             _talloc_free_for_g_hash, _talloc_free_for_g_hash);
747
748     ret = do_search_messages (ctx);
749
750     g_hash_table_unref (ctx->addresses);
751
752
753     _notmuch_search_cleanup (ctx);
754
755     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
756 }