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 argc--; argv++; /* skip subcommand argument */
40 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
41 if (strcmp (argv[i], "--") == 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);
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);
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;
67 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
73 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
81 config = notmuch_config_open (ctx, NULL, NULL);
85 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
86 NOTMUCH_DATABASE_MODE_READ_ONLY);
90 query_str = query_string_from_args (ctx, argc, argv);
91 if (query_str == NULL) {
92 fprintf (stderr, "Out of memory.\n");
96 if (*query_str == '\0') {
97 query_str = talloc_strdup (ctx, "");
100 query = notmuch_query_create (notmuch, query_str);
102 fprintf (stderr, "Out of memory\n");
106 printf ("%u\n", notmuch_query_count_messages(query));
108 notmuch_query_destroy (query);
109 notmuch_database_close (notmuch);