[PATCH v6 1/7] cli: search: Refactor passing of command line options
authorMichal Sojka <sojkam1@fel.cvut.cz>
Fri, 31 Oct 2014 21:53:55 +0000 (22:53 +0100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 18:05:59 +0000 (10:05 -0800)
12/7113fa9ae4d3347d4f06cedda9c87b8e45be55 [new file with mode: 0644]

diff --git a/12/7113fa9ae4d3347d4f06cedda9c87b8e45be55 b/12/7113fa9ae4d3347d4f06cedda9c87b8e45be55
new file mode 100644 (file)
index 0000000..3856732
--- /dev/null
@@ -0,0 +1,366 @@
+Return-Path: <sojkam1@fel.cvut.cz>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 17EE9431FD9\r
+       for <notmuch@notmuchmail.org>; Fri, 31 Oct 2014 14:54:32 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id WjblHgToqLwL for <notmuch@notmuchmail.org>;\r
+       Fri, 31 Oct 2014 14:54:28 -0700 (PDT)\r
+Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
+       by olra.theworths.org (Postfix) with ESMTP id 5ACAF431FC2\r
+       for <notmuch@notmuchmail.org>; Fri, 31 Oct 2014 14:54:21 -0700 (PDT)\r
+Received: from localhost (unknown [192.168.200.7])\r
+       by max.feld.cvut.cz (Postfix) with ESMTP id B408C5CCE59\r
+       for <notmuch@notmuchmail.org>; Fri, 31 Oct 2014 22:54:19 +0100 (CET)\r
+X-Virus-Scanned: IMAP STYX AMAVIS\r
+Received: from max.feld.cvut.cz ([192.168.200.1])\r
+       by localhost (styx.feld.cvut.cz [192.168.200.7]) (amavisd-new,\r
+       port 10044) with ESMTP id p3_9GN5zbHD4 for <notmuch@notmuchmail.org>;\r
+       Fri, 31 Oct 2014 22:54:14 +0100 (CET)\r
+Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
+       by max.feld.cvut.cz (Postfix) with ESMTP id 9FC205CD10B\r
+       for <notmuch@notmuchmail.org>; Fri, 31 Oct 2014 22:54:14 +0100 (CET)\r
+Received: from wsh by steelpick.2x.cz with local (Exim 4.84)\r
+       (envelope-from <sojkam1@fel.cvut.cz>)\r
+       id 1XkK92-0007hd-Le; Fri, 31 Oct 2014 22:54:08 +0100\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v6 1/7] cli: search: Refactor passing of command line options\r
+Date: Fri, 31 Oct 2014 22:53:55 +0100\r
+Message-Id: <1414792441-29555-2-git-send-email-sojkam1@fel.cvut.cz>\r
+X-Mailer: git-send-email 2.1.1\r
+In-Reply-To: <1414792441-29555-1-git-send-email-sojkam1@fel.cvut.cz>\r
+References: <1414792441-29555-1-git-send-email-sojkam1@fel.cvut.cz>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Fri, 31 Oct 2014 21:54:32 -0000\r
+\r
+Many functions that implement the search command need to access command\r
+line options. Instead of passing each option in a separate variable, put\r
+them in a structure and pass only this structure.\r
+\r
+This will become handy in the following commits.\r
+---\r
+ notmuch-search.c | 125 ++++++++++++++++++++++++++++---------------------------\r
+ 1 file changed, 64 insertions(+), 61 deletions(-)\r
+\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index bc9be45..0c3e972 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -30,6 +30,16 @@ typedef enum {\r
+     OUTPUT_TAGS\r
+ } output_t;\r
\r
++typedef struct {\r
++    sprinter_t *format;\r
++    notmuch_query_t *query;\r
++    notmuch_sort_t sort;\r
++    output_t output;\r
++    int offset;\r
++    int limit;\r
++    int dupe;\r
++} search_options_t;\r
++\r
+ /* Return two stable query strings that identify exactly the matched\r
+  * and unmatched messages currently in thread.  If there are no\r
+  * matched or unmatched messages, the returned buffers will be\r
+@@ -70,43 +80,39 @@ get_thread_query (notmuch_thread_t *thread,\r
+ }\r
\r
+ static int\r
+-do_search_threads (sprinter_t *format,\r
+-                 notmuch_query_t *query,\r
+-                 notmuch_sort_t sort,\r
+-                 output_t output,\r
+-                 int offset,\r
+-                 int limit)\r
++do_search_threads (search_options_t *opt)\r
+ {\r
+     notmuch_thread_t *thread;\r
+     notmuch_threads_t *threads;\r
+     notmuch_tags_t *tags;\r
++    sprinter_t *format = opt->format;\r
+     time_t date;\r
+     int i;\r
\r
+-    if (offset < 0) {\r
+-      offset += notmuch_query_count_threads (query);\r
+-      if (offset < 0)\r
+-          offset = 0;\r
++    if (opt->offset < 0) {\r
++      opt->offset += notmuch_query_count_threads (opt->query);\r
++      if (opt->offset < 0)\r
++          opt->offset = 0;\r
+     }\r
\r
+-    threads = notmuch_query_search_threads (query);\r
++    threads = notmuch_query_search_threads (opt->query);\r
+     if (threads == NULL)\r
+       return 1;\r
\r
+     format->begin_list (format);\r
\r
+     for (i = 0;\r
+-       notmuch_threads_valid (threads) && (limit < 0 || i < offset + limit);\r
++       notmuch_threads_valid (threads) && (opt->limit < 0 || i < opt->offset + opt->limit);\r
+        notmuch_threads_move_to_next (threads), i++)\r
+     {\r
+       thread = notmuch_threads_get (threads);\r
\r
+-      if (i < offset) {\r
++      if (i < opt->offset) {\r
+           notmuch_thread_destroy (thread);\r
+           continue;\r
+       }\r
\r
+-      if (output == OUTPUT_THREADS) {\r
++      if (opt->output == OUTPUT_THREADS) {\r
+           format->set_prefix (format, "thread");\r
+           format->string (format,\r
+                           notmuch_thread_get_thread_id (thread));\r
+@@ -123,7 +129,7 @@ do_search_threads (sprinter_t *format,\r
\r
+           format->begin_map (format);\r
\r
+-          if (sort == NOTMUCH_SORT_OLDEST_FIRST)\r
++          if (opt->sort == NOTMUCH_SORT_OLDEST_FIRST)\r
+               date = notmuch_thread_get_oldest_date (thread);\r
+           else\r
+               date = notmuch_thread_get_newest_date (thread);\r
+@@ -215,40 +221,36 @@ do_search_threads (sprinter_t *format,\r
+ }\r
\r
+ static int\r
+-do_search_messages (sprinter_t *format,\r
+-                  notmuch_query_t *query,\r
+-                  output_t output,\r
+-                  int offset,\r
+-                  int limit,\r
+-                  int dupe)\r
++do_search_messages (search_options_t *opt)\r
+ {\r
+     notmuch_message_t *message;\r
+     notmuch_messages_t *messages;\r
+     notmuch_filenames_t *filenames;\r
++    sprinter_t *format = opt->format;\r
+     int i;\r
\r
+-    if (offset < 0) {\r
+-      offset += notmuch_query_count_messages (query);\r
+-      if (offset < 0)\r
+-          offset = 0;\r
++    if (opt->offset < 0) {\r
++      opt->offset += notmuch_query_count_messages (opt->query);\r
++      if (opt->offset < 0)\r
++          opt->offset = 0;\r
+     }\r
\r
+-    messages = notmuch_query_search_messages (query);\r
++    messages = notmuch_query_search_messages (opt->query);\r
+     if (messages == NULL)\r
+       return 1;\r
\r
+     format->begin_list (format);\r
\r
+     for (i = 0;\r
+-       notmuch_messages_valid (messages) && (limit < 0 || i < offset + limit);\r
++       notmuch_messages_valid (messages) && (opt->limit < 0 || i < opt->offset + opt->limit);\r
+        notmuch_messages_move_to_next (messages), i++)\r
+     {\r
+-      if (i < offset)\r
++      if (i < opt->offset)\r
+           continue;\r
\r
+       message = notmuch_messages_get (messages);\r
\r
+-      if (output == OUTPUT_FILES) {\r
++      if (opt->output == OUTPUT_FILES) {\r
+           int j;\r
+           filenames = notmuch_message_get_filenames (message);\r
\r
+@@ -256,7 +258,7 @@ do_search_messages (sprinter_t *format,\r
+                notmuch_filenames_valid (filenames);\r
+                notmuch_filenames_move_to_next (filenames), j++)\r
+           {\r
+-              if (dupe < 0 || dupe == j) {\r
++              if (opt->dupe < 0 || opt->dupe == j) {\r
+                   format->string (format, notmuch_filenames_get (filenames));\r
+                   format->separator (format);\r
+               }\r
+@@ -283,12 +285,13 @@ do_search_messages (sprinter_t *format,\r
\r
+ static int\r
+ do_search_tags (notmuch_database_t *notmuch,\r
+-              sprinter_t *format,\r
+-              notmuch_query_t *query)\r
++              const search_options_t *opt)\r
+ {\r
+     notmuch_messages_t *messages = NULL;\r
+     notmuch_tags_t *tags;\r
+     const char *tag;\r
++    sprinter_t *format = opt->format;\r
++    notmuch_query_t *query = opt->query;\r
\r
+     /* should the following only special case if no excluded terms\r
+      * specified? */\r
+@@ -333,16 +336,16 @@ int\r
+ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+ {\r
+     notmuch_database_t *notmuch;\r
+-    notmuch_query_t *query;\r
++    search_options_t opt = {\r
++      .sort = NOTMUCH_SORT_NEWEST_FIRST,\r
++      .output = OUTPUT_SUMMARY,\r
++      .offset = 0,\r
++      .limit = -1, /* unlimited */\r
++      .dupe = -1,\r
++    };\r
+     char *query_str;\r
+-    notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;\r
+-    sprinter_t *format = NULL;\r
+     int opt_index, ret;\r
+-    output_t output = OUTPUT_SUMMARY;\r
+-    int offset = 0;\r
+-    int limit = -1; /* unlimited */\r
+     notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;\r
+-    int dupe = -1;\r
+     unsigned int i;\r
\r
+     enum {\r
+@@ -353,7 +356,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+     } format_sel = NOTMUCH_FORMAT_TEXT;\r
\r
+     notmuch_opt_desc_t options[] = {\r
+-      { NOTMUCH_OPT_KEYWORD, &sort, "sort", 's',\r
++      { NOTMUCH_OPT_KEYWORD, &opt.sort, "sort", 's',\r
+         (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },\r
+                                 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },\r
+                                 { 0, 0 } } },\r
+@@ -364,7 +367,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+                                 { "text0", NOTMUCH_FORMAT_TEXT0 },\r
+                                 { 0, 0 } } },\r
+       { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 },\r
+-      { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',\r
++      { NOTMUCH_OPT_KEYWORD, &opt.output, "output", 'o',\r
+         (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },\r
+                                 { "threads", OUTPUT_THREADS },\r
+                                 { "messages", OUTPUT_MESSAGES },\r
+@@ -377,9 +380,9 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+                                   { "flag", NOTMUCH_EXCLUDE_FLAG },\r
+                                   { "all", NOTMUCH_EXCLUDE_ALL },\r
+                                   { 0, 0 } } },\r
+-      { NOTMUCH_OPT_INT, &offset, "offset", 'O', 0 },\r
+-      { NOTMUCH_OPT_INT, &limit, "limit", 'L', 0  },\r
+-      { NOTMUCH_OPT_INT, &dupe, "duplicate", 'D', 0  },\r
++      { NOTMUCH_OPT_INT, &opt.offset, "offset", 'O', 0 },\r
++      { NOTMUCH_OPT_INT, &opt.limit, "limit", 'L', 0  },\r
++      { NOTMUCH_OPT_INT, &opt.dupe, "duplicate", 'D', 0  },\r
+       { 0, 0, 0, 0, 0 }\r
+     };\r
\r
+@@ -389,20 +392,20 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
\r
+     switch (format_sel) {\r
+     case NOTMUCH_FORMAT_TEXT:\r
+-      format = sprinter_text_create (config, stdout);\r
++      opt.format = sprinter_text_create (config, stdout);\r
+       break;\r
+     case NOTMUCH_FORMAT_TEXT0:\r
+-      if (output == OUTPUT_SUMMARY) {\r
++      if (opt.output == OUTPUT_SUMMARY) {\r
+           fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");\r
+           return EXIT_FAILURE;\r
+       }\r
+-      format = sprinter_text0_create (config, stdout);\r
++      opt.format = sprinter_text0_create (config, stdout);\r
+       break;\r
+     case NOTMUCH_FORMAT_JSON:\r
+-      format = sprinter_json_create (config, stdout);\r
++      opt.format = sprinter_json_create (config, stdout);\r
+       break;\r
+     case NOTMUCH_FORMAT_SEXP:\r
+-      format = sprinter_sexp_create (config, stdout);\r
++      opt.format = sprinter_sexp_create (config, stdout);\r
+       break;\r
+     default:\r
+       /* this should never happen */\r
+@@ -425,15 +428,15 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+       return EXIT_FAILURE;\r
+     }\r
\r
+-    query = notmuch_query_create (notmuch, query_str);\r
+-    if (query == NULL) {\r
++    opt.query = notmuch_query_create (notmuch, query_str);\r
++    if (opt.query == NULL) {\r
+       fprintf (stderr, "Out of memory\n");\r
+       return EXIT_FAILURE;\r
+     }\r
\r
+-    notmuch_query_set_sort (query, sort);\r
++    notmuch_query_set_sort (opt.query, opt.sort);\r
\r
+-    if (exclude == NOTMUCH_EXCLUDE_FLAG && output != OUTPUT_SUMMARY) {\r
++    if (exclude == NOTMUCH_EXCLUDE_FLAG && opt.output != OUTPUT_SUMMARY) {\r
+       /* If we are not doing summary output there is nowhere to\r
+        * print the excluded flag so fall back on including the\r
+        * excluded messages. */\r
+@@ -448,29 +451,29 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+       search_exclude_tags = notmuch_config_get_search_exclude_tags\r
+           (config, &search_exclude_tags_length);\r
+       for (i = 0; i < search_exclude_tags_length; i++)\r
+-          notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);\r
+-      notmuch_query_set_omit_excluded (query, exclude);\r
++          notmuch_query_add_tag_exclude (opt.query, search_exclude_tags[i]);\r
++      notmuch_query_set_omit_excluded (opt.query, exclude);\r
+     }\r
\r
+-    switch (output) {\r
++    switch (opt.output) {\r
+     default:\r
+     case OUTPUT_SUMMARY:\r
+     case OUTPUT_THREADS:\r
+-      ret = do_search_threads (format, query, sort, output, offset, limit);\r
++      ret = do_search_threads (&opt);\r
+       break;\r
+     case OUTPUT_MESSAGES:\r
+     case OUTPUT_FILES:\r
+-      ret = do_search_messages (format, query, output, offset, limit, dupe);\r
++      ret = do_search_messages (&opt);\r
+       break;\r
+     case OUTPUT_TAGS:\r
+-      ret = do_search_tags (notmuch, format, query);\r
++      ret = do_search_tags (notmuch, &opt);\r
+       break;\r
+     }\r
\r
+-    notmuch_query_destroy (query);\r
++    notmuch_query_destroy (opt.query);\r
+     notmuch_database_destroy (notmuch);\r
\r
+-    talloc_free (format);\r
++    talloc_free (opt.format);\r
\r
+     return ret ? EXIT_FAILURE : EXIT_SUCCESS;\r
+ }\r
+-- \r
+2.1.1\r
+\r