]> git.cworth.org Git - notmuch/blob - notmuch-count.c
Merge branch 'release'
[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 /* Return the number of files matching the query, or -1 for an error */
37 static int
38 count_files (notmuch_query_t *query)
39 {
40     notmuch_messages_t *messages;
41     notmuch_message_t *message;
42     notmuch_filenames_t *filenames;
43     notmuch_status_t status;
44     int count = 0;
45
46     status = notmuch_query_search_messages_st (query, &messages);
47     if (print_status_query ("notmuch count", query, status))
48         return -1;
49
50     for (;
51          notmuch_messages_valid (messages);
52          notmuch_messages_move_to_next (messages)) {
53         message = notmuch_messages_get (messages);
54         filenames = notmuch_message_get_filenames (message);
55
56         for (;
57              notmuch_filenames_valid (filenames);
58              notmuch_filenames_move_to_next (filenames))
59             count++;
60
61         notmuch_filenames_destroy (filenames);
62         notmuch_message_destroy (message);
63     }
64
65     notmuch_messages_destroy (messages);
66
67     return count;
68 }
69
70 static int
71 print_count (notmuch_database_t *notmuch, const char *query_str,
72              const char **exclude_tags, size_t exclude_tags_length, int output, int print_lastmod)
73 {
74     notmuch_query_t *query;
75     size_t i;
76     int count;
77     unsigned long revision;
78     const char *uuid;
79     int ret = 0;
80
81     query = notmuch_query_create (notmuch, query_str);
82     if (query == NULL) {
83         fprintf (stderr, "Out of memory\n");
84         return 1;
85     }
86
87     for (i = 0; i < exclude_tags_length; i++)
88         notmuch_query_add_tag_exclude (query, exclude_tags[i]);
89
90     switch (output) {
91     case OUTPUT_MESSAGES:
92         printf ("%u", notmuch_query_count_messages (query));
93         break;
94     case OUTPUT_THREADS:
95         printf ("%u", notmuch_query_count_threads (query));
96         break;
97     case OUTPUT_FILES:
98         count = count_files (query);
99         if (count >= 0) {
100             printf ("%u", count);
101         } else {
102             ret = -1;
103             goto DONE;
104         }
105         break;
106     }
107
108     if (print_lastmod) {
109         revision = notmuch_database_get_revision (notmuch, &uuid);
110         printf ("\t%s\t%lu\n", uuid, revision);
111     } else {
112         fputs ("\n", stdout);
113     }
114
115   DONE:
116     notmuch_query_destroy (query);
117
118     return ret;
119 }
120
121 static int
122 count_file (notmuch_database_t *notmuch, FILE *input, const char **exclude_tags,
123             size_t exclude_tags_length, int output, int print_lastmod)
124 {
125     char *line = NULL;
126     ssize_t line_len;
127     size_t line_size;
128     int ret = 0;
129
130     while (! ret && (line_len = getline (&line, &line_size, input)) != -1) {
131         chomp_newline (line);
132         ret = print_count (notmuch, line, exclude_tags, exclude_tags_length,
133                            output, print_lastmod);
134     }
135
136     if (line)
137         free (line);
138
139     return ret;
140 }
141
142 int
143 notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
144 {
145     notmuch_database_t *notmuch;
146     char *query_str;
147     int opt_index;
148     int output = OUTPUT_MESSAGES;
149     int exclude = EXCLUDE_TRUE;
150     const char **search_exclude_tags = NULL;
151     size_t search_exclude_tags_length = 0;
152     notmuch_bool_t batch = FALSE;
153     notmuch_bool_t print_lastmod = FALSE;
154     FILE *input = stdin;
155     char *input_file_name = NULL;
156     int ret;
157
158     notmuch_opt_desc_t options[] = {
159         { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
160           (notmuch_keyword_t []){ { "threads", OUTPUT_THREADS },
161                                   { "messages", OUTPUT_MESSAGES },
162                                   { "files", OUTPUT_FILES },
163                                   { 0, 0 } } },
164         { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
165           (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
166                                   { "false", EXCLUDE_FALSE },
167                                   { 0, 0 } } },
168         { NOTMUCH_OPT_BOOLEAN, &print_lastmod, "lastmod", 'l', 0 },
169         { NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },
170         { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
171         { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
172         { 0, 0, 0, 0, 0 }
173     };
174
175     opt_index = parse_arguments (argc, argv, options, 1);
176     if (opt_index < 0)
177         return EXIT_FAILURE;
178
179     notmuch_process_shared_options (argv[0]);
180
181     if (input_file_name) {
182         batch = TRUE;
183         input = fopen (input_file_name, "r");
184         if (input == NULL) {
185             fprintf (stderr, "Error opening %s for reading: %s\n",
186                      input_file_name, strerror (errno));
187             return EXIT_FAILURE;
188         }
189     }
190
191     if (batch && opt_index != argc) {
192         fprintf (stderr, "--batch and query string are not compatible\n");
193         return EXIT_FAILURE;
194     }
195
196     if (notmuch_database_open (notmuch_config_get_database_path (config),
197                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
198         return EXIT_FAILURE;
199
200     notmuch_exit_if_unmatched_db_uuid (notmuch);
201
202     query_str = query_string_from_args (config, argc - opt_index, argv + opt_index);
203     if (query_str == NULL) {
204         fprintf (stderr, "Out of memory.\n");
205         return EXIT_FAILURE;
206     }
207
208     if (exclude == EXCLUDE_TRUE) {
209         search_exclude_tags = notmuch_config_get_search_exclude_tags
210             (config, &search_exclude_tags_length);
211     }
212
213     if (batch)
214         ret = count_file (notmuch, input, search_exclude_tags,
215                           search_exclude_tags_length, output, print_lastmod);
216     else
217         ret = print_count (notmuch, query_str, search_exclude_tags,
218                            search_exclude_tags_length, output, print_lastmod);
219
220     notmuch_database_destroy (notmuch);
221
222     if (input != stdin)
223         fclose (input);
224
225     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
226 }