X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=notmuch.c;h=4ca8c1cc04f95a732cd3ff71ca481d6edc308a86;hp=515ec213f2f0d49c618ed3a9a92724a3dbab6b8b;hb=144b2cbc5532e000c37b4d5ba7bb5d9ea113859f;hpb=0e72d6a9ecf392ec65a23b7c81d33025eaee3e1a 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