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"
29 /* The following is to allow future options to be added more easily */
36 print_count (notmuch_database_t *notmuch, const char *query_str,
37 const char **exclude_tags, size_t exclude_tags_length, int output)
39 notmuch_query_t *query;
42 query = notmuch_query_create (notmuch, query_str);
44 fprintf (stderr, "Out of memory\n");
48 for (i = 0; i < exclude_tags_length; i++)
49 notmuch_query_add_tag_exclude (query, exclude_tags[i]);
53 printf ("%u\n", notmuch_query_count_messages (query));
56 printf ("%u\n", notmuch_query_count_threads (query));
60 notmuch_query_destroy (query);
66 notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
68 notmuch_database_t *notmuch;
71 int output = OUTPUT_MESSAGES;
72 int exclude = EXCLUDE_TRUE;
73 const char **search_exclude_tags = NULL;
74 size_t search_exclude_tags_length = 0;
77 notmuch_opt_desc_t options[] = {
78 { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
79 (notmuch_keyword_t []){ { "threads", OUTPUT_THREADS },
80 { "messages", OUTPUT_MESSAGES },
82 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
83 (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
84 { "false", EXCLUDE_FALSE },
89 opt_index = parse_arguments (argc, argv, options, 1);
95 if (notmuch_database_open (notmuch_config_get_database_path (config),
96 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
99 query_str = query_string_from_args (config, argc-opt_index, argv+opt_index);
100 if (query_str == NULL) {
101 fprintf (stderr, "Out of memory.\n");
105 if (exclude == EXCLUDE_TRUE) {
106 search_exclude_tags = notmuch_config_get_search_exclude_tags
107 (config, &search_exclude_tags_length);
110 ret = print_count (notmuch, query_str, search_exclude_tags,
111 search_exclude_tags_length, output);
113 notmuch_database_destroy (notmuch);