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