[RFC PATCH 2/2] cli: add notmuch address command

Jani Nikula jani at nikula.org
Sat Nov 1 04:30:48 PDT 2014


---
 notmuch-client.h |   3 ++
 notmuch-search.c | 128 +++++++++++++++++++++++++++++++++++--------------------
 notmuch.c        |   2 +
 3 files changed, 87 insertions(+), 46 deletions(-)

diff --git a/notmuch-client.h b/notmuch-client.h
index e1efbe0c8252..5e0d47508c6a 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -199,6 +199,9 @@ int
 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]);
 
 int
+notmuch_address_command (notmuch_config_t *config, int argc, char *argv[]);
+
+int
 notmuch_setup_command (notmuch_config_t *config, int argc, char *argv[]);
 
 int
diff --git a/notmuch-search.c b/notmuch-search.c
index 671fe4139981..07755f115776 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -35,6 +35,7 @@ typedef enum {
 #define OUTPUT_ADDRESS_FLAGS (OUTPUT_SENDER | OUTPUT_RECIPIENTS)
 
 typedef struct {
+    notmuch_bool_t address_command;
     sprinter_t *format;
     notmuch_query_t *query;
     notmuch_sort_t sort;
@@ -440,17 +441,11 @@ do_search_tags (notmuch_database_t *notmuch,
     return 0;
 }
 
-int
-notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
+static int
+_notmuch_search_command (notmuch_config_t *config, int argc, char *argv[],
+			 search_options_t *opt, notmuch_opt_desc_t *subopts)
 {
     notmuch_database_t *notmuch;
-    search_options_t opt = {
-	.sort = NOTMUCH_SORT_NEWEST_FIRST,
-	.output = 0,
-	.offset = 0,
-	.limit = -1, /* unlimited */
-	.dupe = -1,
-    };
     char *query_str;
     int opt_index, ret;
     notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;
@@ -464,7 +459,8 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
     } format_sel = NOTMUCH_FORMAT_TEXT;
 
     notmuch_opt_desc_t options[] = {
-	{ NOTMUCH_OPT_KEYWORD, &opt.sort, "sort", 's',
+	{ NOTMUCH_OPT_INHERIT, subopts, NULL, 0, 0 },
+	{ NOTMUCH_OPT_KEYWORD, &opt->sort, "sort", 's',
 	  (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
 				  { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
 				  { 0, 0 } } },
@@ -475,24 +471,15 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
 				  { "text0", NOTMUCH_FORMAT_TEXT0 },
 				  { 0, 0 } } },
 	{ NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 },
-	{ NOTMUCH_OPT_KEYWORD_FLAGS, &opt.output, "output", 'o',
-	  (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
-				  { "threads", OUTPUT_THREADS },
-				  { "messages", OUTPUT_MESSAGES },
-				  { "sender", OUTPUT_SENDER },
-				  { "recipients", OUTPUT_RECIPIENTS },
-				  { "files", OUTPUT_FILES },
-				  { "tags", OUTPUT_TAGS },
-				  { 0, 0 } } },
         { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
           (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
                                   { "false", NOTMUCH_EXCLUDE_FALSE },
                                   { "flag", NOTMUCH_EXCLUDE_FLAG },
                                   { "all", NOTMUCH_EXCLUDE_ALL },
                                   { 0, 0 } } },
-	{ NOTMUCH_OPT_INT, &opt.offset, "offset", 'O', 0 },
-	{ NOTMUCH_OPT_INT, &opt.limit, "limit", 'L', 0  },
-	{ NOTMUCH_OPT_INT, &opt.dupe, "duplicate", 'D', 0  },
+	{ NOTMUCH_OPT_INT, &opt->offset, "offset", 'O', 0 },
+	{ NOTMUCH_OPT_INT, &opt->limit, "limit", 'L', 0  },
+	{ NOTMUCH_OPT_INT, &opt->dupe, "duplicate", 'D', 0  },
 	{ 0, 0, 0, 0, 0 }
     };
 
@@ -500,25 +487,25 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
     if (opt_index < 0)
 	return EXIT_FAILURE;
 
-    if (! opt.output)
-	opt.output = OUTPUT_SUMMARY;
+    if (! opt->output)
+	opt->output = OUTPUT_SUMMARY;
 
     switch (format_sel) {
     case NOTMUCH_FORMAT_TEXT:
-	opt.format = sprinter_text_create (config, stdout);
+	opt->format = sprinter_text_create (config, stdout);
 	break;
     case NOTMUCH_FORMAT_TEXT0:
-	if (opt.output == OUTPUT_SUMMARY) {
+	if (opt->output == OUTPUT_SUMMARY) {
 	    fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
 	    return EXIT_FAILURE;
 	}
-	opt.format = sprinter_text0_create (config, stdout);
+	opt->format = sprinter_text0_create (config, stdout);
 	break;
     case NOTMUCH_FORMAT_JSON:
-	opt.format = sprinter_json_create (config, stdout);
+	opt->format = sprinter_json_create (config, stdout);
 	break;
     case NOTMUCH_FORMAT_SEXP:
-	opt.format = sprinter_sexp_create (config, stdout);
+	opt->format = sprinter_sexp_create (config, stdout);
 	break;
     default:
 	/* this should never happen */
@@ -541,15 +528,15 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
 	return EXIT_FAILURE;
     }
 
-    opt.query = notmuch_query_create (notmuch, query_str);
-    if (opt.query == NULL) {
+    opt->query = notmuch_query_create (notmuch, query_str);
+    if (opt->query == NULL) {
 	fprintf (stderr, "Out of memory\n");
 	return EXIT_FAILURE;
     }
 
-    notmuch_query_set_sort (opt.query, opt.sort);
+    notmuch_query_set_sort (opt->query, opt->sort);
 
-    if (exclude == NOTMUCH_EXCLUDE_FLAG && opt.output != OUTPUT_SUMMARY) {
+    if (exclude == NOTMUCH_EXCLUDE_FLAG && opt->output != OUTPUT_SUMMARY) {
 	/* If we are not doing summary output there is nowhere to
 	 * print the excluded flag so fall back on including the
 	 * excluded messages. */
@@ -564,28 +551,77 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
 	search_exclude_tags = notmuch_config_get_search_exclude_tags
 	    (config, &search_exclude_tags_length);
 	for (i = 0; i < search_exclude_tags_length; i++)
-	    notmuch_query_add_tag_exclude (opt.query, search_exclude_tags[i]);
-	notmuch_query_set_omit_excluded (opt.query, exclude);
+	    notmuch_query_add_tag_exclude (opt->query, search_exclude_tags[i]);
+	notmuch_query_set_omit_excluded (opt->query, exclude);
     }
 
-    if (opt.output == OUTPUT_SUMMARY ||
-	opt.output == OUTPUT_THREADS)
-	ret = do_search_threads (&opt);
-    else if (opt.output == OUTPUT_MESSAGES ||
-	     opt.output == OUTPUT_FILES ||
-	     (opt.output & OUTPUT_ADDRESS_FLAGS && !(opt.output & ~OUTPUT_ADDRESS_FLAGS)))
-	ret = do_search_messages (&opt);
-    else if (opt.output == OUTPUT_TAGS)
-	ret = do_search_tags (notmuch, &opt);
+    if (opt->output == OUTPUT_SUMMARY ||
+	opt->output == OUTPUT_THREADS)
+	ret = do_search_threads (opt);
+    else if (opt->output == OUTPUT_MESSAGES ||
+	     opt->output == OUTPUT_FILES ||
+	     (opt->output & OUTPUT_ADDRESS_FLAGS && !(opt->output & ~OUTPUT_ADDRESS_FLAGS)))
+	ret = do_search_messages (opt);
+    else if (opt->output == OUTPUT_TAGS)
+	ret = do_search_tags (notmuch, opt);
     else {
 	fprintf (stderr, "Error: the combination of outputs is not supported.\n");
 	ret = 1;
     }
 
-    notmuch_query_destroy (opt.query);
+    notmuch_query_destroy (opt->query);
     notmuch_database_destroy (notmuch);
 
-    talloc_free (opt.format);
+    talloc_free (opt->format);
 
     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
 }
+
+int
+notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
+{
+    search_options_t opt = {
+	.address_command = FALSE,
+	.sort = NOTMUCH_SORT_NEWEST_FIRST,
+	.output = 0,
+	.offset = 0,
+	.limit = -1, /* unlimited */
+	.dupe = -1,
+    };
+
+    notmuch_opt_desc_t options[] = {
+	{ NOTMUCH_OPT_KEYWORD_FLAGS, &opt.output, "output", 'o',
+	  (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
+				  { "threads", OUTPUT_THREADS },
+				  { "messages", OUTPUT_MESSAGES },
+				  { "files", OUTPUT_FILES },
+				  { "tags", OUTPUT_TAGS },
+				  { 0, 0 } } },
+	{ 0, 0, 0, 0, 0 }
+    };
+
+    return _notmuch_search_command (config, argc, argv, &opt, options);
+}
+
+int
+notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
+{
+    search_options_t opt = {
+	.address_command = TRUE,
+	.sort = NOTMUCH_SORT_NEWEST_FIRST,
+	.output = 0,
+	.offset = 0,
+	.limit = -1, /* unlimited */
+	.dupe = -1,
+    };
+
+    notmuch_opt_desc_t options[] = {
+	{ NOTMUCH_OPT_KEYWORD_FLAGS, &opt.output, "output", 'o',
+	  (notmuch_keyword_t []){ { "sender", OUTPUT_SENDER },
+				  { "recipients", OUTPUT_RECIPIENTS },
+				  { 0, 0 } } },
+	{ 0, 0, 0, 0, 0 }
+    };
+
+    return _notmuch_search_command (config, argc, argv, &opt, options);
+}
diff --git a/notmuch.c b/notmuch.c
index dcda0392a094..0fac0997865e 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -54,6 +54,8 @@ static command_t commands[] = {
       "Add a new message into the maildir and notmuch database." },
     { "search", notmuch_search_command, FALSE,
       "Search for messages matching the given search terms." },
+    { "address", notmuch_address_command, FALSE,
+      "Get addresses from messages matching the given search terms." },
     { "show", notmuch_show_command, FALSE,
       "Show all messages matching the search terms." },
     { "count", notmuch_count_command, FALSE,
-- 
2.1.1



More information about the notmuch mailing list