]> git.notmuchmail.org Git - notmuch/blob - notmuch-count.c
configure: Add support for FreeBSD.
[notmuch] / notmuch-count.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  * Copyright © 2009 Keith Packard
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see http://www.gnu.org/licenses/ .
18  *
19  * Author: Keith Packard <keithp@keithp.com>
20  */
21
22 #include "notmuch-client.h"
23
24 enum {
25     OUTPUT_THREADS,
26     OUTPUT_MESSAGES,
27 };
28
29 /* The following is to allow future options to be added more easily */
30 enum {
31     EXCLUDE_TRUE,
32     EXCLUDE_FALSE,
33 };
34
35 int
36 notmuch_count_command (void *ctx, int argc, char *argv[])
37 {
38     notmuch_config_t *config;
39     notmuch_database_t *notmuch;
40     notmuch_query_t *query;
41     char *query_str;
42     int opt_index;
43     int output = OUTPUT_MESSAGES;
44     int exclude = EXCLUDE_TRUE;
45     unsigned int i;
46
47     notmuch_opt_desc_t options[] = {
48         { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
49           (notmuch_keyword_t []){ { "threads", OUTPUT_THREADS },
50                                   { "messages", OUTPUT_MESSAGES },
51                                   { 0, 0 } } },
52         { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
53           (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
54                                   { "false", EXCLUDE_FALSE },
55                                   { 0, 0 } } },
56         { 0, 0, 0, 0, 0 }
57     };
58
59     opt_index = parse_arguments (argc, argv, options, 1);
60
61     if (opt_index < 0) {
62         return 1;
63     }
64
65     config = notmuch_config_open (ctx, NULL, NULL);
66     if (config == NULL)
67         return 1;
68
69     if (notmuch_database_open (notmuch_config_get_database_path (config),
70                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
71         return 1;
72
73     query_str = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
74     if (query_str == NULL) {
75         fprintf (stderr, "Out of memory.\n");
76         return 1;
77     }
78
79     if (*query_str == '\0') {
80         query_str = talloc_strdup (ctx, "");
81     }
82
83     query = notmuch_query_create (notmuch, query_str);
84     if (query == NULL) {
85         fprintf (stderr, "Out of memory\n");
86         return 1;
87     }
88
89     if (exclude == EXCLUDE_TRUE) {
90         const char **search_exclude_tags;
91         size_t search_exclude_tags_length;
92
93         search_exclude_tags = notmuch_config_get_search_exclude_tags
94             (config, &search_exclude_tags_length);
95         for (i = 0; i < search_exclude_tags_length; i++)
96             notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
97     }
98
99     switch (output) {
100     case OUTPUT_MESSAGES:
101         printf ("%u\n", notmuch_query_count_messages (query));
102         break;
103     case OUTPUT_THREADS:
104         printf ("%u\n", notmuch_query_count_threads (query));
105         break;
106     }
107
108     notmuch_query_destroy (query);
109     notmuch_database_destroy (notmuch);
110
111     return 0;
112 }