Re: [PATCH 1/2] cli: convert "notmuch show" to use the new argument parser
authorJani Nikula <jani@nikula.org>
Sat, 4 Feb 2012 08:59:42 +0000 (10:59 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:44:04 +0000 (09:44 -0800)
ff/1a645d55227573220e825898495f03af69082c [new file with mode: 0644]

diff --git a/ff/1a645d55227573220e825898495f03af69082c b/ff/1a645d55227573220e825898495f03af69082c
new file mode 100644 (file)
index 0000000..6e8116b
--- /dev/null
@@ -0,0 +1,314 @@
+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 6EDA0431FAF\r
+       for <notmuch@notmuchmail.org>; Sat,  4 Feb 2012 00:59:51 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 X9bDvp9qyh64 for <notmuch@notmuchmail.org>;\r
+       Sat,  4 Feb 2012 00:59:50 -0800 (PST)\r
+Received: from mail-lpp01m010-f53.google.com (mail-lpp01m010-f53.google.com\r
+       [209.85.215.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id C472B431FAE\r
+       for <notmuch@notmuchmail.org>; Sat,  4 Feb 2012 00:59:49 -0800 (PST)\r
+Received: by lahd3 with SMTP id d3so2445024lah.26\r
+       for <notmuch@notmuchmail.org>; Sat, 04 Feb 2012 00:59:46 -0800 (PST)\r
+Received: by 10.152.147.38 with SMTP id th6mr5256509lab.47.1328345986655;\r
+       Sat, 04 Feb 2012 00:59:46 -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 ny9sm4469597lab.6.2012.02.04.00.59.43\r
+       (version=SSLv3 cipher=OTHER); Sat, 04 Feb 2012 00:59:45 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: Mark Walters <markwalters1009@gmail.com>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH 1/2] cli: convert "notmuch show" to use the new argument\r
+       parser\r
+In-Reply-To: <87aa4zo4cf.fsf@qmul.ac.uk>\r
+References:\r
+ <df2e9bd7682e9c81e1b205cc1d92a1526cdd1a9b.1328308635.git.jani@nikula.org>\r
+       <87aa4zo4cf.fsf@qmul.ac.uk>\r
+User-Agent: Notmuch/0.11+139~g4340989 (http://notmuchmail.org) Emacs/23.3.1\r
+       (i686-pc-linux-gnu)\r
+Date: Sat, 04 Feb 2012 10:59:42 +0200\r
+Message-ID: <87haz755z5.fsf@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\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: Sat, 04 Feb 2012 08:59:51 -0000\r
+\r
+On Sat, 04 Feb 2012 00:00:00 +0000, Mark Walters <markwalters1009@gmail.com> wrote:\r
+> \r
+> On Sat,  4 Feb 2012 00:41:08 +0200, Jani Nikula <jani@nikula.org> wrote:\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
+> This looks great. As far as I can see it is fine (I haven't run or even\r
+> compiled it yet). I only have two query/nits below.\r
+> \r
+> Best wishes\r
+> \r
+> Mark\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
+> \r
+> I think you don't need the default value here. If you think it is\r
+> clearer with the default then that is fine. I think I slightly prefer\r
+> without since in some cases the default is raw but entirely up to you.\r
+\r
+I actually dropped this at first, but the compiler has no way of knowing\r
+that all the cases are covered in the switch, and thinks format may be\r
+uninitialized. I was wondering whether to have a default case in the\r
+switch (which would be just to make the compiler happy), but settled on\r
+this instead.\r
+\r
+> \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
+> \r
+> Does this initialize all the other params bits to zero/NULL?  \r
+\r
+Yes. It's called a "designated initializer for aggregate type" if you\r
+want to look it up.\r
+\r
+Thanks for the review.\r
+\r
+\r
+BR,\r
+Jani.\r
+\r
+> \r
+> \r
+> \r
+> \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
+> > _______________________________________________\r
+> > notmuch mailing list\r
+> > notmuch@notmuchmail.org\r
+> > http://notmuchmail.org/mailman/listinfo/notmuch\r