1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
4 * Copyright © 2009 Keith Packard
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see https://www.gnu.org/licenses/ .
19 * Author: Keith Packard <keithp@keithp.com>
22 #include "notmuch-client.h"
30 /* Return the number of files matching the query, or -1 for an error */
32 count_files (notmuch_query_t *query)
34 notmuch_messages_t *messages;
35 notmuch_message_t *message;
36 notmuch_filenames_t *filenames;
37 notmuch_status_t status;
40 status = notmuch_query_search_messages (query, &messages);
41 if (print_status_query ("notmuch count", query, status))
45 notmuch_messages_valid (messages);
46 notmuch_messages_move_to_next (messages)) {
47 message = notmuch_messages_get (messages);
48 filenames = notmuch_message_get_filenames (message);
51 notmuch_filenames_valid (filenames);
52 notmuch_filenames_move_to_next (filenames))
55 notmuch_filenames_destroy (filenames);
56 notmuch_message_destroy (message);
59 notmuch_messages_destroy (messages);
64 /* return 0 on success, -1 on failure */
66 print_count (notmuch_database_t *notmuch, const char *query_str,
67 notmuch_config_values_t *exclude_tags, int output, int print_lastmod)
69 notmuch_query_t *query;
72 unsigned long revision;
75 notmuch_status_t status;
77 query = notmuch_query_create (notmuch, query_str);
79 fprintf (stderr, "Out of memory\n");
83 for (notmuch_config_values_start (exclude_tags);
84 notmuch_config_values_valid (exclude_tags);
85 notmuch_config_values_move_to_next (exclude_tags)) {
87 status = notmuch_query_add_tag_exclude (query,
88 notmuch_config_values_get (exclude_tags));
89 if (status && status != NOTMUCH_STATUS_IGNORED) {
90 print_status_query ("notmuch count", query, status);
98 status = notmuch_query_count_messages (query, &ucount);
99 if (print_status_query ("notmuch count", query, status))
101 printf ("%u", ucount);
104 status = notmuch_query_count_threads (query, &ucount);
105 if (print_status_query ("notmuch count", query, status))
107 printf ("%u", ucount);
110 count = count_files (query);
112 printf ("%d", count);
121 revision = notmuch_database_get_revision (notmuch, &uuid);
122 printf ("\t%s\t%lu\n", uuid, revision);
124 fputs ("\n", stdout);
128 notmuch_query_destroy (query);
134 count_file (notmuch_database_t *notmuch, FILE *input, notmuch_config_values_t *exclude_tags,
135 int output, int print_lastmod)
142 while (! ret && (line_len = getline (&line, &line_size, input)) != -1) {
143 chomp_newline (line);
144 ret = print_count (notmuch, line, exclude_tags, output, print_lastmod);
154 notmuch_count_command (notmuch_database_t *notmuch, int argc, char *argv[])
158 int output = OUTPUT_MESSAGES;
160 notmuch_config_values_t *exclude_tags = NULL;
162 bool print_lastmod = false;
164 const char *input_file_name = NULL;
167 notmuch_opt_desc_t options[] = {
168 { .opt_keyword = &output, .name = "output", .keywords =
169 (notmuch_keyword_t []){ { "threads", OUTPUT_THREADS },
170 { "messages", OUTPUT_MESSAGES },
171 { "files", OUTPUT_FILES },
173 { .opt_bool = &exclude, .name = "exclude" },
174 { .opt_bool = &print_lastmod, .name = "lastmod" },
175 { .opt_bool = &batch, .name = "batch" },
176 { .opt_string = &input_file_name, .name = "input" },
177 { .opt_inherit = notmuch_shared_options },
181 opt_index = parse_arguments (argc, argv, options, 1);
185 notmuch_process_shared_options (argv[0]);
187 if (input_file_name) {
189 input = fopen (input_file_name, "r");
191 fprintf (stderr, "Error opening %s for reading: %s\n",
192 input_file_name, strerror (errno));
197 if (batch && opt_index != argc) {
198 fprintf (stderr, "--batch and query string are not compatible\n");
204 notmuch_exit_if_unmatched_db_uuid (notmuch);
206 query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
207 if (query_str == NULL) {
208 fprintf (stderr, "Out of memory.\n");
213 exclude_tags = notmuch_config_get_values (notmuch, NOTMUCH_CONFIG_EXCLUDE_TAGS);
217 ret = count_file (notmuch, input, exclude_tags, output, print_lastmod);
219 ret = print_count (notmuch, query_str, exclude_tags, output, print_lastmod);
221 notmuch_database_destroy (notmuch);
226 return ret ? EXIT_FAILURE : EXIT_SUCCESS;