Re: [PATCH v4 02/16] Move crypto.c into libutil
[notmuch-archives.git] / 20 / d0907a1ffde1ffee0e128eea265b99646a7d5f
1 Return-Path: <too@guru-group.fi>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id D4A89431FAE\r
6         for <notmuch@notmuchmail.org>; Tue, 28 May 2013 11:39:12 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id Lc4wyWs6cB3c for <notmuch@notmuchmail.org>;\r
16         Tue, 28 May 2013 11:39:08 -0700 (PDT)\r
17 Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])\r
18         by olra.theworths.org (Postfix) with ESMTP id 84F8D431FB6\r
19         for <notmuch@notmuchmail.org>; Tue, 28 May 2013 11:39:02 -0700 (PDT)\r
20 Received: by guru.guru-group.fi (Postfix, from userid 501)\r
21         id 7D47F1001ED; Tue, 28 May 2013 21:39:00 +0300 (EEST)\r
22 From: Tomi Ollila <tomi.ollila@iki.fi>\r
23 To: notmuch@notmuchmail.org\r
24 Subject: [PATCH v3 1/4] cli: add global option --stderr=FILE\r
25 Date: Tue, 28 May 2013 21:38:52 +0300\r
26 Message-Id: <1369766335-26733-2-git-send-email-tomi.ollila@iki.fi>\r
27 X-Mailer: git-send-email 1.8.0\r
28 In-Reply-To: <1369766335-26733-1-git-send-email-tomi.ollila@iki.fi>\r
29 References: <1369766335-26733-1-git-send-email-tomi.ollila@iki.fi>\r
30 Cc: tomi.ollila@iki.fi\r
31 X-BeenThere: notmuch@notmuchmail.org\r
32 X-Mailman-Version: 2.1.13\r
33 Precedence: list\r
34 List-Id: "Use and development of the notmuch mail system."\r
35         <notmuch.notmuchmail.org>\r
36 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
37         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
38 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
39 List-Post: <mailto:notmuch@notmuchmail.org>\r
40 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
41 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
42         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
43 X-List-Received-Date: Tue, 28 May 2013 18:39:13 -0000\r
44 \r
45 With this option all writes to stderr are redirected to the spesified\r
46 FILE (or to stdout on case FILE is '-'). This is immediately useful\r
47 in emacs interface as some of its exec intefaces do not provide\r
48 separation of stdout and stderr.\r
49 ---\r
50  notmuch-client.h |  1 +\r
51  notmuch.c        | 32 ++++++++++++++++++++++++++++++++\r
52  2 files changed, 33 insertions(+)\r
53 \r
54 diff --git a/notmuch-client.h b/notmuch-client.h\r
55 index 45749a6..4a3c7ac 100644\r
56 --- a/notmuch-client.h\r
57 +++ b/notmuch-client.h\r
58 @@ -54,6 +54,7 @@ typedef GMimeCipherContext notmuch_crypto_context_t;\r
59  #include <sys/stat.h>\r
60  #include <sys/time.h>\r
61  #include <unistd.h>\r
62 +#include <fcntl.h>\r
63  #include <dirent.h>\r
64  #include <errno.h>\r
65  #include <signal.h>\r
66 diff --git a/notmuch.c b/notmuch.c\r
67 index f51a84f..15e90c8 100644\r
68 --- a/notmuch.c\r
69 +++ b/notmuch.c\r
70 @@ -251,6 +251,32 @@ notmuch_command (notmuch_config_t *config,\r
71      return 0;\r
72  }\r
73  \r
74 +static int\r
75 +redirect_stderr (const char * stderr_file)\r
76 +{\r
77 +    if (strcmp (stderr_file, "-") == 0) {\r
78 +       if (dup2 (STDOUT_FILENO, STDERR_FILENO) < 0) {\r
79 +           perror ("dup2");\r
80 +           return 1;\r
81 +       }\r
82 +    } else {\r
83 +       int fd = open (stderr_file, O_WRONLY|O_CREAT|O_TRUNC, 0666);\r
84 +       if (fd < 0) {\r
85 +           fprintf (stderr, "Error: Cannot redirect stderr to '%s': %s\n",\r
86 +                    stderr_file, strerror (errno));\r
87 +           return 1;\r
88 +       }\r
89 +       if (fd != STDERR_FILENO) {\r
90 +           if (dup2 (fd, STDERR_FILENO) < 0) {\r
91 +               perror ("dup2");\r
92 +               return 1;\r
93 +           }\r
94 +           close (fd);\r
95 +       }\r
96 +    }\r
97 +    return 0;\r
98 +}\r
99 +\r
100  int\r
101  main (int argc, char *argv[])\r
102  {\r
103 @@ -259,6 +285,7 @@ main (int argc, char *argv[])\r
104      const char *command_name = NULL;\r
105      command_t *command;\r
106      char *config_file_name = NULL;\r
107 +    char *stderr_file = NULL;\r
108      notmuch_config_t *config;\r
109      notmuch_bool_t print_help=FALSE, print_version=FALSE;\r
110      int opt_index;\r
111 @@ -268,6 +295,7 @@ main (int argc, char *argv[])\r
112         { NOTMUCH_OPT_BOOLEAN, &print_help, "help", 'h', 0 },\r
113         { NOTMUCH_OPT_BOOLEAN, &print_version, "version", 'v', 0 },\r
114         { NOTMUCH_OPT_STRING, &config_file_name, "config", 'c', 0 },\r
115 +       { NOTMUCH_OPT_STRING, &stderr_file, "stderr", '\0', 0 },\r
116         { 0, 0, 0, 0, 0 }\r
117      };\r
118  \r
119 @@ -287,6 +315,10 @@ main (int argc, char *argv[])\r
120         return 1;\r
121      }\r
122  \r
123 +    if (stderr_file && redirect_stderr (stderr_file) != 0) {\r
124 +       /* error already printed */\r
125 +       return 1;\r
126 +    }\r
127      if (print_help)\r
128         return notmuch_help_command (NULL, argc - 1, &argv[1]);\r
129  \r
130 -- \r
131 1.8.1.4\r
132 \r