RE: Reply all - issue
[notmuch-archives.git] / 3d / 703c69c7f58e9b58d19f84619672239d77bcc2
1 Return-Path: <jani@nikula.org>\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 CB996431FC0\r
6         for <notmuch@notmuchmail.org>; Sun, 21 Oct 2012 14:22:50 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: -0.7\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
13         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id B9hjXVpsoJLg for <notmuch@notmuchmail.org>;\r
17         Sun, 21 Oct 2012 14:22:47 -0700 (PDT)\r
18 Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com\r
19         [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 9DDAA431FD0\r
22         for <notmuch@notmuchmail.org>; Sun, 21 Oct 2012 14:22:46 -0700 (PDT)\r
23 Received: by mail-lb0-f181.google.com with SMTP id gg6so1515820lbb.26\r
24         for <notmuch@notmuchmail.org>; Sun, 21 Oct 2012 14:22:46 -0700 (PDT)\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
26         d=google.com; s=20120113;\r
27         h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references\r
28         :in-reply-to:references:mime-version:content-type\r
29         :content-transfer-encoding:x-gm-message-state;\r
30         bh=fozFlLFWYRPIq5jUxgriYDkFz2iAlH550DL/Poy0uz8=;\r
31         b=PVVF0NdjhipnClVHeJ8KfkrcsFtnoT8BjIWz2vtySKta3LdIsLxPENKzcolFJfwiTS\r
32         SZFhkGFnRu2th/P+nYypDmMkLY+VBaojd1vCtQjUQnEzFHzNStPKulKb7N09mNiFB97I\r
33         rm6Dp4cqChOlFrny0Iuj9T7KFO52zyvLPGLLl+WloIAlFY9YOi/jO8jpwHK9Bu+d8Yvk\r
34         Lx3kIuWrJ6ampwAtIlH6rf1B1Mwsc3t9uvVLE9BRgEW8/aIToSztUqxfRxycld4STAJD\r
35         hODHAGpQgCqco4+U+iL8tNiZNCgpYY22exSk7mLAyb0duzr3JkRsxB2ht7eGsResC7Uw\r
36         /o6Q==\r
37 Received: by 10.112.101.10 with SMTP id fc10mr2912074lbb.82.1350854566216;\r
38         Sun, 21 Oct 2012 14:22:46 -0700 (PDT)\r
39 Received: from localhost (dsl-hkibrasgw4-fe51df00-27.dhcp.inet.fi.\r
40         [80.223.81.27])\r
41         by mx.google.com with ESMTPS id oj5sm2417681lab.8.2012.10.21.14.22.44\r
42         (version=SSLv3 cipher=OTHER); Sun, 21 Oct 2012 14:22:45 -0700 (PDT)\r
43 From: Jani Nikula <jani@nikula.org>\r
44 To: notmuch@notmuchmail.org\r
45 Subject:\r
46  [PATCH v5 3/9] test: add new test tool parse-time for date/time parser\r
47 Date: Mon, 22 Oct 2012 00:22:26 +0300\r
48 Message-Id:\r
49  <75a8f129d5e0d824b3e04ddfc1816c45fa0ec70d.1350854171.git.jani@nikula.org>\r
50 X-Mailer: git-send-email 1.7.10.4\r
51 In-Reply-To: <cover.1350854171.git.jani@nikula.org>\r
52 References: <cover.1350854171.git.jani@nikula.org>\r
53 In-Reply-To: <cover.1350854171.git.jani@nikula.org>\r
54 References: <cover.1350854171.git.jani@nikula.org>\r
55 MIME-Version: 1.0\r
56 Content-Type: text/plain; charset=UTF-8\r
57 Content-Transfer-Encoding: 8bit\r
58 X-Gm-Message-State:\r
59  ALoCoQmOzixYrVfDjuQ7k4hg2i7RF4hyj/xUZhAGUulVcnYqHlCx3j16hV2RTX8+aR/6lWvhTXsV\r
60 X-BeenThere: notmuch@notmuchmail.org\r
61 X-Mailman-Version: 2.1.13\r
62 Precedence: list\r
63 List-Id: "Use and development of the notmuch mail system."\r
64         <notmuch.notmuchmail.org>\r
65 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
66         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
67 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
68 List-Post: <mailto:notmuch@notmuchmail.org>\r
69 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
70 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
71         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
72 X-List-Received-Date: Sun, 21 Oct 2012 21:22:51 -0000\r
73 \r
74 Add a smoke testing tool to support testing the date/time parser\r
75 module directly and independent of the rest of notmuch.\r
76 \r
77 Credits to Michal Sojka <sojkam1@fel.cvut.cz> for the stdin parsing\r
78 idea and consequent massive improvement in testability.\r
79 ---\r
80  test/Makefile.local |    7 +-\r
81  test/basic          |    2 +-\r
82  test/parse-time.c   |  281 +++++++++++++++++++++++++++++++++++++++++++++++++++\r
83  3 files changed, 288 insertions(+), 2 deletions(-)\r
84  create mode 100644 test/parse-time.c\r
85 \r
86 diff --git a/test/Makefile.local b/test/Makefile.local\r
87 index 45df4c7..9ae130a 100644\r
88 --- a/test/Makefile.local\r
89 +++ b/test/Makefile.local\r
90 @@ -19,9 +19,13 @@ $(dir)/smtp-dummy: $(smtp_dummy_modules)\r
91  $(dir)/symbol-test: $(dir)/symbol-test.o\r
92         $(call quiet,CXX) $^ -o $@ -Llib -lnotmuch $(XAPIAN_LDFLAGS)\r
93  \r
94 +$(dir)/parse-time: $(dir)/parse-time.o parse-time-string/parse-time-string.o\r
95 +       $(call quiet,CC) $^ -o $@\r
96 +\r
97  .PHONY: test check\r
98  \r
99 -test-binaries: $(dir)/arg-test $(dir)/smtp-dummy $(dir)/symbol-test\r
100 +test-binaries: $(dir)/arg-test $(dir)/smtp-dummy $(dir)/symbol-test \\r
101 +       $(dir)/parse-time\r
102  \r
103  test:  all test-binaries\r
104         @${dir}/notmuch-test $(OPTIONS)\r
105 @@ -32,4 +36,5 @@ SRCS := $(SRCS) $(smtp_dummy_srcs)\r
106  CLEAN := $(CLEAN) $(dir)/smtp-dummy $(dir)/smtp-dummy.o \\r
107          $(dir)/symbol-test $(dir)/symbol-test.o \\r
108          $(dir)/arg-test $(dir)/arg-test.o \\r
109 +        $(dir)/parse-time $(dir)/parse-time.o \\r
110          $(dir)/corpus.mail $(dir)/test-results $(dir)/tmp.*\r
111 diff --git a/test/basic b/test/basic\r
112 index 3b635c8..c47197c 100755\r
113 --- a/test/basic\r
114 +++ b/test/basic\r
115 @@ -54,7 +54,7 @@ test_begin_subtest 'Ensure that all available tests will be run by notmuch-test'\r
116  eval $(sed -n -e '/^TESTS="$/,/^"$/p' $TEST_DIRECTORY/notmuch-test)\r
117  tests_in_suite=$(for i in $TESTS; do echo $i; done | sort)\r
118  available=$(find "$TEST_DIRECTORY" -maxdepth 1 -type f -perm +111 | \\r
119 -    sed -r -e "s,.*/,," -e "/^(aggregate-results.sh|notmuch-test|smtp-dummy|test-verbose|symbol-test|arg-test)$/d" | \\r
120 +    sed -r -e "s,.*/,," -e "/^(aggregate-results.sh|notmuch-test|smtp-dummy|test-verbose|symbol-test|arg-test|parse-time)$/d" | \\r
121      sort)\r
122  test_expect_equal "$tests_in_suite" "$available"\r
123  \r
124 diff --git a/test/parse-time.c b/test/parse-time.c\r
125 new file mode 100644\r
126 index 0000000..5f73b85\r
127 --- /dev/null\r
128 +++ b/test/parse-time.c\r
129 @@ -0,0 +1,281 @@\r
130 +/*\r
131 + * parse time string - user friendly date and time parser\r
132 + * Copyright © 2012 Jani Nikula\r
133 + *\r
134 + * This program is free software: you can redistribute it and/or modify\r
135 + * it under the terms of the GNU General Public License as published by\r
136 + * the Free Software Foundation, either version 2 of the License, or\r
137 + * (at your option) any later version.\r
138 + *\r
139 + * This program is distributed in the hope that it will be useful,\r
140 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
141 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
142 + * GNU General Public License for more details.\r
143 + *\r
144 + * You should have received a copy of the GNU General Public License\r
145 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
146 + *\r
147 + * Author: Jani Nikula <jani@nikula.org>\r
148 + */\r
149 +\r
150 +#include <assert.h>\r
151 +#include <ctype.h>\r
152 +#include <getopt.h>\r
153 +#include <stdio.h>\r
154 +#include <stdlib.h>\r
155 +#include <string.h>\r
156 +\r
157 +#include "parse-time-string.h"\r
158 +\r
159 +#define ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0]))\r
160 +\r
161 +/*\r
162 + * concat argv[start]...argv[end - 1], separating them by a single\r
163 + * space, to a malloced string\r
164 + */\r
165 +static char *\r
166 +concat_args (int start, int end, char *argv[])\r
167 +{\r
168 +    int i;\r
169 +    size_t len = 1;\r
170 +    char *p;\r
171 +\r
172 +    for (i = start; i < end; i++)\r
173 +       len += strlen (argv[i]) + 1;\r
174 +\r
175 +    p = malloc (len);\r
176 +    if (!p)\r
177 +       return NULL;\r
178 +\r
179 +    *p = 0;\r
180 +\r
181 +    for (i = start; i < end; i++) {\r
182 +       if (i != start)\r
183 +           strcat (p, " ");\r
184 +       strcat (p, argv[i]);\r
185 +    }\r
186 +\r
187 +    return p;\r
188 +}\r
189 +\r
190 +#define DEFAULT_FORMAT "%a %b %d %T %z %Y"\r
191 +\r
192 +static void\r
193 +usage (const char *name)\r
194 +{\r
195 +    printf ("Usage: %s [options ...] [<date/time>]\n\n", name);\r
196 +    printf (\r
197 +       "Parse <date/time> and display it in given format. If <date/time> is\n"\r
198 +       "not given, parse each line in stdin according to:\n\n"\r
199 +       "  <date/time> [(==>|==_>|==^>|==^^>)<ignored>] [#<comment>]\n\n"\r
200 +       "and produce output:\n\n"\r
201 +       "  <date/time> (==>|==_>|==^>|==^^>) <time in --format=FMT> [#<comment>]\n\n"\r
202 +       "preserving whitespace and comment in input. The operators ==>, ==_>,\n"\r
203 +       "==^>, and ==^^> define rounding as no rounding, round down, round up\n"\r
204 +       "inclusive, and round up, respectively.\n\n"\r
205 +\r
206 +       "  -f, --format=FMT output format, FMT according to strftime(3)\n"\r
207 +       "                   (default: \"%s\")\n"\r
208 +       "  -r, --ref=N      use N seconds since epoch as reference time\n"\r
209 +       "                   (default: now)\n"\r
210 +       "  -u, --^          round result up inclusive (default: no rounding)\n"\r
211 +       "  -U, --^^         round result up (default: no rounding)\n"\r
212 +       "  -d, --_          round result down (default: no rounding)\n"\r
213 +       "  -h, --help       print this help\n",\r
214 +       DEFAULT_FORMAT);\r
215 +}\r
216 +\r
217 +struct {\r
218 +    const char *operator;\r
219 +    int round;\r
220 +} operators[] = {\r
221 +    { "==>",   PARSE_TIME_NO_ROUND },\r
222 +    { "==_>",  PARSE_TIME_ROUND_DOWN },\r
223 +    { "==^>",  PARSE_TIME_ROUND_UP_INCLUSIVE },\r
224 +    { "==^^>", PARSE_TIME_ROUND_UP },\r
225 +};\r
226 +\r
227 +static const char *\r
228 +find_operator_in_string (char *str, char **ptr, int *round)\r
229 +{\r
230 +    const char *oper = NULL;\r
231 +    unsigned int i;\r
232 +\r
233 +    for (i = 0; i < ARRAY_SIZE (operators); i++) {\r
234 +       char *p = strstr (str, operators[i].operator);\r
235 +       if (p) {\r
236 +           if (round)\r
237 +               *round = operators[i].round;\r
238 +           if (ptr)\r
239 +               *ptr = p;\r
240 +\r
241 +           oper = operators[i].operator;\r
242 +           break;\r
243 +       }\r
244 +    }\r
245 +\r
246 +    return oper;\r
247 +}\r
248 +\r
249 +static const char *\r
250 +get_operator (int round)\r
251 +{\r
252 +    const char *oper = NULL;\r
253 +    unsigned int i;\r
254 +\r
255 +    for (i = 0; i < ARRAY_SIZE(operators); i++) {\r
256 +       if (round == operators[i].round) {\r
257 +           oper = operators[i].operator;\r
258 +           break;\r
259 +       }\r
260 +    }\r
261 +\r
262 +    return oper;\r
263 +}\r
264 +\r
265 +static int\r
266 +parse_stdin (FILE *infile, time_t *ref, int round, const char *format)\r
267 +{\r
268 +    char *input = NULL;\r
269 +    char result[1024];\r
270 +    size_t inputsize;\r
271 +    ssize_t len;\r
272 +    struct tm tm;\r
273 +    time_t t;\r
274 +    int r;\r
275 +\r
276 +    while ((len = getline (&input, &inputsize, infile)) != -1) {\r
277 +       const char *oper;\r
278 +       char *trail, *tmp;\r
279 +\r
280 +       /* trail is trailing whitespace and (optional) comment */\r
281 +       trail = strchr (input, '#');\r
282 +       if (!trail)\r
283 +           trail = input + len;\r
284 +\r
285 +       while (trail > input && isspace ((unsigned char) *(trail-1)))\r
286 +           trail--;\r
287 +\r
288 +       if (trail == input) {\r
289 +           printf ("%s", input);\r
290 +           continue;\r
291 +       }\r
292 +\r
293 +       tmp = strdup (trail);\r
294 +       if (!tmp) {\r
295 +           fprintf (stderr, "strdup() failed\n");\r
296 +           continue;\r
297 +       }\r
298 +       *trail = '\0';\r
299 +       trail = tmp;\r
300 +\r
301 +       /* operator */\r
302 +       oper = find_operator_in_string (input, &tmp, &round);\r
303 +       if (oper) {\r
304 +           *tmp = '\0';\r
305 +       } else {\r
306 +           oper = get_operator (round);\r
307 +           assert (oper);\r
308 +       }\r
309 +\r
310 +       r = parse_time_string (input, &t, ref, round);\r
311 +       if (!r) {\r
312 +           if (!localtime_r (&t, &tm)) {\r
313 +               fprintf (stderr, "localtime_r() failed\n");\r
314 +               free (trail);\r
315 +               continue;\r
316 +           }\r
317 +\r
318 +           strftime (result, sizeof (result), format, &tm);\r
319 +       } else {\r
320 +           snprintf (result, sizeof (result), "ERROR: %d", r);\r
321 +       }\r
322 +\r
323 +       printf ("%s%s %s%s", input, oper, result, trail);\r
324 +       free (trail);\r
325 +    }\r
326 +\r
327 +    free (input);\r
328 +\r
329 +    return 0;\r
330 +}\r
331 +\r
332 +int\r
333 +main (int argc, char *argv[])\r
334 +{\r
335 +    int r;\r
336 +    struct tm tm;\r
337 +    time_t result;\r
338 +    time_t now;\r
339 +    time_t *nowp = NULL;\r
340 +    char *argstr;\r
341 +    int round = PARSE_TIME_NO_ROUND;\r
342 +    char buf[1024];\r
343 +    const char *format = DEFAULT_FORMAT;\r
344 +    struct option options[] = {\r
345 +       { "help",       no_argument,            NULL,   'h' },\r
346 +       { "^",          no_argument,            NULL,   'u' },\r
347 +       { "^^",         no_argument,            NULL,   'U' },\r
348 +       { "_",          no_argument,            NULL,   'd' },\r
349 +       { "format",     required_argument,      NULL,   'f' },\r
350 +       { "ref",        required_argument,      NULL,   'r' },\r
351 +       { NULL, 0, NULL, 0 },\r
352 +    };\r
353 +\r
354 +    for (;;) {\r
355 +       int c;\r
356 +\r
357 +       c = getopt_long (argc, argv, "huUdf:r:", options, NULL);\r
358 +       if (c == -1)\r
359 +           break;\r
360 +\r
361 +       switch (c) {\r
362 +       case 'f':\r
363 +           /* output format */\r
364 +           format = optarg;\r
365 +           break;\r
366 +       case 'u':\r
367 +           round = PARSE_TIME_ROUND_UP_INCLUSIVE;\r
368 +           break;\r
369 +       case 'U':\r
370 +           round = PARSE_TIME_ROUND_UP;\r
371 +           break;\r
372 +       case 'd':\r
373 +           round = PARSE_TIME_ROUND_DOWN;\r
374 +           break;\r
375 +       case 'r':\r
376 +           /* specify now in seconds since epoch */\r
377 +           now = (time_t) strtol (optarg, NULL, 10);\r
378 +           if (now >= (time_t) 0)\r
379 +               nowp = &now;\r
380 +           break;\r
381 +       case 'h':\r
382 +       case '?':\r
383 +       default:\r
384 +           usage (argv[0]);\r
385 +           return 1;\r
386 +       }\r
387 +    }\r
388 +\r
389 +    if (optind == argc)\r
390 +       return parse_stdin (stdin, nowp, round, format);\r
391 +\r
392 +    argstr = concat_args (optind, argc, argv);\r
393 +    if (!argstr)\r
394 +       return 1;\r
395 +\r
396 +    r = parse_time_string (argstr, &result, nowp, round);\r
397 +\r
398 +    free (argstr);\r
399 +\r
400 +    if (r)\r
401 +       return 1;\r
402 +\r
403 +    if (!localtime_r (&result, &tm))\r
404 +       return 1;\r
405 +\r
406 +    strftime (buf, sizeof (buf), format, &tm);\r
407 +    printf ("%s\n", buf);\r
408 +\r
409 +    return 0;\r
410 +}\r
411 -- \r
412 1.7.10.4\r
413 \r