1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
4 * Copyright © 2009 Keith Packard
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.
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.
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/ .
19 * Author: Keith Packard <keithp@keithp.com>
22 #include "notmuch-client.h"
25 notmuch_count_command (void *ctx, int argc, char *argv[])
27 notmuch_config_t *config;
28 notmuch_database_t *notmuch;
29 notmuch_query_t *query;
34 int i, first = 0, max_threads = -1;
35 notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
38 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
39 if (strcmp (argv[i], "--") == 0) {
44 if (STRNCMP_LITERAL (argv[i], "--first=") == 0) {
45 opt = argv[i] + sizeof ("--first=") - 1;
46 first = strtoul (opt, &end, 10);
47 if (*opt == '\0' || *end != '\0') {
48 fprintf (stderr, "Invalid value for --first: %s\n", opt);
51 } else if (STRNCMP_LITERAL (argv[i], "--max-threads=") == 0) {
52 opt = argv[i] + sizeof ("--max-threads=") - 1;
53 max_threads = strtoul (opt, &end, 10);
54 if (*opt == '\0' || *end != '\0') {
55 fprintf (stderr, "Invalid value for --max-threads: %s\n", opt);
58 } else if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
59 opt = argv[i] + sizeof ("--sort=") - 1;
60 if (strcmp (opt, "oldest-first") == 0) {
61 sort = NOTMUCH_SORT_OLDEST_FIRST;
62 } else if (strcmp (opt, "newest-first") == 0) {
63 sort = NOTMUCH_SORT_NEWEST_FIRST;
65 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
71 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
79 config = notmuch_config_open (ctx, NULL, NULL);
83 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
84 NOTMUCH_DATABASE_MODE_READ_ONLY);
88 query_str = query_string_from_args (ctx, argc, argv);
89 if (query_str == NULL) {
90 fprintf (stderr, "Out of memory.\n");
93 if (*query_str == '\0') {
94 fprintf (stderr, "Error: notmuch count requires at least one count term.\n");
98 query = notmuch_query_create (notmuch, query_str);
100 fprintf (stderr, "Out of memory\n");
104 printf ("%u\n", notmuch_query_count_messages(query));
106 notmuch_query_destroy (query);
107 notmuch_database_close (notmuch);