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 http://www.gnu.org/licenses/ .
19 * Author: Keith Packard <keithp@keithp.com>
22 #include "notmuch-client.h"
31 /* The following is to allow future options to be added more easily */
38 count_files (notmuch_query_t *query)
40 notmuch_messages_t *messages;
41 notmuch_message_t *message;
42 notmuch_filenames_t *filenames;
43 unsigned int count = 0;
45 messages = notmuch_query_search_messages (query);
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);
56 notmuch_filenames_valid (filenames);
57 notmuch_filenames_move_to_next (filenames))
60 notmuch_filenames_destroy (filenames);
61 notmuch_message_destroy (message);
64 notmuch_messages_destroy (messages);
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)
73 notmuch_query_t *query;
75 unsigned long revision;
79 query = notmuch_query_create (notmuch, query_str);
81 fprintf (stderr, "Out of memory\n");
85 for (i = 0; i < exclude_tags_length; i++)
86 notmuch_query_add_tag_exclude (query, exclude_tags[i]);
90 printf ("%u", notmuch_query_count_messages (query));
93 printf ("%u", notmuch_query_count_threads (query));
96 printf ("%u", count_files (query));
101 revision = notmuch_database_get_revision (notmuch, &uuid);
102 printf ("\t%s\t%lu\n", uuid, revision);
104 fputs ("\n", stdout);
107 notmuch_query_destroy (query);
113 count_file (notmuch_database_t *notmuch, FILE *input, const char **exclude_tags,
114 size_t exclude_tags_length, int output, int print_lastmod)
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);
134 notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
136 notmuch_database_t *notmuch;
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;
146 char *input_file_name = NULL;
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 },
156 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
157 (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
158 { "false", EXCLUDE_FALSE },
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 *) ¬much_shared_options, NULL, 0, 0 },
167 opt_index = parse_arguments (argc, argv, options, 1);
171 notmuch_process_shared_options (argv[0]);
173 if (input_file_name) {
175 input = fopen (input_file_name, "r");
177 fprintf (stderr, "Error opening %s for reading: %s\n",
178 input_file_name, strerror (errno));
183 if (batch && opt_index != argc) {
184 fprintf (stderr, "--batch and query string are not compatible\n");
188 if (notmuch_database_open (notmuch_config_get_database_path (config),
189 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
192 notmuch_exit_if_unmatched_db_uuid (notmuch);
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");
200 if (exclude == EXCLUDE_TRUE) {
201 search_exclude_tags = notmuch_config_get_search_exclude_tags
202 (config, &search_exclude_tags_length);
206 ret = count_file (notmuch, input, search_exclude_tags,
207 search_exclude_tags_length, output, print_lastmod);
209 ret = print_count (notmuch, query_str, search_exclude_tags,
210 search_exclude_tags_length, output, print_lastmod);
212 notmuch_database_destroy (notmuch);
217 return ret ? EXIT_FAILURE : EXIT_SUCCESS;