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"
30 /* The following is to allow future options to be added more easily */
37 count_files (notmuch_query_t *query)
39 notmuch_messages_t *messages;
40 notmuch_message_t *message;
41 notmuch_filenames_t *filenames;
42 unsigned int count = 0;
44 messages = notmuch_query_search_messages (query);
49 notmuch_messages_valid (messages);
50 notmuch_messages_move_to_next (messages)) {
51 message = notmuch_messages_get (messages);
52 filenames = notmuch_message_get_filenames (message);
55 notmuch_filenames_valid (filenames);
56 notmuch_filenames_move_to_next (filenames))
59 notmuch_filenames_destroy (filenames);
60 notmuch_message_destroy (message);
63 notmuch_messages_destroy (messages);
69 print_count (notmuch_database_t *notmuch, const char *query_str,
70 const char **exclude_tags, size_t exclude_tags_length, int output, int print_lastmod)
72 notmuch_query_t *query;
74 unsigned long revision;
78 query = notmuch_query_create (notmuch, query_str);
80 fprintf (stderr, "Out of memory\n");
84 for (i = 0; i < exclude_tags_length; i++)
85 notmuch_query_add_tag_exclude (query, exclude_tags[i]);
89 printf ("%u", notmuch_query_count_messages (query));
92 printf ("%u", notmuch_query_count_threads (query));
95 printf ("%u", count_files (query));
100 revision = notmuch_database_get_revision (notmuch, &uuid);
101 printf ("\t%s\t%lu\n", uuid, revision);
103 fputs ("\n", stdout);
106 notmuch_query_destroy (query);
112 count_file (notmuch_database_t *notmuch, FILE *input, const char **exclude_tags,
113 size_t exclude_tags_length, int output, int print_lastmod)
120 while (! ret && (line_len = getline (&line, &line_size, input)) != -1) {
121 chomp_newline (line);
122 ret = print_count (notmuch, line, exclude_tags, exclude_tags_length,
123 output, print_lastmod);
133 notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
135 notmuch_database_t *notmuch;
138 int output = OUTPUT_MESSAGES;
139 int exclude = EXCLUDE_TRUE;
140 const char **search_exclude_tags = NULL;
141 size_t search_exclude_tags_length = 0;
142 notmuch_bool_t batch = FALSE;
143 notmuch_bool_t print_lastmod = FALSE;
145 char *input_file_name = NULL;
148 notmuch_opt_desc_t options[] = {
149 { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
150 (notmuch_keyword_t []){ { "threads", OUTPUT_THREADS },
151 { "messages", OUTPUT_MESSAGES },
152 { "files", OUTPUT_FILES },
154 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
155 (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
156 { "false", EXCLUDE_FALSE },
158 { NOTMUCH_OPT_BOOLEAN, &print_lastmod, "lastmod", 'l', 0 },
159 { NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },
160 { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
161 { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },
165 opt_index = parse_arguments (argc, argv, options, 1);
169 notmuch_process_shared_options (argv[0]);
171 if (input_file_name) {
173 input = fopen (input_file_name, "r");
175 fprintf (stderr, "Error opening %s for reading: %s\n",
176 input_file_name, strerror (errno));
181 if (batch && opt_index != argc) {
182 fprintf (stderr, "--batch and query string are not compatible\n");
186 if (notmuch_database_open (notmuch_config_get_database_path (config),
187 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
190 notmuch_exit_if_unmatched_db_uuid (notmuch);
192 query_str = query_string_from_args (config, argc - opt_index, argv + opt_index);
193 if (query_str == NULL) {
194 fprintf (stderr, "Out of memory.\n");
198 if (exclude == EXCLUDE_TRUE) {
199 search_exclude_tags = notmuch_config_get_search_exclude_tags
200 (config, &search_exclude_tags_length);
204 ret = count_file (notmuch, input, search_exclude_tags,
205 search_exclude_tags_length, output, print_lastmod);
207 ret = print_count (notmuch, query_str, search_exclude_tags,
208 search_exclude_tags_length, output, print_lastmod);
210 notmuch_database_destroy (notmuch);
215 return ret ? EXIT_FAILURE : EXIT_SUCCESS;