Re: [PATCH 1/2] Add Google Inc. to AUTHORS as a contributor.
[notmuch-archives.git] / e3 / eb2843a62a39272081c4803d522d0188161f4a
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 9D62B431FD4\r
6         for <notmuch@notmuchmail.org>; Sun,  6 Jan 2013 19:17:12 -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 akfxxruJcZ+F for <notmuch@notmuchmail.org>;\r
16         Sun,  6 Jan 2013 19:17:11 -0800 (PST)\r
17 Received: from tesseract.cs.unb.ca (tesseract.cs.unb.ca [131.202.240.238])\r
18         (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id 3C881431FC9\r
21         for <notmuch@notmuchmail.org>; Sun,  6 Jan 2013 19:17:11 -0800 (PST)\r
22 Received: from fctnnbsc30w-156034082078.dhcp-dynamic.fibreop.nb.bellaliant.net\r
23         ([156.34.82.78] helo=zancas.localnet)\r
24         by tesseract.cs.unb.ca with esmtpsa\r
25         (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.80)\r
26         (envelope-from <bremner@tethera.net>)\r
27         id 1Ts3D3-0002dy-Ai; Sun, 06 Jan 2013 23:17:09 -0400\r
28 Received: from bremner by zancas.localnet with local (Exim 4.80)\r
29         (envelope-from <bremner@tethera.net>)\r
30         id 1Ts3Cx-0001hw-Lr; Sun, 06 Jan 2013 23:17:03 -0400\r
31 From: david@tethera.net\r
32 To: notmuch@notmuchmail.org\r
33 Subject: [Patch v2 3/9] cli: add support for batch tagging operations to\r
34         "notmuch tag"\r
35 Date: Sun,  6 Jan 2013 23:16:48 -0400\r
36 Message-Id: <1357528614-6413-4-git-send-email-david@tethera.net>\r
37 X-Mailer: git-send-email 1.7.10.4\r
38 In-Reply-To: <1357528614-6413-1-git-send-email-david@tethera.net>\r
39 References: <1357528614-6413-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: Mon, 07 Jan 2013 03:17:12 -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> [...] [--] <query> [...]\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> MAY be hex encoded with %NN where NN is the hexadecimal value of\r
66 the character. Any ' ' and '%' characters in <tag> and MUST be hex\r
67 encoded (using %20 and %25, respectively). For future-proofing, any\r
68 '"' characters in <tag> SHOULD be hex-encoded.\r
69 \r
70 Any characters that are not part of <tag> or\r
71 MUST NOT be hex encoded.\r
72 \r
73 <query> is passed verbatim to Xapian\r
74 \r
75 Leading and trailing space ' ' is ignored. Empty lines and lines\r
76 beginning with '#' are ignored.\r
77 \r
78 Signed-off-by: Jani Nikula <jani@nikula.org>\r
79 \r
80 Hacked-like-crazy-by: David Bremner <david@tethera.net>\r
81 ---\r
82  notmuch-tag.c |   94 ++++++++++++++++++++++++++++++++++++++++++++++++++++-----\r
83  1 file changed, 86 insertions(+), 8 deletions(-)\r
84 \r
85 diff --git a/notmuch-tag.c b/notmuch-tag.c\r
86 index 8129912..7fc614d 100644\r
87 --- a/notmuch-tag.c\r
88 +++ b/notmuch-tag.c\r
89 @@ -128,6 +128,46 @@ tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,\r
90      return interrupted;\r
91  }\r
92  \r
93 +static int\r
94 +tag_file (void *ctx, notmuch_database_t *notmuch, tag_op_flag_t flags,\r
95 +         FILE *input)\r
96 +{\r
97 +    char *line = NULL;\r
98 +    char *query_string = NULL;\r
99 +    size_t line_size = 0;\r
100 +    ssize_t line_len;\r
101 +    int ret = 0;\r
102 +    tag_op_list_t *tag_ops;\r
103 +\r
104 +    tag_ops = tag_op_list_create (ctx);\r
105 +    if (tag_ops == NULL) {\r
106 +       fprintf (stderr, "Out of memory.\n");\r
107 +       return 1;\r
108 +    }\r
109 +\r
110 +    while ((line_len = getline (&line, &line_size, input)) != -1 &&\r
111 +          ! interrupted) {\r
112 +\r
113 +       ret = parse_tag_line (ctx, line, TAG_FLAG_NONE,\r
114 +                             &query_string, tag_ops);\r
115 +\r
116 +       if (ret > 0)\r
117 +           continue;\r
118 +\r
119 +       if (ret < 0)\r
120 +           break;\r
121 +\r
122 +       ret = tag_query (ctx, notmuch, query_string, tag_ops, flags);\r
123 +       if (ret)\r
124 +           break;\r
125 +    }\r
126 +\r
127 +    if (line)\r
128 +       free (line);\r
129 +\r
130 +    return ret;\r
131 +}\r
132 +\r
133  int\r
134  notmuch_tag_command (void *ctx, int argc, char *argv[])\r
135  {\r
136 @@ -137,6 +177,10 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
137      notmuch_database_t *notmuch;\r
138      struct sigaction action;\r
139      tag_op_flag_t tag_flags = TAG_FLAG_NONE;\r
140 +    notmuch_bool_t batch = FALSE;\r
141 +    FILE *input = stdin;\r
142 +    char *input_file_name = NULL;\r
143 +    int opt_index;\r
144      int ret = 0;\r
145  \r
146      /* Setup our handler for SIGINT */\r
147 @@ -146,15 +190,43 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
148      action.sa_flags = SA_RESTART;\r
149      sigaction (SIGINT, &action, NULL);\r
150  \r
151 -    tag_ops = tag_op_list_create (ctx);\r
152 -    if (tag_ops == NULL) {\r
153 -       fprintf (stderr, "Out of memory.\n");\r
154 +    notmuch_opt_desc_t options[] = {\r
155 +       { NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },\r
156 +       { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },\r
157 +       { 0, 0, 0, 0, 0 }\r
158 +    };\r
159 +\r
160 +    opt_index = parse_arguments (argc, argv, options, 1);\r
161 +    if (opt_index < 0)\r
162         return 1;\r
163 +\r
164 +    if (input_file_name) {\r
165 +       batch = TRUE;\r
166 +       input = fopen (input_file_name, "r");\r
167 +       if (input == NULL) {\r
168 +           fprintf (stderr, "Error opening %s for reading: %s\n",\r
169 +                    input_file_name, strerror (errno));\r
170 +           return 1;\r
171 +       }\r
172      }\r
173  \r
174 -    if (parse_tag_command_line (ctx, argc - 1, argv + 1,\r
175 -                               &query_string, tag_ops))\r
176 -       return 1;\r
177 +    if (batch) {\r
178 +       if (opt_index != argc) {\r
179 +           fprintf (stderr, "Can't specify both cmdline and stdin!\n");\r
180 +           return 1;\r
181 +       }\r
182 +    } else {\r
183 +\r
184 +       tag_ops = tag_op_list_create (ctx);\r
185 +       if (tag_ops == NULL) {\r
186 +           fprintf (stderr, "Out of memory.\n");\r
187 +           return 1;\r
188 +       }\r
189 +\r
190 +       if (parse_tag_command_line (ctx, argc - opt_index, argv + opt_index,\r
191 +                                   &query_string, tag_ops))\r
192 +           return 1;\r
193 +    }\r
194  \r
195      config = notmuch_config_open (ctx, NULL, NULL);\r
196      if (config == NULL)\r
197 @@ -167,9 +239,15 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
198      if (notmuch_config_get_maildir_synchronize_flags (config))\r
199         tag_flags |= TAG_FLAG_MAILDIR_SYNC;\r
200  \r
201 -    ret = tag_query (ctx, notmuch, query_string, tag_ops, tag_flags);\r
202 +    if (batch)\r
203 +       ret = tag_file (ctx, notmuch, tag_flags, input);\r
204 +    else\r
205 +       ret = tag_query (ctx, notmuch, query_string, tag_ops, tag_flags);\r
206  \r
207      notmuch_database_destroy (notmuch);\r
208  \r
209 -    return ret;\r
210 +    if (input != stdin)\r
211 +       fclose (input);\r
212 +\r
213 +    return ret || interrupted;\r
214  }\r
215 -- \r
216 1.7.10.4\r
217 \r