Re: [PATCH v4 01/16] add util/search-path.{c, h} to test for executables in $PATH
[notmuch-archives.git] / db / ab0474253f08543ba65069ad59e93c865bab05
1 Return-Path: <bremner@tethera.net>\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 26050431FC0\r
6         for <notmuch@notmuchmail.org>; Fri, 14 Dec 2012 05:35:01 -0800 (PST)\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 EvhjI5wYPFHD for <notmuch@notmuchmail.org>;\r
16         Fri, 14 Dec 2012 05:34:59 -0800 (PST)\r
17 Received: from tesseract.cs.unb.ca (tesseract.cs.unb.ca [131.202.240.238])\r
18         (using TLSv1 with cipher AES256-SHA (256/256 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id A6D59431FD9\r
21         for <notmuch@notmuchmail.org>; Fri, 14 Dec 2012 05:34:53 -0800 (PST)\r
22 Received: from fctnnbsc30w-142167090129.dhcp-dynamic.fibreop.nb.bellaliant.net\r
23         ([142.167.90.129] helo=zancas.localnet)\r
24         by tesseract.cs.unb.ca with esmtpsa\r
25         (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72)\r
26         (envelope-from <bremner@tethera.net>)\r
27         id 1TjVPg-0004ym-0m; Fri, 14 Dec 2012 09:34:52 -0400\r
28 Received: from bremner by zancas.localnet with local (Exim 4.80)\r
29         (envelope-from <bremner@tethera.net>)\r
30         id 1TjVPa-00022N-Gg; Fri, 14 Dec 2012 09:34:46 -0400\r
31 From: david@tethera.net\r
32 To: notmuch@notmuchmail.org\r
33 Subject: [Patch v7 07/14] cli: add support for batch tagging operations to\r
34         "notmuch tag"\r
35 Date: Fri, 14 Dec 2012 09:34:15 -0400\r
36 Message-Id: <1355492062-7546-8-git-send-email-david@tethera.net>\r
37 X-Mailer: git-send-email 1.7.10.4\r
38 In-Reply-To: <1355492062-7546-1-git-send-email-david@tethera.net>\r
39 References: <1355492062-7546-1-git-send-email-david@tethera.net>\r
40 X-Spam_bar: -\r
41 X-BeenThere: notmuch@notmuchmail.org\r
42 X-Mailman-Version: 2.1.13\r
43 Precedence: list\r
44 List-Id: "Use and development of the notmuch mail system."\r
45         <notmuch.notmuchmail.org>\r
46 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
47         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
48 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
49 List-Post: <mailto:notmuch@notmuchmail.org>\r
50 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
51 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
52         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
53 X-List-Received-Date: Fri, 14 Dec 2012 13:35:01 -0000\r
54 \r
55 From: Jani Nikula <jani@nikula.org>\r
56 \r
57 Add support for batch tagging operations through stdin to "notmuch\r
58 tag". This can be enabled with the new --batch command line option to\r
59 "notmuch tag". The input must consist of lines of the format:\r
60 \r
61 +<tag>|-<tag> [...] [--] <search-term> [...]\r
62 \r
63 Each line is interpreted similarly to "notmuch tag" command line\r
64 arguments. The delimiter is one or more spaces ' '. Any characters in\r
65 <tag> and <search-term> MAY be hex encoded with %NN where NN is the\r
66 hexadecimal value of the character. Any ' ' and '%' characters in\r
67 <tag> and <search-term> MUST be hex encoded (using %20 and %25,\r
68 respectively). Any characters that are not part of <tag> or\r
69 <search-term> MUST NOT be hex encoded.\r
70 \r
71 Leading and trailing space ' ' is ignored. Empty lines and lines\r
72 beginning with '#' are ignored.\r
73 \r
74 Signed-off-by: Jani Nikula <jani@nikula.org>\r
75 \r
76 Hacked-like-crazy-by: David Bremner <david@tethera.net>\r
77 ---\r
78  notmuch-tag.c |   88 +++++++++++++++++++++++++++++++++++++++++++++++++++------\r
79  1 file changed, 80 insertions(+), 8 deletions(-)\r
80 \r
81 diff --git a/notmuch-tag.c b/notmuch-tag.c\r
82 index 13f2268..a81d911 100644\r
83 --- a/notmuch-tag.c\r
84 +++ b/notmuch-tag.c\r
85 @@ -130,6 +130,43 @@ tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,\r
86      return interrupted;\r
87  }\r
88  \r
89 +static int\r
90 +tag_file (void *ctx, notmuch_database_t *notmuch, tag_op_flag_t flags,\r
91 +         FILE *input)\r
92 +{\r
93 +    char *line = NULL;\r
94 +    char *query_string = NULL;\r
95 +    size_t line_size = 0;\r
96 +    ssize_t line_len;\r
97 +    int ret = 0;\r
98 +    tag_op_list_t *tag_ops;\r
99 +\r
100 +    tag_ops = tag_op_list_create (ctx);\r
101 +    if (tag_ops == NULL) {\r
102 +       fprintf (stderr, "Out of memory.\n");\r
103 +       return 1;\r
104 +    }\r
105 +\r
106 +    while ((line_len = getline (&line, &line_size, input)) != -1 &&\r
107 +          ! interrupted) {\r
108 +\r
109 +       ret = parse_tag_line (ctx, line, TAG_FLAG_NONE,\r
110 +                             &query_string, tag_ops);\r
111 +\r
112 +       if (ret > 0)\r
113 +           continue;\r
114 +\r
115 +       if (ret < 0 || tag_query (ctx, notmuch, query_string,\r
116 +                                 tag_ops, flags))\r
117 +           break;\r
118 +    }\r
119 +\r
120 +    if (line)\r
121 +       free (line);\r
122 +\r
123 +    return ret;\r
124 +}\r
125 +\r
126  int\r
127  notmuch_tag_command (void *ctx, int argc, char *argv[])\r
128  {\r
129 @@ -139,6 +176,10 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
130      notmuch_database_t *notmuch;\r
131      struct sigaction action;\r
132      tag_op_flag_t tag_flags = TAG_FLAG_NONE;\r
133 +    notmuch_bool_t batch = FALSE;\r
134 +    FILE *input = stdin;\r
135 +    char *input_file_name = NULL;\r
136 +    int opt_index;\r
137      int ret = 0;\r
138  \r
139      /* Setup our handler for SIGINT */\r
140 @@ -148,15 +189,43 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
141      action.sa_flags = SA_RESTART;\r
142      sigaction (SIGINT, &action, NULL);\r
143  \r
144 -    tag_ops = tag_op_list_create (ctx);\r
145 -    if (tag_ops == NULL) {\r
146 -       fprintf (stderr, "Out of memory.\n");\r
147 +    notmuch_opt_desc_t options[] = {\r
148 +       { NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },\r
149 +       { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },\r
150 +       { 0, 0, 0, 0, 0 }\r
151 +    };\r
152 +\r
153 +    opt_index = parse_arguments (argc, argv, options, 1);\r
154 +    if (opt_index < 0)\r
155         return 1;\r
156 +\r
157 +    if (input_file_name) {\r
158 +       batch = TRUE;\r
159 +       input = fopen (input_file_name, "r");\r
160 +       if (input == NULL) {\r
161 +           fprintf (stderr, "Error opening %s for reading: %s\n",\r
162 +                    input_file_name, strerror (errno));\r
163 +           return 1;\r
164 +       }\r
165      }\r
166  \r
167 -    if (parse_tag_command_line (ctx, argc - 1, argv + 1,\r
168 -                               &query_string, tag_ops))\r
169 -       return 1;\r
170 +    if (batch) {\r
171 +       if (opt_index != argc) {\r
172 +           fprintf (stderr, "Can't specify both cmdline and stdin!\n");\r
173 +           return 1;\r
174 +       }\r
175 +    } else {\r
176 +\r
177 +       tag_ops = tag_op_list_create (ctx);\r
178 +       if (tag_ops == NULL) {\r
179 +           fprintf (stderr, "Out of memory.\n");\r
180 +           return 1;\r
181 +       }\r
182 +\r
183 +       if (parse_tag_command_line (ctx, argc - opt_index, argv + opt_index,\r
184 +                                   &query_string, tag_ops))\r
185 +           return 1;\r
186 +    }\r
187  \r
188      config = notmuch_config_open (ctx, NULL, NULL);\r
189      if (config == NULL)\r
190 @@ -169,9 +238,12 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
191      if (notmuch_config_get_maildir_synchronize_flags (config))\r
192         tag_flags |= TAG_FLAG_MAILDIR_SYNC;\r
193  \r
194 -    ret = tag_query (ctx, notmuch, query_string, tag_ops, tag_flags);\r
195 +    if (batch)\r
196 +       ret = tag_file (ctx, notmuch, tag_flags, input);\r
197 +    else\r
198 +       ret = tag_query (ctx, notmuch, query_string, tag_ops, tag_flags);\r
199  \r
200      notmuch_database_destroy (notmuch);\r
201  \r
202 -    return ret;\r
203 +    return ret || interrupted;\r
204  }\r
205 -- \r
206 1.7.10.4\r
207 \r