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 status = notmuch_query_create_with_syntax (notmuch, query_str,
78 shared_option_query_syntax (),
80 if (print_status_database ("notmuch count", notmuch, status)) {
85 for (notmuch_config_values_start (exclude_tags);
86 notmuch_config_values_valid (exclude_tags);
87 notmuch_config_values_move_to_next (exclude_tags)) {
89 status = notmuch_query_add_tag_exclude (query,
90 notmuch_config_values_get (exclude_tags));
91 if (status && status != NOTMUCH_STATUS_IGNORED) {
92 print_status_query ("notmuch count", query, status);
100 status = notmuch_query_count_messages (query, &ucount);
101 if (print_status_query ("notmuch count", query, status))
103 printf ("%u", ucount);
106 status = notmuch_query_count_threads (query, &ucount);
107 if (print_status_query ("notmuch count", query, status))
109 printf ("%u", ucount);
112 count = count_files (query);
114 printf ("%d", count);
123 revision = notmuch_database_get_revision (notmuch, &uuid);
124 printf ("\t%s\t%lu\n", uuid, revision);
126 fputs ("\n", stdout);
130 notmuch_query_destroy (query);
136 count_file (notmuch_database_t *notmuch, FILE *input, notmuch_config_values_t *exclude_tags,
137 int output, int print_lastmod)
144 while (! ret && (line_len = getline (&line, &line_size, input)) != -1) {
145 chomp_newline (line);
146 ret = print_count (notmuch, line, exclude_tags, output, print_lastmod);
156 notmuch_count_command (notmuch_database_t *notmuch, int argc, char *argv[])
160 int output = OUTPUT_MESSAGES;
162 notmuch_config_values_t *exclude_tags = NULL;
164 bool print_lastmod = false;
166 const char *input_file_name = NULL;
169 notmuch_opt_desc_t options[] = {
170 { .opt_keyword = &output, .name = "output", .keywords =
171 (notmuch_keyword_t []){ { "threads", OUTPUT_THREADS },
172 { "messages", OUTPUT_MESSAGES },
173 { "files", OUTPUT_FILES },
175 { .opt_bool = &exclude, .name = "exclude" },
176 { .opt_bool = &print_lastmod, .name = "lastmod" },
177 { .opt_bool = &batch, .name = "batch" },
178 { .opt_string = &input_file_name, .name = "input" },
179 { .opt_inherit = notmuch_shared_options },
183 opt_index = parse_arguments (argc, argv, options, 1);
187 notmuch_process_shared_options (notmuch, argv[0]);
189 if (input_file_name) {
191 input = fopen (input_file_name, "r");
193 fprintf (stderr, "Error opening %s for reading: %s\n",
194 input_file_name, strerror (errno));
199 if (batch && opt_index != argc) {
200 fprintf (stderr, "--batch and query string are not compatible\n");
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;