Re: [PATCH v2 1/4] cli: add global option --stderr=FILE
authorAustin Clements <amdragon@MIT.EDU>
Mon, 27 May 2013 21:44:36 +0000 (17:44 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:55:06 +0000 (09:55 -0800)
f9/d0cb9b108d79a80b3c418adca247ae7e6f4367 [new file with mode: 0644]

diff --git a/f9/d0cb9b108d79a80b3c418adca247ae7e6f4367 b/f9/d0cb9b108d79a80b3c418adca247ae7e6f4367
new file mode 100644 (file)
index 0000000..c6a5e05
--- /dev/null
@@ -0,0 +1,169 @@
+Return-Path: <amdragon@mit.edu>\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 1D002431FBF\r
+       for <notmuch@notmuchmail.org>; Mon, 27 May 2013 14:44:49 -0700 (PDT)\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 iJc467Op9lHs for <notmuch@notmuchmail.org>;\r
+       Mon, 27 May 2013 14:44:41 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-3.mit.edu (DMZ-MAILSEC-SCANNER-3.MIT.EDU\r
+       [18.9.25.14])\r
+       by olra.theworths.org (Postfix) with ESMTP id 5CE59431FAF\r
+       for <notmuch@notmuchmail.org>; Mon, 27 May 2013 14:44:41 -0700 (PDT)\r
+X-AuditID: 1209190e-b7f4f6d000005142-92-51a3d3c88eec\r
+Received: from mailhub-auth-2.mit.edu ( [18.7.62.36])\r
+       by dmz-mailsec-scanner-3.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id 56.8F.20802.8C3D3A15; Mon, 27 May 2013 17:44:40 -0400 (EDT)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH-1.MIT.EDU [18.9.28.11])\r
+       by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id r4RLidvF006404; \r
+       Mon, 27 May 2013 17:44:40 -0400\r
+Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91])\r
+       (authenticated bits=0)\r
+       (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+       by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id r4RLibL7009904\r
+       (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NOT);\r
+       Mon, 27 May 2013 17:44:38 -0400\r
+Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.80)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1Uh5DY-0000d7-Ox; Mon, 27 May 2013 17:44:36 -0400\r
+Date: Mon, 27 May 2013 17:44:36 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: Tomi Ollila <tomi.ollila@iki.fi>\r
+Subject: Re: [PATCH v2 1/4] cli: add global option --stderr=FILE\r
+Message-ID: <20130527214436.GT5999@mit.edu>\r
+References: <1369557954-13439-1-git-send-email-tomi.ollila@iki.fi>\r
+       <1369557954-13439-2-git-send-email-tomi.ollila@iki.fi>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+Content-Disposition: inline\r
+In-Reply-To: <1369557954-13439-2-git-send-email-tomi.ollila@iki.fi>\r
+User-Agent: Mutt/1.5.21 (2010-09-15)\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFmpileLIzCtJLcpLzFFi42IRYrdT0T1xeXGgwZsrphbXb85ktnizch6r\r
+       A5PH4a8LWTyerbrFHMAUxWWTkpqTWZZapG+XwJXRcv05W8F1sYodrx8xNjD2C3YxcnJICJhI\r
+       dD9axQ5hi0lcuLeeDcQWEtjHKHG1zaWLkQvI3sgosWL/OqjEaSaJndcFIBJLgBKHzjOBJFgE\r
+       VCV23+hhBbHZBDQktu1fzghiiwioSDxoWw8WZxaQlvj2uxmsXljAQeJVzxNmEJtXQFvi2rmP\r
+       LBALqiXOtD+FigtKnJz5hAWiV0vixr+XQL0cYHOW/+MACXMKOEvs6JoA9oAo0KopJ7exTWAU\r
+       moWkexaS7lkI3QsYmVcxyqbkVunmJmbmFKcm6xYnJ+blpRbpGuvlZpbopaaUbmIEBTWnJN8O\r
+       xq8HlQ4xCnAwKvHwTsxeHCjEmlhWXJl7iFGSg0lJlHfWJaAQX1J+SmVGYnFGfFFpTmrxIUYJ\r
+       DmYlEd7tK4ByvCmJlVWpRfkwKWkOFiVx3ispN/2FBNITS1KzU1MLUotgsjIcHEoSvK9BhgoW\r
+       paanVqRl5pQgpJk4OEGG84AMB6nhLS5IzC3OTIfIn2JUlBLnvQeSEABJZJTmwfXCks4rRnGg\r
+       V4R5r4NU8QATFlz3K6DBTECDxZnBBpckIqSkGhjjVm28tXKzzISZD/IUfrcnPmWuVJ5o7O7n\r
+       LHiurWwZR5HQr2N9730/CZ/a+PDF8diJOiutb7mf1Ztnp5cTFmJV/9p14RsPEf0K1v3x4bsb\r
+       N/8Pf65tzpr8xiD5fdd8pv9TT5/eVD3P7NXk7b9M7E26VKyZVNPn/L/EuTxklnPUvsSVPrYi\r
+       DG+UWIozEg21mIuKEwGHteB2FQMAAA==\r
+Cc: notmuch@notmuchmail.org\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: Mon, 27 May 2013 21:44:49 -0000\r
+\r
+Quoth Tomi Ollila on May 26 at 11:45 am:\r
+> With this option all writes to stderr are redirected to the spesified\r
+> FILE (or to stdout on case FILE is '-'). This is immediately useful\r
+> in emacs interface as some of its exec intefaces do not provide\r
+> separation of stdout and stderr.\r
+> ---\r
+>  notmuch-client.h |  1 +\r
+>  notmuch.c        | 32 ++++++++++++++++++++++++++++++++\r
+>  2 files changed, 33 insertions(+)\r
+> \r
+> diff --git a/notmuch-client.h b/notmuch-client.h\r
+> index 45749a6..4a3c7ac 100644\r
+> --- a/notmuch-client.h\r
+> +++ b/notmuch-client.h\r
+> @@ -54,6 +54,7 @@ typedef GMimeCipherContext notmuch_crypto_context_t;\r
+>  #include <sys/stat.h>\r
+>  #include <sys/time.h>\r
+>  #include <unistd.h>\r
+> +#include <fcntl.h>\r
+>  #include <dirent.h>\r
+>  #include <errno.h>\r
+>  #include <signal.h>\r
+> diff --git a/notmuch.c b/notmuch.c\r
+> index f51a84f..654a568 100644\r
+> --- a/notmuch.c\r
+> +++ b/notmuch.c\r
+> @@ -251,6 +251,32 @@ notmuch_command (notmuch_config_t *config,\r
+>      return 0;\r
+>  }\r
+>  \r
+> +static int\r
+> +redirect_stderr (const char * stderr_file)\r
+> +{\r
+> +    if (strcmp (stderr_file, "-") == 0) {\r
+> +    if (dup2 (STDOUT_FILENO, STDERR_FILENO) < 0) {\r
+> +        perror ("dup2");\r
+> +        return 1;\r
+> +    }\r
+> +    } else {\r
+> +    int fd = open (stderr_file, O_WRONLY|O_CREAT|O_APPEND, 0644);\r
+\r
+I think this should include O_TRUNC; otherwise it's too error-prone to\r
+use programmatically.  The permissions should be 0666 (if the user's\r
+umask says things should be group or world writable, it's not our job\r
+to disagree).\r
+\r
+> +    if (fd < 0) {\r
+> +        fprintf (stderr, "Error: Cannot redirect stderr to '%s': %s\n",\r
+> +                 stderr_file, strerror (errno));\r
+> +        return 1;\r
+> +    }\r
+> +    if (fd != STDERR_FILENO) {\r
+> +        if (dup2 (fd, STDERR_FILENO) < 0) {\r
+> +            perror ("dup2");\r
+> +            return 1;\r
+> +        }\r
+> +        close (fd);\r
+> +    }\r
+> +    }\r
+> +    return 0;\r
+> +}\r
+> +\r
+>  int\r
+>  main (int argc, char *argv[])\r
+>  {\r
+> @@ -259,6 +285,7 @@ main (int argc, char *argv[])\r
+>      const char *command_name = NULL;\r
+>      command_t *command;\r
+>      char *config_file_name = NULL;\r
+> +    char *stderr_file = NULL;\r
+>      notmuch_config_t *config;\r
+>      notmuch_bool_t print_help=FALSE, print_version=FALSE;\r
+>      int opt_index;\r
+> @@ -268,6 +295,7 @@ main (int argc, char *argv[])\r
+>      { NOTMUCH_OPT_BOOLEAN, &print_help, "help", 'h', 0 },\r
+>      { NOTMUCH_OPT_BOOLEAN, &print_version, "version", 'v', 0 },\r
+>      { NOTMUCH_OPT_STRING, &config_file_name, "config", 'c', 0 },\r
+> +    { NOTMUCH_OPT_STRING, &stderr_file, "stderr", '\0', 0 },\r
+>      { 0, 0, 0, 0, 0 }\r
+>      };\r
+>  \r
+> @@ -287,6 +315,10 @@ main (int argc, char *argv[])\r
+>      return 1;\r
+>      }\r
+>  \r
+> +    if (stderr_file && redirect_stderr (stderr_file) != 0) {\r
+> +    /* error already printed */\r
+> +    return 1;\r
+> +    }\r
+>      if (print_help)\r
+>      return notmuch_help_command (NULL, argc - 1, &argv[1]);\r
+>  \r