Re: [PATCH 1/2] cli: convert "notmuch show" to use the new argument parser
authorMark Walters <markwalters1009@gmail.com>
Sat, 4 Feb 2012 00:00:00 +0000 (00:00 +0000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:44:02 +0000 (09:44 -0800)
55/84d37497daf6ec54eb9aff0c501a835865b545 [new file with mode: 0644]

diff --git a/55/84d37497daf6ec54eb9aff0c501a835865b545 b/55/84d37497daf6ec54eb9aff0c501a835865b545
new file mode 100644 (file)
index 0000000..4225533
--- /dev/null
@@ -0,0 +1,317 @@
+Return-Path: <m.walters@qmul.ac.uk>\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 C7C13431FAF\r
+       for <notmuch@notmuchmail.org>; Fri,  3 Feb 2012 15:58:58 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -1.098\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5\r
+       tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,\r
+       NML_ADSP_CUSTOM_MED=1.2, 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 rb0ZLaUef9Qu for <notmuch@notmuchmail.org>;\r
+       Fri,  3 Feb 2012 15:58:57 -0800 (PST)\r
+Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])\r
+       (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 8C6B9431FAE\r
+       for <notmuch@notmuchmail.org>; Fri,  3 Feb 2012 15:58:57 -0800 (PST)\r
+Received: from smtp.qmul.ac.uk ([138.37.6.40])\r
+       by mail2.qmul.ac.uk with esmtp (Exim 4.71)\r
+       (envelope-from <m.walters@qmul.ac.uk>)\r
+       id 1RtT1m-0000Mf-Jf; Fri, 03 Feb 2012 23:58:53 +0000\r
+Received: from 94-192-233-223.zone6.bethere.co.uk ([94.192.233.223]\r
+       helo=localhost)\r
+       by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.69)\r
+       (envelope-from <m.walters@qmul.ac.uk>)\r
+       id 1RtT1m-0006p7-3C; Fri, 03 Feb 2012 23:58:50 +0000\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: Jani Nikula <jani@nikula.org>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH 1/2] cli: convert "notmuch show" to use the new argument\r
+       parser\r
+In-Reply-To:\r
+ <df2e9bd7682e9c81e1b205cc1d92a1526cdd1a9b.1328308635.git.jani@nikula.org>\r
+References:\r
+ <df2e9bd7682e9c81e1b205cc1d92a1526cdd1a9b.1328308635.git.jani@nikula.org>\r
+User-Agent: Notmuch/0.11+129~gdd41085 (http://notmuchmail.org) Emacs/23.2.1\r
+       (i486-pc-linux-gnu)\r
+Date: Sat, 04 Feb 2012 00:00:00 +0000\r
+Message-ID: <87aa4zo4cf.fsf@qmul.ac.uk>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Sender-Host-Address: 94.192.233.223\r
+X-QM-SPAM-Info: Sender has good ham record.  :)\r
+X-QM-Body-MD5: 1ac4dde93c63304f337a3060c40b1ce6 (of first 20000 bytes)\r
+X-SpamAssassin-Score: -1.8\r
+X-SpamAssassin-SpamBar: -\r
+X-SpamAssassin-Report: The QM spam filters have analysed this message to\r
+       determine if it is\r
+       spam. We require at least 5.0 points to mark a message as spam.\r
+       This message scored -1.8 points.\r
+       Summary of the scoring: \r
+       * -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/,\r
+       *      medium trust\r
+       *      [138.37.6.40 listed in list.dnswl.org]\r
+       * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\r
+       provider *      (markwalters1009[at]gmail.com)\r
+       * -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay\r
+       *      domain\r
+       *  0.5 AWL AWL: From: address is in the auto white-list\r
+X-QM-Scan-Virus: ClamAV says the message is clean\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 23:58:58 -0000\r
+\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
+> -    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
+\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