]> git.notmuchmail.org Git - notmuch/blob - notmuch-count.c
cli: add print_status_message()
[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 https://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 /* Return the number of files matching the query, or -1 for an error */
31 static int
32 count_files (notmuch_query_t *query)
33 {
34     notmuch_messages_t *messages;
35     notmuch_message_t *message;
36     notmuch_filenames_t *filenames;
37     notmuch_status_t status;
38     int count = 0;
39
40     status = notmuch_query_search_messages (query, &messages);
41     if (print_status_query ("notmuch count", query, status))
42         return -1;
43
44     for (;
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);
49
50         for (;
51              notmuch_filenames_valid (filenames);
52              notmuch_filenames_move_to_next (filenames))
53             count++;
54
55         notmuch_filenames_destroy (filenames);
56         notmuch_message_destroy (message);
57     }
58
59     notmuch_messages_destroy (messages);
60
61     return count;
62 }
63
64 /* return 0 on success, -1 on failure */
65 static int
66 print_count (notmuch_database_t *notmuch, const char *query_str,
67              const char **exclude_tags, size_t exclude_tags_length, int output, int print_lastmod)
68 {
69     notmuch_query_t *query;
70     size_t i;
71     int count;
72     unsigned int ucount;
73     unsigned long revision;
74     const char *uuid;
75     int ret = 0;
76     notmuch_status_t status;
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         status = notmuch_query_add_tag_exclude (query, exclude_tags[i]);
86         if (status && status != NOTMUCH_STATUS_IGNORED) {
87             print_status_query ("notmuch count", query, status);
88             return -1;
89         }
90     }
91
92     switch (output) {
93     case OUTPUT_MESSAGES:
94         status = notmuch_query_count_messages (query, &ucount);
95         if (print_status_query ("notmuch count", query, status))
96             return -1;
97         printf ("%u", ucount);
98         break;
99     case OUTPUT_THREADS:
100         status = notmuch_query_count_threads (query, &ucount);
101         if (print_status_query ("notmuch count", query, status))
102             return -1;
103         printf ("%u", ucount);
104         break;
105     case OUTPUT_FILES:
106         count = count_files (query);
107         if (count >= 0) {
108             printf ("%d", count);
109         } else {
110             ret = -1;
111             goto DONE;
112         }
113         break;
114     }
115
116     if (print_lastmod) {
117         revision = notmuch_database_get_revision (notmuch, &uuid);
118         printf ("\t%s\t%lu\n", uuid, revision);
119     } else {
120         fputs ("\n", stdout);
121     }
122
123   DONE:
124     notmuch_query_destroy (query);
125
126     return ret;
127 }
128
129 static int
130 count_file (notmuch_database_t *notmuch, FILE *input, const char **exclude_tags,
131             size_t exclude_tags_length, int output, int print_lastmod)
132 {
133     char *line = NULL;
134     ssize_t line_len;
135     size_t line_size;
136     int ret = 0;
137
138     while (! ret && (line_len = getline (&line, &line_size, input)) != -1) {
139         chomp_newline (line);
140         ret = print_count (notmuch, line, exclude_tags, exclude_tags_length,
141                            output, print_lastmod);
142     }
143
144     if (line)
145         free (line);
146
147     return ret;
148 }
149
150 int
151 notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
152 {
153     notmuch_database_t *notmuch;
154     char *query_str;
155     int opt_index;
156     int output = OUTPUT_MESSAGES;
157     bool exclude = true;
158     const char **search_exclude_tags = NULL;
159     size_t search_exclude_tags_length = 0;
160     bool batch = false;
161     bool print_lastmod = false;
162     FILE *input = stdin;
163     const char *input_file_name = NULL;
164     int ret;
165
166     notmuch_opt_desc_t options[] = {
167         { .opt_keyword = &output, .name = "output", .keywords =
168           (notmuch_keyword_t []){ { "threads", OUTPUT_THREADS },
169                                   { "messages", OUTPUT_MESSAGES },
170                                   { "files", OUTPUT_FILES },
171                                   { 0, 0 } } },
172         { .opt_bool = &exclude, .name = "exclude" },
173         { .opt_bool = &print_lastmod, .name = "lastmod" },
174         { .opt_bool = &batch, .name = "batch" },
175         { .opt_string = &input_file_name, .name = "input" },
176         { .opt_inherit = notmuch_shared_options },
177         { }
178     };
179
180     opt_index = parse_arguments (argc, argv, options, 1);
181     if (opt_index < 0)
182         return EXIT_FAILURE;
183
184     notmuch_process_shared_options (argv[0]);
185
186     if (input_file_name) {
187         batch = true;
188         input = fopen (input_file_name, "r");
189         if (input == NULL) {
190             fprintf (stderr, "Error opening %s for reading: %s\n",
191                      input_file_name, strerror (errno));
192             return EXIT_FAILURE;
193         }
194     }
195
196     if (batch && opt_index != argc) {
197         fprintf (stderr, "--batch and query string are not compatible\n");
198         if (input)
199             fclose (input);
200         return EXIT_FAILURE;
201     }
202
203     if (notmuch_database_open (notmuch_config_get_database_path (config),
204                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
205         return EXIT_FAILURE;
206
207     notmuch_exit_if_unmatched_db_uuid (notmuch);
208
209     query_str = query_string_from_args (config, argc - opt_index, argv + opt_index);
210     if (query_str == NULL) {
211         fprintf (stderr, "Out of memory.\n");
212         return EXIT_FAILURE;
213     }
214
215     if (exclude) {
216         search_exclude_tags = notmuch_config_get_search_exclude_tags
217             (config, &search_exclude_tags_length);
218     }
219
220     if (batch)
221         ret = count_file (notmuch, input, search_exclude_tags,
222                           search_exclude_tags_length, output, print_lastmod);
223     else
224         ret = print_count (notmuch, query_str, search_exclude_tags,
225                            search_exclude_tags_length, output, print_lastmod);
226
227     notmuch_database_destroy (notmuch);
228
229     if (input != stdin)
230         fclose (input);
231
232     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
233 }