[PATCH 1/2] cli: convert "notmuch show" to use the new argument parser
authorJani Nikula <jani@nikula.org>
Fri, 3 Feb 2012 22:41:08 +0000 (00:41 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:44:02 +0000 (09:44 -0800)
ee/3ce507890558e87fa97cb8ae39b985a63d794a [new file with mode: 0644]

diff --git a/ee/3ce507890558e87fa97cb8ae39b985a63d794a b/ee/3ce507890558e87fa97cb8ae39b985a63d794a
new file mode 100644 (file)
index 0000000..1a9455c
--- /dev/null
@@ -0,0 +1,266 @@
+Return-Path: <jani@nikula.org>\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 56E52431FB6\r
+       for <notmuch@notmuchmail.org>; Fri,  3 Feb 2012 14:41:19 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+       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 T6xVHja0012b for <notmuch@notmuchmail.org>;\r
+       Fri,  3 Feb 2012 14:41:18 -0800 (PST)\r
+Received: from mail-lpp01m020-f181.google.com (mail-lpp01m020-f181.google.com\r
+       [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 2FEFB431FAE\r
+       for <notmuch@notmuchmail.org>; Fri,  3 Feb 2012 14:41:18 -0800 (PST)\r
+Received: by lbbgn5 with SMTP id gn5so940158lbb.26\r
+       for <notmuch@notmuchmail.org>; Fri, 03 Feb 2012 14:41:15 -0800 (PST)\r
+Received: by 10.112.102.37 with SMTP id fl5mr2369846lbb.95.1328308875156;\r
+       Fri, 03 Feb 2012 14:41:15 -0800 (PST)\r
+Received: from localhost (dsl-hkibrasgw4-fe50f800-253.dhcp.inet.fi.\r
+       [84.248.80.253])\r
+       by mx.google.com with ESMTPS id hl9sm5901620lab.5.2012.02.03.14.41.12\r
+       (version=SSLv3 cipher=OTHER); Fri, 03 Feb 2012 14:41:13 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 1/2] cli: convert "notmuch show" to use the new argument\r
+ parser\r
+Date: Sat,  4 Feb 2012 00:41:08 +0200\r
+Message-Id:\r
+ <df2e9bd7682e9c81e1b205cc1d92a1526cdd1a9b.1328308635.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.5.4\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, 03 Feb 2012 22:41:19 -0000\r
+\r
+Use the new notmuch argument parser to handle arguments in "notmuch\r
+show". There are two corner case functional changes:\r
+\r
+1) Also set params.raw = 1 when defaulting to raw format when part is\r
+   requested but format is not specified.\r
+\r
+2) Do not set params.decrypt if crypto context creation fails.\r
+\r
+Signed-off-by: Jani Nikula <jani@nikula.org>\r
+---\r
+ notmuch-show.c |  153 +++++++++++++++++++++++++++++---------------------------\r
+ 1 files changed, 79 insertions(+), 74 deletions(-)\r
+\r
+diff --git a/notmuch-show.c b/notmuch-show.c\r
+index dec799c..f93e121 100644\r
+--- a/notmuch-show.c\r
++++ b/notmuch-show.c\r
+@@ -1049,6 +1049,14 @@ do_show (void *ctx,\r
+     return 0;\r
+ }\r
\r
++enum {\r
++    NOTMUCH_FORMAT_NOT_SPECIFIED,\r
++    NOTMUCH_FORMAT_JSON,\r
++    NOTMUCH_FORMAT_TEXT,\r
++    NOTMUCH_FORMAT_MBOX,\r
++    NOTMUCH_FORMAT_RAW\r
++};\r
++\r
+ int\r
+ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
+ {\r
+@@ -1056,92 +1064,98 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
+     notmuch_database_t *notmuch;\r
+     notmuch_query_t *query;\r
+     char *query_string;\r
+-    char *opt;\r
++    int opt_index;\r
+     const notmuch_show_format_t *format = &format_text;\r
+-    notmuch_show_params_t params;\r
+-    int mbox = 0;\r
+-    int format_specified = 0;\r
+-    int i;\r
++    notmuch_show_params_t params = { .part = -1 };\r
++    int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED;\r
++    notmuch_bool_t decrypt = FALSE;\r
++    notmuch_bool_t verify = FALSE;\r
++    notmuch_bool_t entire_thread = FALSE;\r
++\r
++    notmuch_opt_desc_t options[] = {\r
++      { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',\r
++        (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },\r
++                                { "text", NOTMUCH_FORMAT_TEXT },\r
++                                { "mbox", NOTMUCH_FORMAT_MBOX },\r
++                                { "raw", NOTMUCH_FORMAT_RAW },\r
++                                { 0, 0 } } },\r
++      { NOTMUCH_OPT_INT, &params.part, "part", 'p', 0 },\r
++      { NOTMUCH_OPT_BOOLEAN, &entire_thread, "entire-thread", 't', 0 },\r
++      { NOTMUCH_OPT_BOOLEAN, &decrypt, "decrypt", 'd', 0 },\r
++      { NOTMUCH_OPT_BOOLEAN, &verify, "verify", 'v', 0 },\r
++      { 0, 0, 0, 0, 0 }\r
++    };\r
++\r
++    opt_index = parse_arguments (argc, argv, options, 1);\r
++    if (opt_index < 0) {\r
++      /* diagnostics already printed */\r
++      return 1;\r
++    }\r
\r
+-    params.entire_thread = 0;\r
+-    params.raw = 0;\r
+-    params.part = -1;\r
+-    params.cryptoctx = NULL;\r
+-    params.decrypt = 0;\r
++    params.entire_thread = entire_thread;\r
\r
+-    argc--; argv++; /* skip subcommand argument */\r
++    if (format_sel == NOTMUCH_FORMAT_NOT_SPECIFIED) {\r
++      /* if part was requested and format was not specified, use format=raw */\r
++      if (params.part >= 0)\r
++          format_sel = NOTMUCH_FORMAT_RAW;\r
++      else\r
++          format_sel = NOTMUCH_FORMAT_TEXT;\r
++    }\r
\r
+-    for (i = 0; i < argc && argv[i][0] == '-'; i++) {\r
+-      if (strcmp (argv[i], "--") == 0) {\r
+-          i++;\r
+-          break;\r
++    switch (format_sel) {\r
++    case NOTMUCH_FORMAT_JSON:\r
++      format = &format_json;\r
++      params.entire_thread = 1;\r
++      break;\r
++    case NOTMUCH_FORMAT_TEXT:\r
++      format = &format_text;\r
++      break;\r
++    case NOTMUCH_FORMAT_MBOX:\r
++      if (params.part > 0) {\r
++          fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");\r
++          return 1;\r
+       }\r
+-      if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {\r
+-          opt = argv[i] + sizeof ("--format=") - 1;\r
+-          if (strcmp (opt, "text") == 0) {\r
+-              format = &format_text;\r
+-          } else if (strcmp (opt, "json") == 0) {\r
+-              format = &format_json;\r
+-              params.entire_thread = 1;\r
+-          } else if (strcmp (opt, "mbox") == 0) {\r
+-              format = &format_mbox;\r
+-              mbox = 1;\r
+-          } else if (strcmp (opt, "raw") == 0) {\r
+-              format = &format_raw;\r
+-              params.raw = 1;\r
+-          } else {\r
+-              fprintf (stderr, "Invalid value for --format: %s\n", opt);\r
+-              return 1;\r
+-          }\r
+-          format_specified = 1;\r
+-      } else if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {\r
+-          params.part = atoi(argv[i] + sizeof ("--part=") - 1);\r
+-      } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {\r
+-          params.entire_thread = 1;\r
+-      } else if ((STRNCMP_LITERAL (argv[i], "--verify") == 0) ||\r
+-                 (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)) {\r
+-          if (params.cryptoctx == NULL) {\r
++      format = &format_mbox;\r
++      break;\r
++    case NOTMUCH_FORMAT_RAW:\r
++      format = &format_raw;\r
++      /* If --format=raw specified without specifying part, we can only\r
++       * output single message, so set part=0 */\r
++      if (params.part < 0)\r
++          params.part = 0;\r
++      params.raw = 1;\r
++      break;\r
++    }\r
++\r
++    if (decrypt || verify) {\r
+ #ifdef GMIME_ATLEAST_26\r
+-              /* TODO: GMimePasswordRequestFunc */\r
+-              if (NULL == (params.cryptoctx = g_mime_gpg_context_new(NULL, "gpg")))\r
++      /* TODO: GMimePasswordRequestFunc */\r
++      params.cryptoctx = g_mime_gpg_context_new (NULL, "gpg");\r
+ #else\r
+-              GMimeSession* session = g_object_new(g_mime_session_get_type(), NULL);\r
+-              if (NULL == (params.cryptoctx = g_mime_gpg_context_new(session, "gpg")))\r
+-#endif\r
+-                  fprintf (stderr, "Failed to construct gpg context.\n");\r
+-              else\r
+-                  g_mime_gpg_context_set_always_trust((GMimeGpgContext*)params.cryptoctx, FALSE);\r
+-#ifndef GMIME_ATLEAST_26\r
+-              g_object_unref (session);\r
+-              session = NULL;\r
++      GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);\r
++      params.cryptoctx = g_mime_gpg_context_new (session, "gpg");\r
+ #endif\r
+-          }\r
+-          if (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)\r
+-              params.decrypt = 1;\r
++      if (params.cryptoctx) {\r
++          g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.cryptoctx, FALSE);\r
++          params.decrypt = decrypt;\r
+       } else {\r
+-          fprintf (stderr, "Unrecognized option: %s\n", argv[i]);\r
+-          return 1;\r
++          fprintf (stderr, "Failed to construct gpg context.\n");\r
+       }\r
++#ifndef GMIME_ATLEAST_26\r
++      g_object_unref (session);\r
++#endif\r
+     }\r
\r
+-    argc -= i;\r
+-    argv += i;\r
+-\r
+     config = notmuch_config_open (ctx, NULL, NULL);\r
+     if (config == NULL)\r
+       return 1;\r
\r
+-    query_string = query_string_from_args (ctx, argc, argv);\r
++    query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);\r
+     if (query_string == NULL) {\r
+       fprintf (stderr, "Out of memory\n");\r
+       return 1;\r
+     }\r
\r
+-    if (mbox && params.part > 0) {\r
+-      fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");\r
+-      return 1;\r
+-    }\r
+-\r
+     if (*query_string == '\0') {\r
+       fprintf (stderr, "Error: notmuch show requires at least one search term.\n");\r
+       return 1;\r
+@@ -1158,15 +1172,6 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
+       return 1;\r
+     }\r
\r
+-    /* if part was requested and format was not specified, use format=raw */\r
+-    if (params.part >= 0 && !format_specified)\r
+-      format = &format_raw;\r
+-\r
+-    /* If --format=raw specified without specifying part, we can only\r
+-     * output single message, so set part=0 */\r
+-    if (params.raw && params.part < 0)\r
+-      params.part = 0;\r
+-\r
+     if (params.part >= 0)\r
+       return do_show_single (ctx, query, format, &params);\r
+     else\r
+-- \r
+1.7.5.4\r
+\r