]> git.notmuchmail.org Git - notmuch/blob - notmuch-count.c
NEWS: key bindings for next/previous thread
[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 };
28
29 /* The following is to allow future options to be added more easily */
30 enum {
31     EXCLUDE_TRUE,
32     EXCLUDE_FALSE,
33 };
34
35 static int
36 print_count (notmuch_database_t *notmuch, const char *query_str,
37              const char **exclude_tags, size_t exclude_tags_length, int output)
38 {
39     notmuch_query_t *query;
40     size_t i;
41
42     query = notmuch_query_create (notmuch, query_str);
43     if (query == NULL) {
44         fprintf (stderr, "Out of memory\n");
45         return 1;
46     }
47
48     for (i = 0; i < exclude_tags_length; i++)
49         notmuch_query_add_tag_exclude (query, exclude_tags[i]);
50
51     switch (output) {
52     case OUTPUT_MESSAGES:
53         printf ("%u\n", notmuch_query_count_messages (query));
54         break;
55     case OUTPUT_THREADS:
56         printf ("%u\n", notmuch_query_count_threads (query));
57         break;
58     }
59
60     notmuch_query_destroy (query);
61
62     return 0;
63 }
64
65 static int
66 count_file (notmuch_database_t *notmuch, FILE *input, const char **exclude_tags,
67             size_t exclude_tags_length, int output)
68 {
69     char *line = NULL;
70     ssize_t line_len;
71     size_t line_size;
72     int ret = 0;
73
74     while (!ret && (line_len = getline (&line, &line_size, input)) != -1) {
75         chomp_newline (line);
76         ret = print_count (notmuch, line, exclude_tags, exclude_tags_length,
77                            output);
78     }
79
80     if (line)
81         free (line);
82
83     return ret;
84 }
85
86 int
87 notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
88 {
89     notmuch_database_t *notmuch;
90     char *query_str;
91     int opt_index;
92     int output = OUTPUT_MESSAGES;
93     int exclude = EXCLUDE_TRUE;
94     const char **search_exclude_tags = NULL;
95     size_t search_exclude_tags_length = 0;
96     notmuch_bool_t batch = FALSE;
97     FILE *input = stdin;
98     char *input_file_name = NULL;
99     int ret;
100
101     notmuch_opt_desc_t options[] = {
102         { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
103           (notmuch_keyword_t []){ { "threads", OUTPUT_THREADS },
104                                   { "messages", OUTPUT_MESSAGES },
105                                   { 0, 0 } } },
106         { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
107           (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
108                                   { "false", EXCLUDE_FALSE },
109                                   { 0, 0 } } },
110         { NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },
111         { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
112         { 0, 0, 0, 0, 0 }
113     };
114
115     opt_index = parse_arguments (argc, argv, options, 1);
116
117     if (opt_index < 0) {
118         return 1;
119     }
120
121     if (input_file_name) {
122         batch = TRUE;
123         input = fopen (input_file_name, "r");
124         if (input == NULL) {
125             fprintf (stderr, "Error opening %s for reading: %s\n",
126                      input_file_name, strerror (errno));
127             return 1;
128         }
129     }
130
131     if (batch && opt_index != argc) {
132         fprintf (stderr, "--batch and query string are not compatible\n");
133         return 1;
134     }
135
136     if (notmuch_database_open (notmuch_config_get_database_path (config),
137                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
138         return 1;
139
140     query_str = query_string_from_args (config, argc-opt_index, argv+opt_index);
141     if (query_str == NULL) {
142         fprintf (stderr, "Out of memory.\n");
143         return 1;
144     }
145
146     if (exclude == EXCLUDE_TRUE) {
147         search_exclude_tags = notmuch_config_get_search_exclude_tags
148             (config, &search_exclude_tags_length);
149     }
150
151     if (batch)
152         ret = count_file (notmuch, input, search_exclude_tags,
153                           search_exclude_tags_length, output);
154     else
155         ret = print_count (notmuch, query_str, search_exclude_tags,
156                            search_exclude_tags_length, output);
157
158     notmuch_database_destroy (notmuch);
159
160     if (input != stdin)
161         fclose (input);
162
163     return ret;
164 }