Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / a2 / 941bcebbf5c45f278bfa01398f016734e99904
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 16544431FC2\r
6         for <notmuch@notmuchmail.org>; Sat,  8 Dec 2012 14:57:55 -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 yIU39Bk32DF9 for <notmuch@notmuchmail.org>;\r
16         Sat,  8 Dec 2012 14:57:52 -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 1AA32429E41\r
21         for <notmuch@notmuchmail.org>; Sat,  8 Dec 2012 14:57:43 -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 1ThTL3-00066h-NS; Sat, 08 Dec 2012 18:57:42 -0400\r
28 Received: from bremner by zancas.localnet with local (Exim 4.80)\r
29         (envelope-from <bremner@tethera.net>)\r
30         id 1ThTKy-0000qh-8K; Sat, 08 Dec 2012 18:57:36 -0400\r
31 From: david@tethera.net\r
32 To: notmuch@notmuchmail.org\r
33 Subject: [Patch v5 05/11] notmuch-restore: add support for input format\r
34         'batch-tag'\r
35 Date: Sat,  8 Dec 2012 18:56:55 -0400\r
36 Message-Id: <1355007421-3069-6-git-send-email-david@tethera.net>\r
37 X-Mailer: git-send-email 1.7.10.4\r
38 In-Reply-To: <1355007421-3069-1-git-send-email-david@tethera.net>\r
39 References: <1355007421-3069-1-git-send-email-david@tethera.net>\r
40 X-Spam_bar: -\r
41 Cc: David Bremner <bremner@debian.org>\r
42 X-BeenThere: notmuch@notmuchmail.org\r
43 X-Mailman-Version: 2.1.13\r
44 Precedence: list\r
45 List-Id: "Use and development of the notmuch mail system."\r
46         <notmuch.notmuchmail.org>\r
47 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
48         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
49 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
50 List-Post: <mailto:notmuch@notmuchmail.org>\r
51 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
52 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
53         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
54 X-List-Received-Date: Sat, 08 Dec 2012 22:57:55 -0000\r
55 \r
56 From: David Bremner <bremner@debian.org>\r
57 \r
58 This can be enabled with the new --format=batch-tag command line\r
59 option to "notmuch restore". The input must consist of lines of the\r
60 format:\r
61 \r
62     +<tag>|-<tag> [...] [--] id:<msg-id>\r
63 \r
64 Each line is interpreted similarly to "notmuch tag" command line\r
65 arguments. The delimiter is one or more spaces ' '. Any characters in\r
66 <tag> and <search-terms> MAY be hex encoded with %NN where NN is the\r
67 hexadecimal value of the character. Any ' ' and '%' characters in\r
68 <tag> and <msg-id> MUST be hex encoded (using %20 and %25,\r
69 respectively). Any characters that are not part of <tag> or\r
70 <search-terms> MUST NOT be hex encoded.\r
71 \r
72 Leading and trailing space ' ' is ignored. Empty lines and lines\r
73 beginning with '#' are ignored.\r
74 \r
75 Commit message mainly stolen from Jani's batch tagging commit, to\r
76 follow.\r
77 ---\r
78  notmuch-restore.c |  220 +++++++++++++++++++++++++++++++++--------------------\r
79  1 file changed, 138 insertions(+), 82 deletions(-)\r
80 \r
81 diff --git a/notmuch-restore.c b/notmuch-restore.c\r
82 index f03dcac..44bf88d 100644\r
83 --- a/notmuch-restore.c\r
84 +++ b/notmuch-restore.c\r
85 @@ -19,18 +19,22 @@\r
86   */\r
87  \r
88  #include "notmuch-client.h"\r
89 +#include "dump-restore-private.h"\r
90 +#include "tag-util.h"\r
91 +#include "string-util.h"\r
92 +\r
93 +static volatile sig_atomic_t interrupted;\r
94 +static regex_t regex;\r
95  \r
96  static int\r
97 -tag_message (notmuch_database_t *notmuch, const char *message_id,\r
98 -            char *file_tags, notmuch_bool_t remove_all,\r
99 -            notmuch_bool_t synchronize_flags)\r
100 +tag_message (unused (void *ctx),\r
101 +            notmuch_database_t *notmuch,\r
102 +            const char *message_id,\r
103 +            tag_op_list_t *tag_ops,\r
104 +            tag_op_flag_t flags)\r
105  {\r
106      notmuch_status_t status;\r
107 -    notmuch_tags_t *db_tags;\r
108 -    char *db_tags_str;\r
109      notmuch_message_t *message = NULL;\r
110 -    const char *tag;\r
111 -    char *next;\r
112      int ret = 0;\r
113  \r
114      status = notmuch_database_find_message (notmuch, message_id, &message);\r
115 @@ -44,55 +48,67 @@ tag_message (notmuch_database_t *notmuch, const char *message_id,\r
116  \r
117      /* In order to detect missing messages, this check/optimization is\r
118       * intentionally done *after* first finding the message. */\r
119 -    if (! remove_all && (file_tags == NULL || *file_tags == '\0'))\r
120 -       goto DONE;\r
121 -\r
122 -    db_tags_str = NULL;\r
123 -    for (db_tags = notmuch_message_get_tags (message);\r
124 -        notmuch_tags_valid (db_tags);\r
125 -        notmuch_tags_move_to_next (db_tags)) {\r
126 -       tag = notmuch_tags_get (db_tags);\r
127 -\r
128 -       if (db_tags_str)\r
129 -           db_tags_str = talloc_asprintf_append (db_tags_str, " %s", tag);\r
130 -       else\r
131 -           db_tags_str = talloc_strdup (message, tag);\r
132 -    }\r
133 +    if ((flags & TAG_FLAG_REMOVE_ALL) || tag_op_list_size (tag_ops))\r
134 +       tag_op_list_apply (message, tag_ops, flags);\r
135  \r
136 -    if (((file_tags == NULL || *file_tags == '\0') &&\r
137 -        (db_tags_str == NULL || *db_tags_str == '\0')) ||\r
138 -       (file_tags && db_tags_str && strcmp (file_tags, db_tags_str) == 0))\r
139 -       goto DONE;\r
140 +    notmuch_message_destroy (message);\r
141  \r
142 -    notmuch_message_freeze (message);\r
143 +    return ret;\r
144 +}\r
145  \r
146 -    if (remove_all)\r
147 -       notmuch_message_remove_all_tags (message);\r
148 +/* Sup dump output is one line per message. We match a sequence of\r
149 + * non-space characters for the message-id, then one or more\r
150 + * spaces, then a list of space-separated tags as a sequence of\r
151 + * characters within literal '(' and ')'. */\r
152  \r
153 -    next = file_tags;\r
154 -    while (next) {\r
155 -       tag = strsep (&next, " ");\r
156 -       if (*tag == '\0')\r
157 -           continue;\r
158 -       status = notmuch_message_add_tag (message, tag);\r
159 -       if (status) {\r
160 -           fprintf (stderr, "Error applying tag %s to message %s:\n",\r
161 -                    tag, message_id);\r
162 -           fprintf (stderr, "%s\n", notmuch_status_to_string (status));\r
163 -           ret = 1;\r
164 -       }\r
165 +static int\r
166 +parse_sup_line (void *ctx, char *line,\r
167 +               char **query_str, tag_op_list_t *tag_ops)\r
168 +{\r
169 +\r
170 +    regmatch_t match[3];\r
171 +    char *file_tags;\r
172 +    int rerr;\r
173 +\r
174 +    tag_op_list_reset (tag_ops);\r
175 +\r
176 +    chomp_newline (line);\r
177 +\r
178 +    /* Silently ignore blank lines */\r
179 +    if (line[0] == '\0') {\r
180 +       return 1;\r
181      }\r
182  \r
183 -    notmuch_message_thaw (message);\r
184 +    rerr = xregexec (&regex, line, 3, match, 0);\r
185 +    if (rerr == REG_NOMATCH) {\r
186 +       fprintf (stderr, "Warning: Ignoring invalid sup format line: %s\n",\r
187 +                line);\r
188 +       return 1;\r
189 +    }\r
190  \r
191 -    if (synchronize_flags)\r
192 -       notmuch_message_tags_to_maildir_flags (message);\r
193 +    *query_str = talloc_strndup (ctx, line + match[1].rm_so,\r
194 +                                match[1].rm_eo - match[1].rm_so);\r
195 +    file_tags = talloc_strndup (ctx, line + match[2].rm_so,\r
196 +                               match[2].rm_eo - match[2].rm_so);\r
197  \r
198 -  DONE:\r
199 -    if (message)\r
200 -       notmuch_message_destroy (message);\r
201 +    char *tok = file_tags;\r
202 +    size_t tok_len = 0;\r
203 +\r
204 +    tag_op_list_reset (tag_ops);\r
205 +\r
206 +    while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {\r
207 +\r
208 +       if (*(tok + tok_len) != '\0') {\r
209 +           *(tok + tok_len) = '\0';\r
210 +           tok_len++;\r
211 +       }\r
212 +\r
213 +       if (tag_op_list_append (ctx, tag_ops, tok, FALSE))\r
214 +           return -1;\r
215 +    }\r
216 +\r
217 +    return 0;\r
218  \r
219 -    return ret;\r
220  }\r
221  \r
222  int\r
223 @@ -100,16 +116,19 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
224  {\r
225      notmuch_config_t *config;\r
226      notmuch_database_t *notmuch;\r
227 -    notmuch_bool_t synchronize_flags;\r
228      notmuch_bool_t accumulate = FALSE;\r
229 +    tag_op_flag_t flags = 0;\r
230 +    tag_op_list_t *tag_ops;\r
231 +\r
232      char *input_file_name = NULL;\r
233      FILE *input = stdin;\r
234      char *line = NULL;\r
235      size_t line_size;\r
236      ssize_t line_len;\r
237 -    regex_t regex;\r
238 -    int rerr;\r
239 +\r
240 +    int ret = 0;\r
241      int opt_index;\r
242 +    int input_format = DUMP_FORMAT_AUTO;\r
243  \r
244      config = notmuch_config_open (ctx, NULL, NULL);\r
245      if (config == NULL)\r
246 @@ -119,9 +138,15 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
247                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
248         return 1;\r
249  \r
250 -    synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
251 +    if (notmuch_config_get_maildir_synchronize_flags (config))\r
252 +       flags |= TAG_FLAG_MAILDIR_SYNC;\r
253  \r
254      notmuch_opt_desc_t options[] = {\r
255 +       { NOTMUCH_OPT_KEYWORD, &input_format, "format", 'f',\r
256 +         (notmuch_keyword_t []){ { "auto", DUMP_FORMAT_AUTO },\r
257 +                                 { "batch-tag", DUMP_FORMAT_BATCH_TAG },\r
258 +                                 { "sup", DUMP_FORMAT_SUP },\r
259 +                                 { 0, 0 } } },\r
260         { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },\r
261         { NOTMUCH_OPT_BOOLEAN,  &accumulate, "accumulate", 'a', 0 },\r
262         { 0, 0, 0, 0, 0 }\r
263 @@ -134,6 +159,9 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
264         return 1;\r
265      }\r
266  \r
267 +    if (! accumulate)\r
268 +       flags |= TAG_FLAG_REMOVE_ALL;\r
269 +\r
270      if (input_file_name) {\r
271         input = fopen (input_file_name, "r");\r
272         if (input == NULL) {\r
273 @@ -149,49 +177,77 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
274                  argv[opt_index]);\r
275         return 1;\r
276      }\r
277 +    char *p;\r
278  \r
279 -    /* Dump output is one line per message. We match a sequence of\r
280 -     * non-space characters for the message-id, then one or more\r
281 -     * spaces, then a list of space-separated tags as a sequence of\r
282 -     * characters within literal '(' and ')'. */\r
283 -    if ( xregcomp (&regex,\r
284 -                  "^([^ ]+) \\(([^)]*)\\)$",\r
285 -                  REG_EXTENDED) )\r
286 -       INTERNAL_ERROR ("compile time constant regex failed.");\r
287 -\r
288 -    while ((line_len = getline (&line, &line_size, input)) != -1) {\r
289 -       regmatch_t match[3];\r
290 -       char *message_id, *file_tags;\r
291 -\r
292 -       chomp_newline (line);\r
293 -\r
294 -       rerr = xregexec (&regex, line, 3, match, 0);\r
295 -       if (rerr == REG_NOMATCH) {\r
296 -           fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",\r
297 -                    line);\r
298 -           continue;\r
299 +    line_len = getline (&line, &line_size, input);\r
300 +    if (line_len == 0)\r
301 +       return 0;\r
302 +\r
303 +    tag_ops = tag_op_list_create (ctx);\r
304 +    if (tag_ops == NULL) {\r
305 +       fprintf (stderr, "Out of memory.\n");\r
306 +       return 1;\r
307 +    }\r
308 +\r
309 +    for (p = line; (input_format == DUMP_FORMAT_AUTO) && *p; p++) {\r
310 +       if (*p == '(')\r
311 +           input_format = DUMP_FORMAT_SUP;\r
312 +    }\r
313 +\r
314 +    if (input_format == DUMP_FORMAT_AUTO)\r
315 +       input_format = DUMP_FORMAT_BATCH_TAG;\r
316 +\r
317 +    if (input_format == DUMP_FORMAT_SUP)\r
318 +       if ( xregcomp (&regex,\r
319 +                      "^([^ ]+) \\(([^)]*)\\)$",\r
320 +                      REG_EXTENDED) )\r
321 +           INTERNAL_ERROR ("compile time constant regex failed.");\r
322 +\r
323 +    do {\r
324 +       char *query_string;\r
325 +\r
326 +       if (input_format == DUMP_FORMAT_SUP) {\r
327 +           ret = parse_sup_line (ctx, line, &query_string, tag_ops);\r
328 +       } else {\r
329 +           ret = parse_tag_line (ctx, line, TAG_FLAG_BE_GENEROUS,\r
330 +                                 &query_string, tag_ops);\r
331 +\r
332 +           if (ret == 0) {\r
333 +               if (strncmp ("id:", query_string, 3) != 0) {\r
334 +                   fprintf (stderr, "Unsupported query: %s\n", query_string);\r
335 +                   continue;\r
336 +               }\r
337 +               /* delete id: from front of string; tag_message\r
338 +                * expects a raw message-id.\r
339 +                *\r
340 +                * XXX: Note that query string id:foo and bar will be\r
341 +                * interpreted as a message id "foo and bar". This\r
342 +                * should eventually be fixed to give a better error\r
343 +                * message.\r
344 +                */\r
345 +               query_string = query_string + 3;\r
346 +           }\r
347         }\r
348  \r
349 -       message_id = xstrndup (line + match[1].rm_so,\r
350 -                              match[1].rm_eo - match[1].rm_so);\r
351 -       file_tags = xstrndup (line + match[2].rm_so,\r
352 -                             match[2].rm_eo - match[2].rm_so);\r
353 +       if (ret > 0)\r
354 +           continue;\r
355  \r
356 -       tag_message (notmuch, message_id, file_tags, ! accumulate,\r
357 -                    synchronize_flags);\r
358 +       if (ret < 0 || tag_message (ctx, notmuch, query_string,\r
359 +                                   tag_ops, flags))\r
360 +           break;\r
361  \r
362 -       free (message_id);\r
363 -       free (file_tags);\r
364 -    }\r
365 +    }  while ((line_len = getline (&line, &line_size, input)) != -1);\r
366  \r
367 -    regfree (&regex);\r
368 +    if (input_format == DUMP_FORMAT_SUP)\r
369 +       regfree (&regex);\r
370  \r
371      if (line)\r
372         free (line);\r
373  \r
374      notmuch_database_destroy (notmuch);\r
375 +\r
376      if (input != stdin)\r
377         fclose (input);\r
378  \r
379 -    return 0;\r
380 +    return ret;\r
381  }\r
382 -- \r
383 1.7.10.4\r
384 \r