From: Carl Worth Date: Sun, 25 Oct 2009 05:16:10 +0000 (-0700) Subject: Add a preliminary "notmuch search" command. X-Git-Tag: 0.1~730 X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=commitdiff_plain;h=144b2cbc5532e000c37b4d5ba7bb5d9ea113859f Add a preliminary "notmuch search" command. This isn't behaving at all like it's documented yet, (for example, it's returning message IDs not thread IDs[*]). In fact, the output code is just a copy of the body of "notmuch dump", so all you get for now is message ID and tags. But this should at least be enough to start exercising the query functionality, (which is currently very buggy). [*] I'll want to convert the databse to store thread documents before fixing that. --- diff --git a/notmuch.c b/notmuch.c index 515ec213..4ca8c1cc 100644 --- a/notmuch.c +++ b/notmuch.c @@ -577,8 +577,73 @@ new_command (int argc, char *argv[]) int search_command (int argc, char *argv[]) { - fprintf (stderr, "Error: search is not implemented yet.\n"); - return 1; + void *local = talloc_new (NULL); + notmuch_database_t *notmuch = NULL; + notmuch_query_t *query; + notmuch_results_t *results; + notmuch_message_t *message; + notmuch_tags_t *tags; + char *query_str; + int i; + notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS; + + notmuch = notmuch_database_open (NULL); + if (notmuch == NULL) { + ret = 1; + goto DONE; + } + + /* XXX: Should add xtalloc wrappers here and use them. */ + query_str = talloc_strdup (local, ""); + + for (i = 0; i < argc; i++) { + if (i != 0) + query_str = talloc_asprintf_append (query_str, " "); + + query_str = talloc_asprintf_append (query_str, "%s", argv[i]); + } + + query = notmuch_query_create (notmuch, query_str); + if (query == NULL) { + fprintf (stderr, "Out of memory\n"); + ret = 1; + goto DONE; + } + + for (results = notmuch_query_search (query); + notmuch_results_has_more (results); + notmuch_results_advance (results)) + { + int first = 1; + message = notmuch_results_get (results); + + printf ("%s (", notmuch_message_get_message_id (message)); + + for (tags = notmuch_message_get_tags (message); + notmuch_tags_has_more (tags); + notmuch_tags_advance (tags)) + { + if (! first) + printf (" "); + + printf ("%s", notmuch_tags_get (tags)); + + first = 0; + } + + printf (")\n"); + + notmuch_message_destroy (message); + } + + notmuch_query_destroy (query); + + DONE: + if (notmuch) + notmuch_database_close (notmuch); + talloc_free (local); + + return ret; } int