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