]> git.notmuchmail.org Git - notmuch/blob - notmuch-count.c
contrib: pick: bind M-p and M-n to prev/next thread
[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     OUTPUT_FILES,
28 };
29
30 /* The following is to allow future options to be added more easily */
31 enum {
32     EXCLUDE_TRUE,
33     EXCLUDE_FALSE,
34 };
35
36 static unsigned int
37 count_files (notmuch_query_t *query)
38 {
39     notmuch_messages_t *messages;
40     notmuch_message_t *message;
41     notmuch_filenames_t *filenames;
42     unsigned int count = 0;
43
44     messages = notmuch_query_search_messages (query);
45     if (messages == NULL)
46         return 0;
47
48     for (;
49          notmuch_messages_valid (messages);
50          notmuch_messages_move_to_next (messages)) {
51         message = notmuch_messages_get (messages);
52         filenames = notmuch_message_get_filenames (message);
53
54         for (;
55              notmuch_filenames_valid (filenames);
56              notmuch_filenames_move_to_next (filenames))
57             count++;
58
59         notmuch_filenames_destroy (filenames);
60         notmuch_message_destroy (message);
61     }
62
63     notmuch_messages_destroy (messages);
64
65     return count;
66 }
67
68 static int
69 print_count (notmuch_database_t *notmuch, const char *query_str,
70              const char **exclude_tags, size_t exclude_tags_length, int output)
71 {
72     notmuch_query_t *query;
73     size_t i;
74
75     query = notmuch_query_create (notmuch, query_str);
76     if (query == NULL) {
77         fprintf (stderr, "Out of memory\n");
78         return 1;
79     }
80
81     for (i = 0; i < exclude_tags_length; i++)
82         notmuch_query_add_tag_exclude (query, exclude_tags[i]);
83
84     switch (output) {
85     case OUTPUT_MESSAGES:
86         printf ("%u\n", notmuch_query_count_messages (query));
87         break;
88     case OUTPUT_THREADS:
89         printf ("%u\n", notmuch_query_count_threads (query));
90         break;
91     case OUTPUT_FILES:
92         printf ("%u\n", count_files (query));
93         break;
94     }
95
96     notmuch_query_destroy (query);
97
98     return 0;
99 }
100
101 static int
102 count_file (notmuch_database_t *notmuch, FILE *input, const char **exclude_tags,
103             size_t exclude_tags_length, int output)
104 {
105     char *line = NULL;
106     ssize_t line_len;
107     size_t line_size;
108     int ret = 0;
109
110     while (!ret && (line_len = getline (&line, &line_size, input)) != -1) {
111         chomp_newline (line);
112         ret = print_count (notmuch, line, exclude_tags, exclude_tags_length,
113                            output);
114     }
115
116     if (line)
117         free (line);
118
119     return ret;
120 }
121
122 int
123 notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
124 {
125     notmuch_database_t *notmuch;
126     char *query_str;
127     int opt_index;
128     int output = OUTPUT_MESSAGES;
129     int exclude = EXCLUDE_TRUE;
130     const char **search_exclude_tags = NULL;
131     size_t search_exclude_tags_length = 0;
132     notmuch_bool_t batch = FALSE;
133     FILE *input = stdin;
134     char *input_file_name = NULL;
135     int ret;
136
137     notmuch_opt_desc_t options[] = {
138         { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
139           (notmuch_keyword_t []){ { "threads", OUTPUT_THREADS },
140                                   { "messages", OUTPUT_MESSAGES },
141                                   { "files", OUTPUT_FILES },
142                                   { 0, 0 } } },
143         { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
144           (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
145                                   { "false", EXCLUDE_FALSE },
146                                   { 0, 0 } } },
147         { NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },
148         { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
149         { 0, 0, 0, 0, 0 }
150     };
151
152     opt_index = parse_arguments (argc, argv, options, 1);
153
154     if (opt_index < 0) {
155         return 1;
156     }
157
158     if (input_file_name) {
159         batch = TRUE;
160         input = fopen (input_file_name, "r");
161         if (input == NULL) {
162             fprintf (stderr, "Error opening %s for reading: %s\n",
163                      input_file_name, strerror (errno));
164             return 1;
165         }
166     }
167
168     if (batch && opt_index != argc) {
169         fprintf (stderr, "--batch and query string are not compatible\n");
170         return 1;
171     }
172
173     if (notmuch_database_open (notmuch_config_get_database_path (config),
174                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
175         return 1;
176
177     query_str = query_string_from_args (config, argc-opt_index, argv+opt_index);
178     if (query_str == NULL) {
179         fprintf (stderr, "Out of memory.\n");
180         return 1;
181     }
182
183     if (exclude == EXCLUDE_TRUE) {
184         search_exclude_tags = notmuch_config_get_search_exclude_tags
185             (config, &search_exclude_tags_length);
186     }
187
188     if (batch)
189         ret = count_file (notmuch, input, search_exclude_tags,
190                           search_exclude_tags_length, output);
191     else
192         ret = print_count (notmuch, query_str, search_exclude_tags,
193                            search_exclude_tags_length, output);
194
195     notmuch_database_destroy (notmuch);
196
197     if (input != stdin)
198         fclose (input);
199
200     return ret;
201 }