--- /dev/null
+Return-Path: <too@guru-group.fi>\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 3E755431FCB\r
+ for <notmuch@notmuchmail.org>; Sun, 26 May 2013 01:46:05 -0700 (PDT)\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 L9BOf9ULE66m for <notmuch@notmuchmail.org>;\r
+ Sun, 26 May 2013 01:46:00 -0700 (PDT)\r
+Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])\r
+ by olra.theworths.org (Postfix) with ESMTP id 601FF431FAE\r
+ for <notmuch@notmuchmail.org>; Sun, 26 May 2013 01:46:00 -0700 (PDT)\r
+Received: by guru.guru-group.fi (Postfix, from userid 501)\r
+ id 4B0131001ED; Sun, 26 May 2013 11:45:58 +0300 (EEST)\r
+From: Tomi Ollila <tomi.ollila@iki.fi>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 1/4] cli: add global option --stderr=FILE\r
+Date: Sun, 26 May 2013 11:45:51 +0300\r
+Message-Id: <1369557954-13439-2-git-send-email-tomi.ollila@iki.fi>\r
+X-Mailer: git-send-email 1.8.0\r
+In-Reply-To: <1369557954-13439-1-git-send-email-tomi.ollila@iki.fi>\r
+References: <1369557954-13439-1-git-send-email-tomi.ollila@iki.fi>\r
+Cc: tomi.ollila@iki.fi\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: Sun, 26 May 2013 08:46:05 -0000\r
+\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
++ 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
+-- \r
+1.8.0\r
+\r