]> git.notmuchmail.org Git - notmuch/blob - notmuch-count.c
xutil.c: remove duplicate copies, create new library libutil.a to contain xutil.
[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 #if 0
33     char *opt, *end;
34     int i, first = 0, max_threads = -1;
35     notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
36 #endif
37
38     argc--; argv++; /* skip subcommand argument */
39
40     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
41         if (strcmp (argv[i], "--") == 0) {
42             i++;
43             break;
44         }
45 #if 0
46         if (STRNCMP_LITERAL (argv[i], "--first=") == 0) {
47             opt = argv[i] + sizeof ("--first=") - 1;
48             first = strtoul (opt, &end, 10);
49             if (*opt == '\0' || *end != '\0') {
50                 fprintf (stderr, "Invalid value for --first: %s\n", opt);
51                 return 1;
52             }
53         } else if (STRNCMP_LITERAL (argv[i], "--max-threads=") == 0) {
54             opt = argv[i] + sizeof ("--max-threads=") - 1;
55             max_threads = strtoul (opt, &end, 10);
56             if (*opt == '\0' || *end != '\0') {
57                 fprintf (stderr, "Invalid value for --max-threads: %s\n", opt);
58                 return 1;
59             }
60         } else if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
61             opt = argv[i] + sizeof ("--sort=") - 1;
62             if (strcmp (opt, "oldest-first") == 0) {
63                 sort = NOTMUCH_SORT_OLDEST_FIRST;
64             } else if (strcmp (opt, "newest-first") == 0) {
65                 sort = NOTMUCH_SORT_NEWEST_FIRST;
66             } else {
67                 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
68                 return 1;
69             }
70         } else
71 #endif
72         {
73             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
74             return 1;
75         }
76     }
77
78     argc -= i;
79     argv += i;
80
81     config = notmuch_config_open (ctx, NULL, NULL);
82     if (config == NULL)
83         return 1;
84
85     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
86                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
87     if (notmuch == NULL)
88         return 1;
89
90     query_str = query_string_from_args (ctx, argc, argv);
91     if (query_str == NULL) {
92         fprintf (stderr, "Out of memory.\n");
93         return 1;
94     }
95
96     if (*query_str == '\0') {
97         query_str = talloc_strdup (ctx, "");
98     }
99
100     query = notmuch_query_create (notmuch, query_str);
101     if (query == NULL) {
102         fprintf (stderr, "Out of memory\n");
103         return 1;
104     }
105
106     printf ("%u\n", notmuch_query_count_messages(query));
107
108     notmuch_query_destroy (query);
109     notmuch_database_close (notmuch);
110
111     return 0;
112 }