]> git.notmuchmail.org Git - notmuch/blob - notmuch-count.c
cli: drop unused code from notmuch count
[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 int
25 notmuch_count_command (void *ctx, int argc, char *argv[])
26 {
27     notmuch_config_t *config;
28     notmuch_database_t *notmuch;
29     notmuch_query_t *query;
30     char *query_str;
31     int i;
32
33     argc--; argv++; /* skip subcommand argument */
34
35     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
36         if (strcmp (argv[i], "--") == 0) {
37             i++;
38             break;
39         }
40         {
41             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
42             return 1;
43         }
44     }
45
46     argc -= i;
47     argv += i;
48
49     config = notmuch_config_open (ctx, NULL, NULL);
50     if (config == NULL)
51         return 1;
52
53     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
54                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
55     if (notmuch == NULL)
56         return 1;
57
58     query_str = query_string_from_args (ctx, argc, argv);
59     if (query_str == NULL) {
60         fprintf (stderr, "Out of memory.\n");
61         return 1;
62     }
63
64     if (*query_str == '\0') {
65         query_str = talloc_strdup (ctx, "");
66     }
67
68     query = notmuch_query_create (notmuch, query_str);
69     if (query == NULL) {
70         fprintf (stderr, "Out of memory\n");
71         return 1;
72     }
73
74     printf ("%u\n", notmuch_query_count_messages(query));
75
76     notmuch_query_destroy (query);
77     notmuch_database_close (notmuch);
78
79     return 0;
80 }