]> git.notmuchmail.org Git - notmuch/blob - notmuch-count.c
cli: add utility routine to print error status.
[notmuch] / notmuch-count.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  * Copyright © 2009 Keith Packard
5  *
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.
10  *
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.
15  *
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/ .
18  *
19  * Author: Keith Packard <keithp@keithp.com>
20  */
21
22 #include "notmuch-client.h"
23
24 enum {
25     OUTPUT_THREADS,
26     OUTPUT_MESSAGES,
27     OUTPUT_FILES,
28 };
29
30 /* The following is to allow future options to be added more easily */
31 enum {
32     EXCLUDE_TRUE,
33     EXCLUDE_FALSE,
34 };
35
36 static unsigned int
37 count_files (notmuch_query_t *query)
38 {
39     notmuch_messages_t *messages;
40     notmuch_message_t *message;
41     notmuch_filenames_t *filenames;
42     unsigned int count = 0;
43
44     messages = notmuch_query_search_messages (query);
45     if (messages == NULL)
46         return 0;
47
48     for (;
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);
53
54         for (;
55              notmuch_filenames_valid (filenames);
56              notmuch_filenames_move_to_next (filenames))
57             count++;
58
59         notmuch_filenames_destroy (filenames);
60         notmuch_message_destroy (message);
61     }
62
63     notmuch_messages_destroy (messages);
64
65     return count;
66 }
67
68 static int
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)
71 {
72     notmuch_query_t *query;
73     size_t i;
74     unsigned long revision;
75     const char *uuid;
76     int ret = 0;
77
78     query = notmuch_query_create (notmuch, query_str);
79     if (query == NULL) {
80         fprintf (stderr, "Out of memory\n");
81         return 1;
82     }
83
84     for (i = 0; i < exclude_tags_length; i++)
85         notmuch_query_add_tag_exclude (query, exclude_tags[i]);
86
87     switch (output) {
88     case OUTPUT_MESSAGES:
89         printf ("%u", notmuch_query_count_messages (query));
90         break;
91     case OUTPUT_THREADS:
92         printf ("%u", notmuch_query_count_threads (query));
93         break;
94     case OUTPUT_FILES:
95         printf ("%u", count_files (query));
96         break;
97     }
98
99     if (print_lastmod) {
100         revision = notmuch_database_get_revision (notmuch, &uuid);
101         printf ("\t%s\t%lu\n", uuid, revision);
102     } else {
103         fputs ("\n", stdout);
104     }
105
106     notmuch_query_destroy (query);
107
108     return ret;
109 }
110
111 static int
112 count_file (notmuch_database_t *notmuch, FILE *input, const char **exclude_tags,
113             size_t exclude_tags_length, int output, int print_lastmod)
114 {
115     char *line = NULL;
116     ssize_t line_len;
117     size_t line_size;
118     int ret = 0;
119
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);
124     }
125
126     if (line)
127         free (line);
128
129     return ret;
130 }
131
132 int
133 notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
134 {
135     notmuch_database_t *notmuch;
136     char *query_str;
137     int opt_index;
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;
144     FILE *input = stdin;
145     char *input_file_name = NULL;
146     int ret;
147
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 },
153                                   { 0, 0 } } },
154         { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
155           (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
156                                   { "false", EXCLUDE_FALSE },
157                                   { 0, 0 } } },
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 *) &notmuch_shared_options, NULL, 0, 0 },
162         { 0, 0, 0, 0, 0 }
163     };
164
165     opt_index = parse_arguments (argc, argv, options, 1);
166     if (opt_index < 0)
167         return EXIT_FAILURE;
168
169     notmuch_process_shared_options (argv[0]);
170
171     if (input_file_name) {
172         batch = TRUE;
173         input = fopen (input_file_name, "r");
174         if (input == NULL) {
175             fprintf (stderr, "Error opening %s for reading: %s\n",
176                      input_file_name, strerror (errno));
177             return EXIT_FAILURE;
178         }
179     }
180
181     if (batch && opt_index != argc) {
182         fprintf (stderr, "--batch and query string are not compatible\n");
183         return EXIT_FAILURE;
184     }
185
186     if (notmuch_database_open (notmuch_config_get_database_path (config),
187                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
188         return EXIT_FAILURE;
189
190     notmuch_exit_if_unmatched_db_uuid (notmuch);
191
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");
195         return EXIT_FAILURE;
196     }
197
198     if (exclude == EXCLUDE_TRUE) {
199         search_exclude_tags = notmuch_config_get_search_exclude_tags
200             (config, &search_exclude_tags_length);
201     }
202
203     if (batch)
204         ret = count_file (notmuch, input, search_exclude_tags,
205                           search_exclude_tags_length, output, print_lastmod);
206     else
207         ret = print_count (notmuch, query_str, search_exclude_tags,
208                            search_exclude_tags_length, output, print_lastmod);
209
210     notmuch_database_destroy (notmuch);
211
212     if (input != stdin)
213         fclose (input);
214
215     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
216 }